1e2be04c7SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ 2daedfb22SAlexei Starovoitov /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com 3daedfb22SAlexei Starovoitov * 4daedfb22SAlexei Starovoitov * This program is free software; you can redistribute it and/or 5daedfb22SAlexei Starovoitov * modify it under the terms of version 2 of the GNU General Public 6daedfb22SAlexei Starovoitov * License as published by the Free Software Foundation. 7daedfb22SAlexei Starovoitov */ 8daedfb22SAlexei Starovoitov #ifndef _UAPI__LINUX_BPF_H__ 9daedfb22SAlexei Starovoitov #define _UAPI__LINUX_BPF_H__ 10daedfb22SAlexei Starovoitov 11daedfb22SAlexei Starovoitov #include <linux/types.h> 12c15952dcSAlexei Starovoitov #include <linux/bpf_common.h> 13daedfb22SAlexei Starovoitov 14daedfb22SAlexei Starovoitov /* Extended instruction set based on top of classic BPF */ 15daedfb22SAlexei Starovoitov 16daedfb22SAlexei Starovoitov /* instruction classes */ 17d405c740SJiong Wang #define BPF_JMP32 0x06 /* jmp mode in word width */ 18daedfb22SAlexei Starovoitov #define BPF_ALU64 0x07 /* alu mode in double word width */ 19daedfb22SAlexei Starovoitov 20daedfb22SAlexei Starovoitov /* ld/ldx fields */ 21cb5f7334SJesper Dangaard Brouer #define BPF_DW 0x18 /* double word (64-bit) */ 22daedfb22SAlexei Starovoitov #define BPF_XADD 0xc0 /* exclusive add */ 23daedfb22SAlexei Starovoitov 24daedfb22SAlexei Starovoitov /* alu/jmp fields */ 25daedfb22SAlexei Starovoitov #define BPF_MOV 0xb0 /* mov reg to reg */ 26daedfb22SAlexei Starovoitov #define BPF_ARSH 0xc0 /* sign extending arithmetic shift right */ 27daedfb22SAlexei Starovoitov 28daedfb22SAlexei Starovoitov /* change endianness of a register */ 29daedfb22SAlexei Starovoitov #define BPF_END 0xd0 /* flags for endianness conversion: */ 30daedfb22SAlexei Starovoitov #define BPF_TO_LE 0x00 /* convert to little-endian */ 31daedfb22SAlexei Starovoitov #define BPF_TO_BE 0x08 /* convert to big-endian */ 32daedfb22SAlexei Starovoitov #define BPF_FROM_LE BPF_TO_LE 33daedfb22SAlexei Starovoitov #define BPF_FROM_BE BPF_TO_BE 34daedfb22SAlexei Starovoitov 3592b31a9aSDaniel Borkmann /* jmp encodings */ 36daedfb22SAlexei Starovoitov #define BPF_JNE 0x50 /* jump != */ 3792b31a9aSDaniel Borkmann #define BPF_JLT 0xa0 /* LT is unsigned, '<' */ 3892b31a9aSDaniel Borkmann #define BPF_JLE 0xb0 /* LE is unsigned, '<=' */ 39daedfb22SAlexei Starovoitov #define BPF_JSGT 0x60 /* SGT is signed '>', GT in x86 */ 40daedfb22SAlexei Starovoitov #define BPF_JSGE 0x70 /* SGE is signed '>=', GE in x86 */ 4192b31a9aSDaniel Borkmann #define BPF_JSLT 0xc0 /* SLT is signed, '<' */ 4292b31a9aSDaniel Borkmann #define BPF_JSLE 0xd0 /* SLE is signed, '<=' */ 43daedfb22SAlexei Starovoitov #define BPF_CALL 0x80 /* function call */ 44daedfb22SAlexei Starovoitov #define BPF_EXIT 0x90 /* function return */ 45daedfb22SAlexei Starovoitov 46daedfb22SAlexei Starovoitov /* Register numbers */ 47daedfb22SAlexei Starovoitov enum { 48daedfb22SAlexei Starovoitov BPF_REG_0 = 0, 49daedfb22SAlexei Starovoitov BPF_REG_1, 50daedfb22SAlexei Starovoitov BPF_REG_2, 51daedfb22SAlexei Starovoitov BPF_REG_3, 52daedfb22SAlexei Starovoitov BPF_REG_4, 53daedfb22SAlexei Starovoitov BPF_REG_5, 54daedfb22SAlexei Starovoitov BPF_REG_6, 55daedfb22SAlexei Starovoitov BPF_REG_7, 56daedfb22SAlexei Starovoitov BPF_REG_8, 57daedfb22SAlexei Starovoitov BPF_REG_9, 58daedfb22SAlexei Starovoitov BPF_REG_10, 59daedfb22SAlexei Starovoitov __MAX_BPF_REG, 60daedfb22SAlexei Starovoitov }; 61daedfb22SAlexei Starovoitov 62daedfb22SAlexei Starovoitov /* BPF has 10 general purpose 64-bit registers and stack frame. */ 63daedfb22SAlexei Starovoitov #define MAX_BPF_REG __MAX_BPF_REG 64daedfb22SAlexei Starovoitov 65daedfb22SAlexei Starovoitov struct bpf_insn { 66daedfb22SAlexei Starovoitov __u8 code; /* opcode */ 67daedfb22SAlexei Starovoitov __u8 dst_reg:4; /* dest register */ 68daedfb22SAlexei Starovoitov __u8 src_reg:4; /* source register */ 69daedfb22SAlexei Starovoitov __s16 off; /* signed offset */ 70daedfb22SAlexei Starovoitov __s32 imm; /* signed immediate constant */ 71daedfb22SAlexei Starovoitov }; 72daedfb22SAlexei Starovoitov 73b95a5c4dSDaniel Mack /* Key of an a BPF_MAP_TYPE_LPM_TRIE entry */ 74b95a5c4dSDaniel Mack struct bpf_lpm_trie_key { 75b95a5c4dSDaniel Mack __u32 prefixlen; /* up to 32 for AF_INET, 128 for AF_INET6 */ 76b95a5c4dSDaniel Mack __u8 data[0]; /* Arbitrary size */ 77b95a5c4dSDaniel Mack }; 78b95a5c4dSDaniel Mack 79de9cbbaaSRoman Gushchin struct bpf_cgroup_storage_key { 80de9cbbaaSRoman Gushchin __u64 cgroup_inode_id; /* cgroup inode id */ 81de9cbbaaSRoman Gushchin __u32 attach_type; /* program attach type */ 82de9cbbaaSRoman Gushchin }; 83de9cbbaaSRoman Gushchin 84b2197755SDaniel Borkmann /* BPF syscall commands, see bpf(2) man-page for details. */ 8599c55f7dSAlexei Starovoitov enum bpf_cmd { 8699c55f7dSAlexei Starovoitov BPF_MAP_CREATE, 87db20fd2bSAlexei Starovoitov BPF_MAP_LOOKUP_ELEM, 88db20fd2bSAlexei Starovoitov BPF_MAP_UPDATE_ELEM, 89db20fd2bSAlexei Starovoitov BPF_MAP_DELETE_ELEM, 90db20fd2bSAlexei Starovoitov BPF_MAP_GET_NEXT_KEY, 9109756af4SAlexei Starovoitov BPF_PROG_LOAD, 92b2197755SDaniel Borkmann BPF_OBJ_PIN, 93b2197755SDaniel Borkmann BPF_OBJ_GET, 94f4324551SDaniel Mack BPF_PROG_ATTACH, 95f4324551SDaniel Mack BPF_PROG_DETACH, 961cf1cae9SAlexei Starovoitov BPF_PROG_TEST_RUN, 9734ad5580SMartin KaFai Lau BPF_PROG_GET_NEXT_ID, 9834ad5580SMartin KaFai Lau BPF_MAP_GET_NEXT_ID, 99b16d9aa4SMartin KaFai Lau BPF_PROG_GET_FD_BY_ID, 100bd5f5f4eSMartin KaFai Lau BPF_MAP_GET_FD_BY_ID, 1011e270976SMartin KaFai Lau BPF_OBJ_GET_INFO_BY_FD, 102468e2f64SAlexei Starovoitov BPF_PROG_QUERY, 103c4f6699dSAlexei Starovoitov BPF_RAW_TRACEPOINT_OPEN, 104f56a653cSMartin KaFai Lau BPF_BTF_LOAD, 10578958fcaSMartin KaFai Lau BPF_BTF_GET_FD_BY_ID, 10641bdc4b4SYonghong Song BPF_TASK_FD_QUERY, 107bd513cd0SMauricio Vasquez B BPF_MAP_LOOKUP_AND_DELETE_ELEM, 10887df15deSDaniel Borkmann BPF_MAP_FREEZE, 1091b9ed84eSQuentin Monnet BPF_BTF_GET_NEXT_ID, 110cb4d03abSBrian Vazquez BPF_MAP_LOOKUP_BATCH, 11105799638SYonghong Song BPF_MAP_LOOKUP_AND_DELETE_BATCH, 112aa2e93b8SBrian Vazquez BPF_MAP_UPDATE_BATCH, 113aa2e93b8SBrian Vazquez BPF_MAP_DELETE_BATCH, 11499c55f7dSAlexei Starovoitov }; 11599c55f7dSAlexei Starovoitov 11699c55f7dSAlexei Starovoitov enum bpf_map_type { 11799c55f7dSAlexei Starovoitov BPF_MAP_TYPE_UNSPEC, 1180f8e4bd8SAlexei Starovoitov BPF_MAP_TYPE_HASH, 11928fbcfa0SAlexei Starovoitov BPF_MAP_TYPE_ARRAY, 12004fd61abSAlexei Starovoitov BPF_MAP_TYPE_PROG_ARRAY, 121ea317b26SKaixu Xia BPF_MAP_TYPE_PERF_EVENT_ARRAY, 122824bd0ceSAlexei Starovoitov BPF_MAP_TYPE_PERCPU_HASH, 123a10423b8SAlexei Starovoitov BPF_MAP_TYPE_PERCPU_ARRAY, 124d5a3b1f6SAlexei Starovoitov BPF_MAP_TYPE_STACK_TRACE, 1254ed8ec52SMartin KaFai Lau BPF_MAP_TYPE_CGROUP_ARRAY, 12629ba732aSMartin KaFai Lau BPF_MAP_TYPE_LRU_HASH, 1278f844938SMartin KaFai Lau BPF_MAP_TYPE_LRU_PERCPU_HASH, 128b95a5c4dSDaniel Mack BPF_MAP_TYPE_LPM_TRIE, 12956f668dfSMartin KaFai Lau BPF_MAP_TYPE_ARRAY_OF_MAPS, 130bcc6b1b7SMartin KaFai Lau BPF_MAP_TYPE_HASH_OF_MAPS, 131546ac1ffSJohn Fastabend BPF_MAP_TYPE_DEVMAP, 132174a79ffSJohn Fastabend BPF_MAP_TYPE_SOCKMAP, 1336710e112SJesper Dangaard Brouer BPF_MAP_TYPE_CPUMAP, 134fbfc504aSBjörn Töpel BPF_MAP_TYPE_XSKMAP, 13581110384SJohn Fastabend BPF_MAP_TYPE_SOCKHASH, 136de9cbbaaSRoman Gushchin BPF_MAP_TYPE_CGROUP_STORAGE, 1375dc4c4b7SMartin KaFai Lau BPF_MAP_TYPE_REUSEPORT_SOCKARRAY, 138b741f163SRoman Gushchin BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE, 139f1a2e44aSMauricio Vasquez B BPF_MAP_TYPE_QUEUE, 140f1a2e44aSMauricio Vasquez B BPF_MAP_TYPE_STACK, 1416ac99e8fSMartin KaFai Lau BPF_MAP_TYPE_SK_STORAGE, 1426f9d451aSToke Høiland-Jørgensen BPF_MAP_TYPE_DEVMAP_HASH, 14385d33df3SMartin KaFai Lau BPF_MAP_TYPE_STRUCT_OPS, 14499c55f7dSAlexei Starovoitov }; 14599c55f7dSAlexei Starovoitov 1466c4fc209SDaniel Borkmann /* Note that tracing related programs such as 1476c4fc209SDaniel Borkmann * BPF_PROG_TYPE_{KPROBE,TRACEPOINT,PERF_EVENT,RAW_TRACEPOINT} 1486c4fc209SDaniel Borkmann * are not subject to a stable API since kernel internal data 1496c4fc209SDaniel Borkmann * structures can change from release to release and may 1506c4fc209SDaniel Borkmann * therefore break existing tracing BPF programs. Tracing BPF 1516c4fc209SDaniel Borkmann * programs correspond to /a/ specific kernel which is to be 1526c4fc209SDaniel Borkmann * analyzed, and not /a/ specific kernel /and/ all future ones. 1536c4fc209SDaniel Borkmann */ 15409756af4SAlexei Starovoitov enum bpf_prog_type { 15509756af4SAlexei Starovoitov BPF_PROG_TYPE_UNSPEC, 156ddd872bcSAlexei Starovoitov BPF_PROG_TYPE_SOCKET_FILTER, 1572541517cSAlexei Starovoitov BPF_PROG_TYPE_KPROBE, 15896be4325SDaniel Borkmann BPF_PROG_TYPE_SCHED_CLS, 15994caee8cSDaniel Borkmann BPF_PROG_TYPE_SCHED_ACT, 16098b5c2c6SAlexei Starovoitov BPF_PROG_TYPE_TRACEPOINT, 1616a773a15SBrenden Blanco BPF_PROG_TYPE_XDP, 1620515e599SAlexei Starovoitov BPF_PROG_TYPE_PERF_EVENT, 1630e33661dSDaniel Mack BPF_PROG_TYPE_CGROUP_SKB, 16461023658SDavid Ahern BPF_PROG_TYPE_CGROUP_SOCK, 1653a0af8fdSThomas Graf BPF_PROG_TYPE_LWT_IN, 1663a0af8fdSThomas Graf BPF_PROG_TYPE_LWT_OUT, 1673a0af8fdSThomas Graf BPF_PROG_TYPE_LWT_XMIT, 16840304b2aSLawrence Brakmo BPF_PROG_TYPE_SOCK_OPS, 169b005fd18SJohn Fastabend BPF_PROG_TYPE_SK_SKB, 170ebc614f6SRoman Gushchin BPF_PROG_TYPE_CGROUP_DEVICE, 1714f738adbSJohn Fastabend BPF_PROG_TYPE_SK_MSG, 172c4f6699dSAlexei Starovoitov BPF_PROG_TYPE_RAW_TRACEPOINT, 1734fbac77dSAndrey Ignatov BPF_PROG_TYPE_CGROUP_SOCK_ADDR, 174004d4b27SMathieu Xhonneux BPF_PROG_TYPE_LWT_SEG6LOCAL, 175f4364dcfSSean Young BPF_PROG_TYPE_LIRC_MODE2, 1762dbb9b9eSMartin KaFai Lau BPF_PROG_TYPE_SK_REUSEPORT, 177d58e468bSPetar Penkov BPF_PROG_TYPE_FLOW_DISSECTOR, 1787b146cebSAndrey Ignatov BPF_PROG_TYPE_CGROUP_SYSCTL, 1799df1c28bSMatt Mullins BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE, 1800d01da6aSStanislav Fomichev BPF_PROG_TYPE_CGROUP_SOCKOPT, 181f1b9509cSAlexei Starovoitov BPF_PROG_TYPE_TRACING, 18227ae7997SMartin KaFai Lau BPF_PROG_TYPE_STRUCT_OPS, 183be8704ffSAlexei Starovoitov BPF_PROG_TYPE_EXT, 18409756af4SAlexei Starovoitov }; 18509756af4SAlexei Starovoitov 1860e33661dSDaniel Mack enum bpf_attach_type { 1870e33661dSDaniel Mack BPF_CGROUP_INET_INGRESS, 1880e33661dSDaniel Mack BPF_CGROUP_INET_EGRESS, 18961023658SDavid Ahern BPF_CGROUP_INET_SOCK_CREATE, 19040304b2aSLawrence Brakmo BPF_CGROUP_SOCK_OPS, 191464bc0fdSJohn Fastabend BPF_SK_SKB_STREAM_PARSER, 192464bc0fdSJohn Fastabend BPF_SK_SKB_STREAM_VERDICT, 193ebc614f6SRoman Gushchin BPF_CGROUP_DEVICE, 1944f738adbSJohn Fastabend BPF_SK_MSG_VERDICT, 1954fbac77dSAndrey Ignatov BPF_CGROUP_INET4_BIND, 1964fbac77dSAndrey Ignatov BPF_CGROUP_INET6_BIND, 197d74bad4eSAndrey Ignatov BPF_CGROUP_INET4_CONNECT, 198d74bad4eSAndrey Ignatov BPF_CGROUP_INET6_CONNECT, 199aac3fc32SAndrey Ignatov BPF_CGROUP_INET4_POST_BIND, 200aac3fc32SAndrey Ignatov BPF_CGROUP_INET6_POST_BIND, 2011cedee13SAndrey Ignatov BPF_CGROUP_UDP4_SENDMSG, 2021cedee13SAndrey Ignatov BPF_CGROUP_UDP6_SENDMSG, 203f4364dcfSSean Young BPF_LIRC_MODE2, 204d58e468bSPetar Penkov BPF_FLOW_DISSECTOR, 2057b146cebSAndrey Ignatov BPF_CGROUP_SYSCTL, 206983695faSDaniel Borkmann BPF_CGROUP_UDP4_RECVMSG, 207983695faSDaniel Borkmann BPF_CGROUP_UDP6_RECVMSG, 2080d01da6aSStanislav Fomichev BPF_CGROUP_GETSOCKOPT, 2090d01da6aSStanislav Fomichev BPF_CGROUP_SETSOCKOPT, 210f1b9509cSAlexei Starovoitov BPF_TRACE_RAW_TP, 211fec56f58SAlexei Starovoitov BPF_TRACE_FENTRY, 212fec56f58SAlexei Starovoitov BPF_TRACE_FEXIT, 2130e33661dSDaniel Mack __MAX_BPF_ATTACH_TYPE 2140e33661dSDaniel Mack }; 2150e33661dSDaniel Mack 2160e33661dSDaniel Mack #define MAX_BPF_ATTACH_TYPE __MAX_BPF_ATTACH_TYPE 2170e33661dSDaniel Mack 218324bda9eSAlexei Starovoitov /* cgroup-bpf attach flags used in BPF_PROG_ATTACH command 219324bda9eSAlexei Starovoitov * 220324bda9eSAlexei Starovoitov * NONE(default): No further bpf programs allowed in the subtree. 221324bda9eSAlexei Starovoitov * 222324bda9eSAlexei Starovoitov * BPF_F_ALLOW_OVERRIDE: If a sub-cgroup installs some bpf program, 223324bda9eSAlexei Starovoitov * the program in this cgroup yields to sub-cgroup program. 224324bda9eSAlexei Starovoitov * 225324bda9eSAlexei Starovoitov * BPF_F_ALLOW_MULTI: If a sub-cgroup installs some bpf program, 226324bda9eSAlexei Starovoitov * that cgroup program gets run in addition to the program in this cgroup. 227324bda9eSAlexei Starovoitov * 228324bda9eSAlexei Starovoitov * Only one program is allowed to be attached to a cgroup with 229324bda9eSAlexei Starovoitov * NONE or BPF_F_ALLOW_OVERRIDE flag. 230324bda9eSAlexei Starovoitov * Attaching another program on top of NONE or BPF_F_ALLOW_OVERRIDE will 231324bda9eSAlexei Starovoitov * release old program and attach the new one. Attach flags has to match. 232324bda9eSAlexei Starovoitov * 233324bda9eSAlexei Starovoitov * Multiple programs are allowed to be attached to a cgroup with 234324bda9eSAlexei Starovoitov * BPF_F_ALLOW_MULTI flag. They are executed in FIFO order 235324bda9eSAlexei Starovoitov * (those that were attached first, run first) 236324bda9eSAlexei Starovoitov * The programs of sub-cgroup are executed first, then programs of 237324bda9eSAlexei Starovoitov * this cgroup and then programs of parent cgroup. 238324bda9eSAlexei Starovoitov * When children program makes decision (like picking TCP CA or sock bind) 239324bda9eSAlexei Starovoitov * parent program has a chance to override it. 240324bda9eSAlexei Starovoitov * 2417dd68b32SAndrey Ignatov * With BPF_F_ALLOW_MULTI a new program is added to the end of the list of 2427dd68b32SAndrey Ignatov * programs for a cgroup. Though it's possible to replace an old program at 2437dd68b32SAndrey Ignatov * any position by also specifying BPF_F_REPLACE flag and position itself in 2447dd68b32SAndrey Ignatov * replace_bpf_fd attribute. Old program at this position will be released. 2457dd68b32SAndrey Ignatov * 246324bda9eSAlexei Starovoitov * A cgroup with MULTI or OVERRIDE flag allows any attach flags in sub-cgroups. 247324bda9eSAlexei Starovoitov * A cgroup with NONE doesn't allow any programs in sub-cgroups. 248324bda9eSAlexei Starovoitov * Ex1: 249324bda9eSAlexei Starovoitov * cgrp1 (MULTI progs A, B) -> 250324bda9eSAlexei Starovoitov * cgrp2 (OVERRIDE prog C) -> 251324bda9eSAlexei Starovoitov * cgrp3 (MULTI prog D) -> 252324bda9eSAlexei Starovoitov * cgrp4 (OVERRIDE prog E) -> 253324bda9eSAlexei Starovoitov * cgrp5 (NONE prog F) 254324bda9eSAlexei Starovoitov * the event in cgrp5 triggers execution of F,D,A,B in that order. 255324bda9eSAlexei Starovoitov * if prog F is detached, the execution is E,D,A,B 256324bda9eSAlexei Starovoitov * if prog F and D are detached, the execution is E,A,B 257324bda9eSAlexei Starovoitov * if prog F, E and D are detached, the execution is C,A,B 258324bda9eSAlexei Starovoitov * 259324bda9eSAlexei Starovoitov * All eligible programs are executed regardless of return code from 260324bda9eSAlexei Starovoitov * earlier programs. 2617f677633SAlexei Starovoitov */ 2627f677633SAlexei Starovoitov #define BPF_F_ALLOW_OVERRIDE (1U << 0) 263324bda9eSAlexei Starovoitov #define BPF_F_ALLOW_MULTI (1U << 1) 2647dd68b32SAndrey Ignatov #define BPF_F_REPLACE (1U << 2) 2657f677633SAlexei Starovoitov 266e07b98d9SDavid S. Miller /* If BPF_F_STRICT_ALIGNMENT is used in BPF_PROG_LOAD command, the 267e07b98d9SDavid S. Miller * verifier will perform strict alignment checking as if the kernel 268e07b98d9SDavid S. Miller * has been built with CONFIG_EFFICIENT_UNALIGNED_ACCESS not set, 269e07b98d9SDavid S. Miller * and NET_IP_ALIGN defined to 2. 270e07b98d9SDavid S. Miller */ 271e07b98d9SDavid S. Miller #define BPF_F_STRICT_ALIGNMENT (1U << 0) 272e07b98d9SDavid S. Miller 273e9ee9efcSDavid Miller /* If BPF_F_ANY_ALIGNMENT is used in BPF_PROF_LOAD command, the 274e9ee9efcSDavid Miller * verifier will allow any alignment whatsoever. On platforms 275e9ee9efcSDavid Miller * with strict alignment requirements for loads ands stores (such 276e9ee9efcSDavid Miller * as sparc and mips) the verifier validates that all loads and 277e9ee9efcSDavid Miller * stores provably follow this requirement. This flag turns that 278e9ee9efcSDavid Miller * checking and enforcement off. 279e9ee9efcSDavid Miller * 280e9ee9efcSDavid Miller * It is mostly used for testing when we want to validate the 281e9ee9efcSDavid Miller * context and memory access aspects of the verifier, but because 282e9ee9efcSDavid Miller * of an unaligned access the alignment check would trigger before 283e9ee9efcSDavid Miller * the one we are interested in. 284e9ee9efcSDavid Miller */ 285e9ee9efcSDavid Miller #define BPF_F_ANY_ALIGNMENT (1U << 1) 286e9ee9efcSDavid Miller 287c240eff6SJiong Wang /* BPF_F_TEST_RND_HI32 is used in BPF_PROG_LOAD command for testing purpose. 288c240eff6SJiong Wang * Verifier does sub-register def/use analysis and identifies instructions whose 289c240eff6SJiong Wang * def only matters for low 32-bit, high 32-bit is never referenced later 290c240eff6SJiong Wang * through implicit zero extension. Therefore verifier notifies JIT back-ends 291c240eff6SJiong Wang * that it is safe to ignore clearing high 32-bit for these instructions. This 292c240eff6SJiong Wang * saves some back-ends a lot of code-gen. However such optimization is not 293c240eff6SJiong Wang * necessary on some arches, for example x86_64, arm64 etc, whose JIT back-ends 294c240eff6SJiong Wang * hence hasn't used verifier's analysis result. But, we really want to have a 295c240eff6SJiong Wang * way to be able to verify the correctness of the described optimization on 296c240eff6SJiong Wang * x86_64 on which testsuites are frequently exercised. 297c240eff6SJiong Wang * 298c240eff6SJiong Wang * So, this flag is introduced. Once it is set, verifier will randomize high 299c240eff6SJiong Wang * 32-bit for those instructions who has been identified as safe to ignore them. 300c240eff6SJiong Wang * Then, if verifier is not doing correct analysis, such randomization will 301c240eff6SJiong Wang * regress tests to expose bugs. 302c240eff6SJiong Wang */ 303c240eff6SJiong Wang #define BPF_F_TEST_RND_HI32 (1U << 2) 304c240eff6SJiong Wang 30510d274e8SAlexei Starovoitov /* The verifier internal test flag. Behavior is undefined */ 30610d274e8SAlexei Starovoitov #define BPF_F_TEST_STATE_FREQ (1U << 3) 30710d274e8SAlexei Starovoitov 308d8eca5bbSDaniel Borkmann /* When BPF ldimm64's insn[0].src_reg != 0 then this can have 309d8eca5bbSDaniel Borkmann * two extensions: 310d8eca5bbSDaniel Borkmann * 311d8eca5bbSDaniel Borkmann * insn[0].src_reg: BPF_PSEUDO_MAP_FD BPF_PSEUDO_MAP_VALUE 312d8eca5bbSDaniel Borkmann * insn[0].imm: map fd map fd 313d8eca5bbSDaniel Borkmann * insn[1].imm: 0 offset into value 314d8eca5bbSDaniel Borkmann * insn[0].off: 0 0 315d8eca5bbSDaniel Borkmann * insn[1].off: 0 0 316d8eca5bbSDaniel Borkmann * ldimm64 rewrite: address of map address of map[0]+offset 317d8eca5bbSDaniel Borkmann * verifier type: CONST_PTR_TO_MAP PTR_TO_MAP_VALUE 318d8eca5bbSDaniel Borkmann */ 319f1a66f85SDaniel Borkmann #define BPF_PSEUDO_MAP_FD 1 320d8eca5bbSDaniel Borkmann #define BPF_PSEUDO_MAP_VALUE 2 321f1a66f85SDaniel Borkmann 322cc8b0b92SAlexei Starovoitov /* when bpf_call->src_reg == BPF_PSEUDO_CALL, bpf_call->imm == pc-relative 323cc8b0b92SAlexei Starovoitov * offset to another bpf function 324cc8b0b92SAlexei Starovoitov */ 325cc8b0b92SAlexei Starovoitov #define BPF_PSEUDO_CALL 1 326cc8b0b92SAlexei Starovoitov 3273274f520SAlexei Starovoitov /* flags for BPF_MAP_UPDATE_ELEM command */ 3283274f520SAlexei Starovoitov #define BPF_ANY 0 /* create new element or update existing */ 3293274f520SAlexei Starovoitov #define BPF_NOEXIST 1 /* create new element if it didn't exist */ 3303274f520SAlexei Starovoitov #define BPF_EXIST 2 /* update existing element */ 33196049f3aSAlexei Starovoitov #define BPF_F_LOCK 4 /* spin_lock-ed map_lookup/map_update */ 3323274f520SAlexei Starovoitov 33396eabe7aSMartin KaFai Lau /* flags for BPF_MAP_CREATE command */ 3346c905981SAlexei Starovoitov #define BPF_F_NO_PREALLOC (1U << 0) 33529ba732aSMartin KaFai Lau /* Instead of having one common LRU list in the 3368f844938SMartin KaFai Lau * BPF_MAP_TYPE_LRU_[PERCPU_]HASH map, use a percpu LRU list 33729ba732aSMartin KaFai Lau * which can scale and perform better. 33829ba732aSMartin KaFai Lau * Note, the LRU nodes (including free nodes) cannot be moved 33929ba732aSMartin KaFai Lau * across different LRU lists. 34029ba732aSMartin KaFai Lau */ 34129ba732aSMartin KaFai Lau #define BPF_F_NO_COMMON_LRU (1U << 1) 34296eabe7aSMartin KaFai Lau /* Specify numa node during map creation */ 34396eabe7aSMartin KaFai Lau #define BPF_F_NUMA_NODE (1U << 2) 3446c905981SAlexei Starovoitov 345cb4d2b3fSMartin KaFai Lau #define BPF_OBJ_NAME_LEN 16U 346cb4d2b3fSMartin KaFai Lau 347591fe988SDaniel Borkmann /* Flags for accessing BPF object from syscall side. */ 3486e71b04aSChenbo Feng #define BPF_F_RDONLY (1U << 3) 3496e71b04aSChenbo Feng #define BPF_F_WRONLY (1U << 4) 3506e71b04aSChenbo Feng 351615755a7SSong Liu /* Flag for stack_map, store build_id+offset instead of pointer */ 352615755a7SSong Liu #define BPF_F_STACK_BUILD_ID (1U << 5) 353615755a7SSong Liu 35496b3b6c9SLorenz Bauer /* Zero-initialize hash function seed. This should only be used for testing. */ 35596b3b6c9SLorenz Bauer #define BPF_F_ZERO_SEED (1U << 6) 35696b3b6c9SLorenz Bauer 357591fe988SDaniel Borkmann /* Flags for accessing BPF object from program side. */ 358591fe988SDaniel Borkmann #define BPF_F_RDONLY_PROG (1U << 7) 359591fe988SDaniel Borkmann #define BPF_F_WRONLY_PROG (1U << 8) 360591fe988SDaniel Borkmann 3618f51dfc7SStanislav Fomichev /* Clone map from listener for newly accepted socket */ 3628f51dfc7SStanislav Fomichev #define BPF_F_CLONE (1U << 9) 3638f51dfc7SStanislav Fomichev 364fc970227SAndrii Nakryiko /* Enable memory-mapping BPF map */ 365fc970227SAndrii Nakryiko #define BPF_F_MMAPABLE (1U << 10) 366fc970227SAndrii Nakryiko 367f5bfcd95SAndrey Ignatov /* Flags for BPF_PROG_QUERY. */ 368f5bfcd95SAndrey Ignatov 369f5bfcd95SAndrey Ignatov /* Query effective (directly attached + inherited from ancestor cgroups) 370f5bfcd95SAndrey Ignatov * programs that will be executed for events within a cgroup. 371f5bfcd95SAndrey Ignatov * attach_flags with this flag are returned only for directly attached programs. 372f5bfcd95SAndrey Ignatov */ 3732f183360SLorenz Bauer #define BPF_F_QUERY_EFFECTIVE (1U << 0) 3742f183360SLorenz Bauer 375615755a7SSong Liu enum bpf_stack_build_id_status { 376615755a7SSong Liu /* user space need an empty entry to identify end of a trace */ 377615755a7SSong Liu BPF_STACK_BUILD_ID_EMPTY = 0, 378615755a7SSong Liu /* with valid build_id and offset */ 379615755a7SSong Liu BPF_STACK_BUILD_ID_VALID = 1, 380615755a7SSong Liu /* couldn't get build_id, fallback to ip */ 381615755a7SSong Liu BPF_STACK_BUILD_ID_IP = 2, 382615755a7SSong Liu }; 383615755a7SSong Liu 384615755a7SSong Liu #define BPF_BUILD_ID_SIZE 20 385615755a7SSong Liu struct bpf_stack_build_id { 386615755a7SSong Liu __s32 status; 387615755a7SSong Liu unsigned char build_id[BPF_BUILD_ID_SIZE]; 388615755a7SSong Liu union { 389615755a7SSong Liu __u64 offset; 390615755a7SSong Liu __u64 ip; 391615755a7SSong Liu }; 392615755a7SSong Liu }; 393615755a7SSong Liu 39499c55f7dSAlexei Starovoitov union bpf_attr { 39599c55f7dSAlexei Starovoitov struct { /* anonymous struct used by BPF_MAP_CREATE command */ 39699c55f7dSAlexei Starovoitov __u32 map_type; /* one of enum bpf_map_type */ 39799c55f7dSAlexei Starovoitov __u32 key_size; /* size of key in bytes */ 39899c55f7dSAlexei Starovoitov __u32 value_size; /* size of value in bytes */ 39999c55f7dSAlexei Starovoitov __u32 max_entries; /* max number of entries in a map */ 40096eabe7aSMartin KaFai Lau __u32 map_flags; /* BPF_MAP_CREATE related 40196eabe7aSMartin KaFai Lau * flags defined above. 40296eabe7aSMartin KaFai Lau */ 40356f668dfSMartin KaFai Lau __u32 inner_map_fd; /* fd pointing to the inner map */ 40496eabe7aSMartin KaFai Lau __u32 numa_node; /* numa node (effective only if 40596eabe7aSMartin KaFai Lau * BPF_F_NUMA_NODE is set). 40696eabe7aSMartin KaFai Lau */ 407067cae47SMartin KaFai Lau char map_name[BPF_OBJ_NAME_LEN]; 408a3884572SJakub Kicinski __u32 map_ifindex; /* ifindex of netdev to create on */ 409a26ca7c9SMartin KaFai Lau __u32 btf_fd; /* fd pointing to a BTF type data */ 4109b2cf328SMartin KaFai Lau __u32 btf_key_type_id; /* BTF type_id of the key */ 4119b2cf328SMartin KaFai Lau __u32 btf_value_type_id; /* BTF type_id of the value */ 41285d33df3SMartin KaFai Lau __u32 btf_vmlinux_value_type_id;/* BTF type_id of a kernel- 41385d33df3SMartin KaFai Lau * struct stored as the 41485d33df3SMartin KaFai Lau * map value 41585d33df3SMartin KaFai Lau */ 41699c55f7dSAlexei Starovoitov }; 417db20fd2bSAlexei Starovoitov 418db20fd2bSAlexei Starovoitov struct { /* anonymous struct used by BPF_MAP_*_ELEM commands */ 419db20fd2bSAlexei Starovoitov __u32 map_fd; 420db20fd2bSAlexei Starovoitov __aligned_u64 key; 421db20fd2bSAlexei Starovoitov union { 422db20fd2bSAlexei Starovoitov __aligned_u64 value; 423db20fd2bSAlexei Starovoitov __aligned_u64 next_key; 424db20fd2bSAlexei Starovoitov }; 4253274f520SAlexei Starovoitov __u64 flags; 426db20fd2bSAlexei Starovoitov }; 42709756af4SAlexei Starovoitov 428cb4d03abSBrian Vazquez struct { /* struct used by BPF_MAP_*_BATCH commands */ 429cb4d03abSBrian Vazquez __aligned_u64 in_batch; /* start batch, 430cb4d03abSBrian Vazquez * NULL to start from beginning 431cb4d03abSBrian Vazquez */ 432cb4d03abSBrian Vazquez __aligned_u64 out_batch; /* output: next start batch */ 433cb4d03abSBrian Vazquez __aligned_u64 keys; 434cb4d03abSBrian Vazquez __aligned_u64 values; 435cb4d03abSBrian Vazquez __u32 count; /* input/output: 436cb4d03abSBrian Vazquez * input: # of key/value 437cb4d03abSBrian Vazquez * elements 438cb4d03abSBrian Vazquez * output: # of filled elements 439cb4d03abSBrian Vazquez */ 440cb4d03abSBrian Vazquez __u32 map_fd; 441cb4d03abSBrian Vazquez __u64 elem_flags; 442cb4d03abSBrian Vazquez __u64 flags; 443cb4d03abSBrian Vazquez } batch; 444cb4d03abSBrian Vazquez 44509756af4SAlexei Starovoitov struct { /* anonymous struct used by BPF_PROG_LOAD command */ 44609756af4SAlexei Starovoitov __u32 prog_type; /* one of enum bpf_prog_type */ 44709756af4SAlexei Starovoitov __u32 insn_cnt; 44809756af4SAlexei Starovoitov __aligned_u64 insns; 44909756af4SAlexei Starovoitov __aligned_u64 license; 450cbd35700SAlexei Starovoitov __u32 log_level; /* verbosity level of verifier */ 451cbd35700SAlexei Starovoitov __u32 log_size; /* size of user buffer */ 452cbd35700SAlexei Starovoitov __aligned_u64 log_buf; /* user supplied buffer */ 4536c4fc209SDaniel Borkmann __u32 kern_version; /* not used */ 454e07b98d9SDavid S. Miller __u32 prog_flags; 455067cae47SMartin KaFai Lau char prog_name[BPF_OBJ_NAME_LEN]; 4561f6f4cb7SJakub Kicinski __u32 prog_ifindex; /* ifindex of netdev to prep for */ 4575e43f899SAndrey Ignatov /* For some prog types expected attach type must be known at 4585e43f899SAndrey Ignatov * load time to verify attach type specific parts of prog 4595e43f899SAndrey Ignatov * (context accesses, allowed helpers, etc). 4605e43f899SAndrey Ignatov */ 4615e43f899SAndrey Ignatov __u32 expected_attach_type; 462838e9690SYonghong Song __u32 prog_btf_fd; /* fd pointing to BTF type data */ 463838e9690SYonghong Song __u32 func_info_rec_size; /* userspace bpf_func_info size */ 464838e9690SYonghong Song __aligned_u64 func_info; /* func info */ 465838e9690SYonghong Song __u32 func_info_cnt; /* number of bpf_func_info records */ 466c454a46bSMartin KaFai Lau __u32 line_info_rec_size; /* userspace bpf_line_info size */ 467c454a46bSMartin KaFai Lau __aligned_u64 line_info; /* line info */ 468c454a46bSMartin KaFai Lau __u32 line_info_cnt; /* number of bpf_line_info records */ 469ccfe29ebSAlexei Starovoitov __u32 attach_btf_id; /* in-kernel BTF type id to attach to */ 4705b92a28aSAlexei Starovoitov __u32 attach_prog_fd; /* 0 to attach to vmlinux */ 47109756af4SAlexei Starovoitov }; 472b2197755SDaniel Borkmann 473b2197755SDaniel Borkmann struct { /* anonymous struct used by BPF_OBJ_* commands */ 474b2197755SDaniel Borkmann __aligned_u64 pathname; 475b2197755SDaniel Borkmann __u32 bpf_fd; 4766e71b04aSChenbo Feng __u32 file_flags; 477b2197755SDaniel Borkmann }; 478f4324551SDaniel Mack 479f4324551SDaniel Mack struct { /* anonymous struct used by BPF_PROG_ATTACH/DETACH commands */ 480f4324551SDaniel Mack __u32 target_fd; /* container object to attach to */ 481f4324551SDaniel Mack __u32 attach_bpf_fd; /* eBPF program to attach */ 482f4324551SDaniel Mack __u32 attach_type; 4837f677633SAlexei Starovoitov __u32 attach_flags; 4847dd68b32SAndrey Ignatov __u32 replace_bpf_fd; /* previously attached eBPF 4857dd68b32SAndrey Ignatov * program to replace if 4867dd68b32SAndrey Ignatov * BPF_F_REPLACE is used 4877dd68b32SAndrey Ignatov */ 488f4324551SDaniel Mack }; 4891cf1cae9SAlexei Starovoitov 4901cf1cae9SAlexei Starovoitov struct { /* anonymous struct used by BPF_PROG_TEST_RUN command */ 4911cf1cae9SAlexei Starovoitov __u32 prog_fd; 4921cf1cae9SAlexei Starovoitov __u32 retval; 493b5a36b1eSLorenz Bauer __u32 data_size_in; /* input: len of data_in */ 494b5a36b1eSLorenz Bauer __u32 data_size_out; /* input/output: len of data_out 495b5a36b1eSLorenz Bauer * returns ENOSPC if data_out 496b5a36b1eSLorenz Bauer * is too small. 497b5a36b1eSLorenz Bauer */ 4981cf1cae9SAlexei Starovoitov __aligned_u64 data_in; 4991cf1cae9SAlexei Starovoitov __aligned_u64 data_out; 5001cf1cae9SAlexei Starovoitov __u32 repeat; 5011cf1cae9SAlexei Starovoitov __u32 duration; 502b0b9395dSStanislav Fomichev __u32 ctx_size_in; /* input: len of ctx_in */ 503b0b9395dSStanislav Fomichev __u32 ctx_size_out; /* input/output: len of ctx_out 504b0b9395dSStanislav Fomichev * returns ENOSPC if ctx_out 505b0b9395dSStanislav Fomichev * is too small. 506b0b9395dSStanislav Fomichev */ 507b0b9395dSStanislav Fomichev __aligned_u64 ctx_in; 508b0b9395dSStanislav Fomichev __aligned_u64 ctx_out; 5091cf1cae9SAlexei Starovoitov } test; 51034ad5580SMartin KaFai Lau 511b16d9aa4SMartin KaFai Lau struct { /* anonymous struct used by BPF_*_GET_*_ID */ 512b16d9aa4SMartin KaFai Lau union { 51334ad5580SMartin KaFai Lau __u32 start_id; 514b16d9aa4SMartin KaFai Lau __u32 prog_id; 515bd5f5f4eSMartin KaFai Lau __u32 map_id; 51678958fcaSMartin KaFai Lau __u32 btf_id; 517b16d9aa4SMartin KaFai Lau }; 51834ad5580SMartin KaFai Lau __u32 next_id; 5196e71b04aSChenbo Feng __u32 open_flags; 52034ad5580SMartin KaFai Lau }; 5211e270976SMartin KaFai Lau 5221e270976SMartin KaFai Lau struct { /* anonymous struct used by BPF_OBJ_GET_INFO_BY_FD */ 5231e270976SMartin KaFai Lau __u32 bpf_fd; 5241e270976SMartin KaFai Lau __u32 info_len; 5251e270976SMartin KaFai Lau __aligned_u64 info; 5261e270976SMartin KaFai Lau } info; 527468e2f64SAlexei Starovoitov 528468e2f64SAlexei Starovoitov struct { /* anonymous struct used by BPF_PROG_QUERY command */ 529468e2f64SAlexei Starovoitov __u32 target_fd; /* container object to query */ 530468e2f64SAlexei Starovoitov __u32 attach_type; 531468e2f64SAlexei Starovoitov __u32 query_flags; 532468e2f64SAlexei Starovoitov __u32 attach_flags; 533468e2f64SAlexei Starovoitov __aligned_u64 prog_ids; 534468e2f64SAlexei Starovoitov __u32 prog_cnt; 535468e2f64SAlexei Starovoitov } query; 536c4f6699dSAlexei Starovoitov 537c4f6699dSAlexei Starovoitov struct { 538c4f6699dSAlexei Starovoitov __u64 name; 539c4f6699dSAlexei Starovoitov __u32 prog_fd; 540c4f6699dSAlexei Starovoitov } raw_tracepoint; 541f56a653cSMartin KaFai Lau 542f56a653cSMartin KaFai Lau struct { /* anonymous struct for BPF_BTF_LOAD */ 543f56a653cSMartin KaFai Lau __aligned_u64 btf; 544f56a653cSMartin KaFai Lau __aligned_u64 btf_log_buf; 545f56a653cSMartin KaFai Lau __u32 btf_size; 546f56a653cSMartin KaFai Lau __u32 btf_log_size; 547f56a653cSMartin KaFai Lau __u32 btf_log_level; 548f56a653cSMartin KaFai Lau }; 54941bdc4b4SYonghong Song 55041bdc4b4SYonghong Song struct { 55141bdc4b4SYonghong Song __u32 pid; /* input: pid */ 55241bdc4b4SYonghong Song __u32 fd; /* input: fd */ 55341bdc4b4SYonghong Song __u32 flags; /* input: flags */ 55441bdc4b4SYonghong Song __u32 buf_len; /* input/output: buf len */ 55541bdc4b4SYonghong Song __aligned_u64 buf; /* input/output: 55641bdc4b4SYonghong Song * tp_name for tracepoint 55741bdc4b4SYonghong Song * symbol for kprobe 55841bdc4b4SYonghong Song * filename for uprobe 55941bdc4b4SYonghong Song */ 56041bdc4b4SYonghong Song __u32 prog_id; /* output: prod_id */ 56141bdc4b4SYonghong Song __u32 fd_type; /* output: BPF_FD_TYPE_* */ 56241bdc4b4SYonghong Song __u64 probe_offset; /* output: probe_offset */ 56341bdc4b4SYonghong Song __u64 probe_addr; /* output: probe_addr */ 56441bdc4b4SYonghong Song } task_fd_query; 56599c55f7dSAlexei Starovoitov } __attribute__((aligned(8))); 56699c55f7dSAlexei Starovoitov 56756a092c8SQuentin Monnet /* The description below is an attempt at providing documentation to eBPF 56856a092c8SQuentin Monnet * developers about the multiple available eBPF helper functions. It can be 56956a092c8SQuentin Monnet * parsed and used to produce a manual page. The workflow is the following, 57056a092c8SQuentin Monnet * and requires the rst2man utility: 571ebb676daSThomas Graf * 57256a092c8SQuentin Monnet * $ ./scripts/bpf_helpers_doc.py \ 57356a092c8SQuentin Monnet * --filename include/uapi/linux/bpf.h > /tmp/bpf-helpers.rst 57456a092c8SQuentin Monnet * $ rst2man /tmp/bpf-helpers.rst > /tmp/bpf-helpers.7 57556a092c8SQuentin Monnet * $ man /tmp/bpf-helpers.7 576ebb676daSThomas Graf * 57756a092c8SQuentin Monnet * Note that in order to produce this external documentation, some RST 57856a092c8SQuentin Monnet * formatting is used in the descriptions to get "bold" and "italics" in 57956a092c8SQuentin Monnet * manual pages. Also note that the few trailing white spaces are 58056a092c8SQuentin Monnet * intentional, removing them would break paragraphs for rst2man. 581ebb676daSThomas Graf * 58256a092c8SQuentin Monnet * Start of BPF helper function descriptions: 583ad4a5223SQuentin Monnet * 584ad4a5223SQuentin Monnet * void *bpf_map_lookup_elem(struct bpf_map *map, const void *key) 585ad4a5223SQuentin Monnet * Description 586ad4a5223SQuentin Monnet * Perform a lookup in *map* for an entry associated to *key*. 587ad4a5223SQuentin Monnet * Return 588ad4a5223SQuentin Monnet * Map value associated to *key*, or **NULL** if no entry was 589ad4a5223SQuentin Monnet * found. 590ad4a5223SQuentin Monnet * 591ad4a5223SQuentin Monnet * int bpf_map_update_elem(struct bpf_map *map, const void *key, const void *value, u64 flags) 592ad4a5223SQuentin Monnet * Description 593ad4a5223SQuentin Monnet * Add or update the value of the entry associated to *key* in 594ad4a5223SQuentin Monnet * *map* with *value*. *flags* is one of: 595ad4a5223SQuentin Monnet * 596ad4a5223SQuentin Monnet * **BPF_NOEXIST** 597ad4a5223SQuentin Monnet * The entry for *key* must not exist in the map. 598ad4a5223SQuentin Monnet * **BPF_EXIST** 599ad4a5223SQuentin Monnet * The entry for *key* must already exist in the map. 600ad4a5223SQuentin Monnet * **BPF_ANY** 601ad4a5223SQuentin Monnet * No condition on the existence of the entry for *key*. 602ad4a5223SQuentin Monnet * 603ad4a5223SQuentin Monnet * Flag value **BPF_NOEXIST** cannot be used for maps of types 604ad4a5223SQuentin Monnet * **BPF_MAP_TYPE_ARRAY** or **BPF_MAP_TYPE_PERCPU_ARRAY** (all 605ad4a5223SQuentin Monnet * elements always exist), the helper would return an error. 606ad4a5223SQuentin Monnet * Return 607ad4a5223SQuentin Monnet * 0 on success, or a negative error in case of failure. 608ad4a5223SQuentin Monnet * 609ad4a5223SQuentin Monnet * int bpf_map_delete_elem(struct bpf_map *map, const void *key) 610ad4a5223SQuentin Monnet * Description 611ad4a5223SQuentin Monnet * Delete entry with *key* from *map*. 612ad4a5223SQuentin Monnet * Return 613ad4a5223SQuentin Monnet * 0 on success, or a negative error in case of failure. 614ad4a5223SQuentin Monnet * 6156ae08ae3SDaniel Borkmann * int bpf_probe_read(void *dst, u32 size, const void *unsafe_ptr) 616ad4a5223SQuentin Monnet * Description 617ad4a5223SQuentin Monnet * For tracing programs, safely attempt to read *size* bytes from 6186ae08ae3SDaniel Borkmann * kernel space address *unsafe_ptr* and store the data in *dst*. 6196ae08ae3SDaniel Borkmann * 6206ae08ae3SDaniel Borkmann * Generally, use bpf_probe_read_user() or bpf_probe_read_kernel() 6216ae08ae3SDaniel Borkmann * instead. 622ad4a5223SQuentin Monnet * Return 623ad4a5223SQuentin Monnet * 0 on success, or a negative error in case of failure. 624ad4a5223SQuentin Monnet * 625ad4a5223SQuentin Monnet * u64 bpf_ktime_get_ns(void) 626ad4a5223SQuentin Monnet * Description 627ad4a5223SQuentin Monnet * Return the time elapsed since system boot, in nanoseconds. 628ad4a5223SQuentin Monnet * Return 629ad4a5223SQuentin Monnet * Current *ktime*. 630ad4a5223SQuentin Monnet * 631ad4a5223SQuentin Monnet * int bpf_trace_printk(const char *fmt, u32 fmt_size, ...) 632ad4a5223SQuentin Monnet * Description 633ad4a5223SQuentin Monnet * This helper is a "printk()-like" facility for debugging. It 634ad4a5223SQuentin Monnet * prints a message defined by format *fmt* (of size *fmt_size*) 635ad4a5223SQuentin Monnet * to file *\/sys/kernel/debug/tracing/trace* from DebugFS, if 636ad4a5223SQuentin Monnet * available. It can take up to three additional **u64** 637ad4a5223SQuentin Monnet * arguments (as an eBPF helpers, the total number of arguments is 638ad4a5223SQuentin Monnet * limited to five). 639ad4a5223SQuentin Monnet * 640ad4a5223SQuentin Monnet * Each time the helper is called, it appends a line to the trace. 64155c33dfbSPeter Wu * Lines are discarded while *\/sys/kernel/debug/tracing/trace* is 64255c33dfbSPeter Wu * open, use *\/sys/kernel/debug/tracing/trace_pipe* to avoid this. 643ad4a5223SQuentin Monnet * The format of the trace is customizable, and the exact output 644ad4a5223SQuentin Monnet * one will get depends on the options set in 645ad4a5223SQuentin Monnet * *\/sys/kernel/debug/tracing/trace_options* (see also the 646ad4a5223SQuentin Monnet * *README* file under the same directory). However, it usually 647ad4a5223SQuentin Monnet * defaults to something like: 648ad4a5223SQuentin Monnet * 649ad4a5223SQuentin Monnet * :: 650ad4a5223SQuentin Monnet * 651ad4a5223SQuentin Monnet * telnet-470 [001] .N.. 419421.045894: 0x00000001: <formatted msg> 652ad4a5223SQuentin Monnet * 653ad4a5223SQuentin Monnet * In the above: 654ad4a5223SQuentin Monnet * 655ad4a5223SQuentin Monnet * * ``telnet`` is the name of the current task. 656ad4a5223SQuentin Monnet * * ``470`` is the PID of the current task. 657ad4a5223SQuentin Monnet * * ``001`` is the CPU number on which the task is 658ad4a5223SQuentin Monnet * running. 659ad4a5223SQuentin Monnet * * In ``.N..``, each character refers to a set of 660ad4a5223SQuentin Monnet * options (whether irqs are enabled, scheduling 661ad4a5223SQuentin Monnet * options, whether hard/softirqs are running, level of 662ad4a5223SQuentin Monnet * preempt_disabled respectively). **N** means that 663ad4a5223SQuentin Monnet * **TIF_NEED_RESCHED** and **PREEMPT_NEED_RESCHED** 664ad4a5223SQuentin Monnet * are set. 665ad4a5223SQuentin Monnet * * ``419421.045894`` is a timestamp. 666ad4a5223SQuentin Monnet * * ``0x00000001`` is a fake value used by BPF for the 667ad4a5223SQuentin Monnet * instruction pointer register. 668ad4a5223SQuentin Monnet * * ``<formatted msg>`` is the message formatted with 669ad4a5223SQuentin Monnet * *fmt*. 670ad4a5223SQuentin Monnet * 671ad4a5223SQuentin Monnet * The conversion specifiers supported by *fmt* are similar, but 672ad4a5223SQuentin Monnet * more limited than for printk(). They are **%d**, **%i**, 673ad4a5223SQuentin Monnet * **%u**, **%x**, **%ld**, **%li**, **%lu**, **%lx**, **%lld**, 674ad4a5223SQuentin Monnet * **%lli**, **%llu**, **%llx**, **%p**, **%s**. No modifier (size 675ad4a5223SQuentin Monnet * of field, padding with zeroes, etc.) is available, and the 676ad4a5223SQuentin Monnet * helper will return **-EINVAL** (but print nothing) if it 677ad4a5223SQuentin Monnet * encounters an unknown specifier. 678ad4a5223SQuentin Monnet * 679ad4a5223SQuentin Monnet * Also, note that **bpf_trace_printk**\ () is slow, and should 680ad4a5223SQuentin Monnet * only be used for debugging purposes. For this reason, a notice 681ad4a5223SQuentin Monnet * bloc (spanning several lines) is printed to kernel logs and 682ad4a5223SQuentin Monnet * states that the helper should not be used "for production use" 683ad4a5223SQuentin Monnet * the first time this helper is used (or more precisely, when 684ad4a5223SQuentin Monnet * **trace_printk**\ () buffers are allocated). For passing values 685ad4a5223SQuentin Monnet * to user space, perf events should be preferred. 686ad4a5223SQuentin Monnet * Return 687ad4a5223SQuentin Monnet * The number of bytes written to the buffer, or a negative error 688ad4a5223SQuentin Monnet * in case of failure. 689ad4a5223SQuentin Monnet * 6901fdd08beSQuentin Monnet * u32 bpf_get_prandom_u32(void) 6911fdd08beSQuentin Monnet * Description 6921fdd08beSQuentin Monnet * Get a pseudo-random number. 6931fdd08beSQuentin Monnet * 6941fdd08beSQuentin Monnet * From a security point of view, this helper uses its own 6951fdd08beSQuentin Monnet * pseudo-random internal state, and cannot be used to infer the 6961fdd08beSQuentin Monnet * seed of other random functions in the kernel. However, it is 6971fdd08beSQuentin Monnet * essential to note that the generator used by the helper is not 6981fdd08beSQuentin Monnet * cryptographically secure. 6991fdd08beSQuentin Monnet * Return 7001fdd08beSQuentin Monnet * A random 32-bit unsigned value. 7011fdd08beSQuentin Monnet * 7021fdd08beSQuentin Monnet * u32 bpf_get_smp_processor_id(void) 7031fdd08beSQuentin Monnet * Description 7041fdd08beSQuentin Monnet * Get the SMP (symmetric multiprocessing) processor id. Note that 7051fdd08beSQuentin Monnet * all programs run with preemption disabled, which means that the 7061fdd08beSQuentin Monnet * SMP processor id is stable during all the execution of the 7071fdd08beSQuentin Monnet * program. 7081fdd08beSQuentin Monnet * Return 7091fdd08beSQuentin Monnet * The SMP id of the processor running the program. 7101fdd08beSQuentin Monnet * 711ad4a5223SQuentin Monnet * int bpf_skb_store_bytes(struct sk_buff *skb, u32 offset, const void *from, u32 len, u64 flags) 712ad4a5223SQuentin Monnet * Description 713ad4a5223SQuentin Monnet * Store *len* bytes from address *from* into the packet 714ad4a5223SQuentin Monnet * associated to *skb*, at *offset*. *flags* are a combination of 715ad4a5223SQuentin Monnet * **BPF_F_RECOMPUTE_CSUM** (automatically recompute the 716ad4a5223SQuentin Monnet * checksum for the packet after storing the bytes) and 717ad4a5223SQuentin Monnet * **BPF_F_INVALIDATE_HASH** (set *skb*\ **->hash**, *skb*\ 718ad4a5223SQuentin Monnet * **->swhash** and *skb*\ **->l4hash** to 0). 719ad4a5223SQuentin Monnet * 72032e7dc28SQuentin Monnet * A call to this helper is susceptible to change the underlying 721ad4a5223SQuentin Monnet * packet buffer. Therefore, at load time, all checks on pointers 722ad4a5223SQuentin Monnet * previously done by the verifier are invalidated and must be 723ad4a5223SQuentin Monnet * performed again, if the helper is used in combination with 724ad4a5223SQuentin Monnet * direct packet access. 725ad4a5223SQuentin Monnet * Return 726ad4a5223SQuentin Monnet * 0 on success, or a negative error in case of failure. 727ad4a5223SQuentin Monnet * 728ad4a5223SQuentin Monnet * int bpf_l3_csum_replace(struct sk_buff *skb, u32 offset, u64 from, u64 to, u64 size) 729ad4a5223SQuentin Monnet * Description 730ad4a5223SQuentin Monnet * Recompute the layer 3 (e.g. IP) checksum for the packet 731ad4a5223SQuentin Monnet * associated to *skb*. Computation is incremental, so the helper 732ad4a5223SQuentin Monnet * must know the former value of the header field that was 733ad4a5223SQuentin Monnet * modified (*from*), the new value of this field (*to*), and the 734ad4a5223SQuentin Monnet * number of bytes (2 or 4) for this field, stored in *size*. 735ad4a5223SQuentin Monnet * Alternatively, it is possible to store the difference between 736ad4a5223SQuentin Monnet * the previous and the new values of the header field in *to*, by 737ad4a5223SQuentin Monnet * setting *from* and *size* to 0. For both methods, *offset* 738ad4a5223SQuentin Monnet * indicates the location of the IP checksum within the packet. 739ad4a5223SQuentin Monnet * 740ad4a5223SQuentin Monnet * This helper works in combination with **bpf_csum_diff**\ (), 741ad4a5223SQuentin Monnet * which does not update the checksum in-place, but offers more 742ad4a5223SQuentin Monnet * flexibility and can handle sizes larger than 2 or 4 for the 743ad4a5223SQuentin Monnet * checksum to update. 744ad4a5223SQuentin Monnet * 74532e7dc28SQuentin Monnet * A call to this helper is susceptible to change the underlying 746ad4a5223SQuentin Monnet * packet buffer. Therefore, at load time, all checks on pointers 747ad4a5223SQuentin Monnet * previously done by the verifier are invalidated and must be 748ad4a5223SQuentin Monnet * performed again, if the helper is used in combination with 749ad4a5223SQuentin Monnet * direct packet access. 750ad4a5223SQuentin Monnet * Return 751ad4a5223SQuentin Monnet * 0 on success, or a negative error in case of failure. 752ad4a5223SQuentin Monnet * 753ad4a5223SQuentin Monnet * int bpf_l4_csum_replace(struct sk_buff *skb, u32 offset, u64 from, u64 to, u64 flags) 754ad4a5223SQuentin Monnet * Description 755ad4a5223SQuentin Monnet * Recompute the layer 4 (e.g. TCP, UDP or ICMP) checksum for the 756ad4a5223SQuentin Monnet * packet associated to *skb*. Computation is incremental, so the 757ad4a5223SQuentin Monnet * helper must know the former value of the header field that was 758ad4a5223SQuentin Monnet * modified (*from*), the new value of this field (*to*), and the 759ad4a5223SQuentin Monnet * number of bytes (2 or 4) for this field, stored on the lowest 760ad4a5223SQuentin Monnet * four bits of *flags*. Alternatively, it is possible to store 761ad4a5223SQuentin Monnet * the difference between the previous and the new values of the 762ad4a5223SQuentin Monnet * header field in *to*, by setting *from* and the four lowest 763ad4a5223SQuentin Monnet * bits of *flags* to 0. For both methods, *offset* indicates the 764ad4a5223SQuentin Monnet * location of the IP checksum within the packet. In addition to 765ad4a5223SQuentin Monnet * the size of the field, *flags* can be added (bitwise OR) actual 766ad4a5223SQuentin Monnet * flags. With **BPF_F_MARK_MANGLED_0**, a null checksum is left 767ad4a5223SQuentin Monnet * untouched (unless **BPF_F_MARK_ENFORCE** is added as well), and 768ad4a5223SQuentin Monnet * for updates resulting in a null checksum the value is set to 769ad4a5223SQuentin Monnet * **CSUM_MANGLED_0** instead. Flag **BPF_F_PSEUDO_HDR** indicates 770ad4a5223SQuentin Monnet * the checksum is to be computed against a pseudo-header. 771ad4a5223SQuentin Monnet * 772ad4a5223SQuentin Monnet * This helper works in combination with **bpf_csum_diff**\ (), 773ad4a5223SQuentin Monnet * which does not update the checksum in-place, but offers more 774ad4a5223SQuentin Monnet * flexibility and can handle sizes larger than 2 or 4 for the 775ad4a5223SQuentin Monnet * checksum to update. 776ad4a5223SQuentin Monnet * 77732e7dc28SQuentin Monnet * A call to this helper is susceptible to change the underlying 778ad4a5223SQuentin Monnet * packet buffer. Therefore, at load time, all checks on pointers 779ad4a5223SQuentin Monnet * previously done by the verifier are invalidated and must be 780ad4a5223SQuentin Monnet * performed again, if the helper is used in combination with 781ad4a5223SQuentin Monnet * direct packet access. 782ad4a5223SQuentin Monnet * Return 783ad4a5223SQuentin Monnet * 0 on success, or a negative error in case of failure. 784ad4a5223SQuentin Monnet * 785ad4a5223SQuentin Monnet * int bpf_tail_call(void *ctx, struct bpf_map *prog_array_map, u32 index) 786ad4a5223SQuentin Monnet * Description 787ad4a5223SQuentin Monnet * This special helper is used to trigger a "tail call", or in 788ad4a5223SQuentin Monnet * other words, to jump into another eBPF program. The same stack 789ad4a5223SQuentin Monnet * frame is used (but values on stack and in registers for the 790ad4a5223SQuentin Monnet * caller are not accessible to the callee). This mechanism allows 791ad4a5223SQuentin Monnet * for program chaining, either for raising the maximum number of 792ad4a5223SQuentin Monnet * available eBPF instructions, or to execute given programs in 793ad4a5223SQuentin Monnet * conditional blocks. For security reasons, there is an upper 794ad4a5223SQuentin Monnet * limit to the number of successive tail calls that can be 795ad4a5223SQuentin Monnet * performed. 796ad4a5223SQuentin Monnet * 797ad4a5223SQuentin Monnet * Upon call of this helper, the program attempts to jump into a 798ad4a5223SQuentin Monnet * program referenced at index *index* in *prog_array_map*, a 799ad4a5223SQuentin Monnet * special map of type **BPF_MAP_TYPE_PROG_ARRAY**, and passes 800ad4a5223SQuentin Monnet * *ctx*, a pointer to the context. 801ad4a5223SQuentin Monnet * 802ad4a5223SQuentin Monnet * If the call succeeds, the kernel immediately runs the first 803ad4a5223SQuentin Monnet * instruction of the new program. This is not a function call, 804ad4a5223SQuentin Monnet * and it never returns to the previous program. If the call 805ad4a5223SQuentin Monnet * fails, then the helper has no effect, and the caller continues 806ad4a5223SQuentin Monnet * to run its subsequent instructions. A call can fail if the 807ad4a5223SQuentin Monnet * destination program for the jump does not exist (i.e. *index* 808ad4a5223SQuentin Monnet * is superior to the number of entries in *prog_array_map*), or 809ad4a5223SQuentin Monnet * if the maximum number of tail calls has been reached for this 810ad4a5223SQuentin Monnet * chain of programs. This limit is defined in the kernel by the 811ad4a5223SQuentin Monnet * macro **MAX_TAIL_CALL_CNT** (not accessible to user space), 812ad4a5223SQuentin Monnet * which is currently set to 32. 813ad4a5223SQuentin Monnet * Return 814ad4a5223SQuentin Monnet * 0 on success, or a negative error in case of failure. 815ad4a5223SQuentin Monnet * 816ad4a5223SQuentin Monnet * int bpf_clone_redirect(struct sk_buff *skb, u32 ifindex, u64 flags) 817ad4a5223SQuentin Monnet * Description 818ad4a5223SQuentin Monnet * Clone and redirect the packet associated to *skb* to another 819ad4a5223SQuentin Monnet * net device of index *ifindex*. Both ingress and egress 820ad4a5223SQuentin Monnet * interfaces can be used for redirection. The **BPF_F_INGRESS** 821ad4a5223SQuentin Monnet * value in *flags* is used to make the distinction (ingress path 822ad4a5223SQuentin Monnet * is selected if the flag is present, egress path otherwise). 823ad4a5223SQuentin Monnet * This is the only flag supported for now. 824ad4a5223SQuentin Monnet * 825ad4a5223SQuentin Monnet * In comparison with **bpf_redirect**\ () helper, 826ad4a5223SQuentin Monnet * **bpf_clone_redirect**\ () has the associated cost of 827ad4a5223SQuentin Monnet * duplicating the packet buffer, but this can be executed out of 828ad4a5223SQuentin Monnet * the eBPF program. Conversely, **bpf_redirect**\ () is more 829ad4a5223SQuentin Monnet * efficient, but it is handled through an action code where the 830ad4a5223SQuentin Monnet * redirection happens only after the eBPF program has returned. 831ad4a5223SQuentin Monnet * 83232e7dc28SQuentin Monnet * A call to this helper is susceptible to change the underlying 833ad4a5223SQuentin Monnet * packet buffer. Therefore, at load time, all checks on pointers 834ad4a5223SQuentin Monnet * previously done by the verifier are invalidated and must be 835ad4a5223SQuentin Monnet * performed again, if the helper is used in combination with 836ad4a5223SQuentin Monnet * direct packet access. 837ad4a5223SQuentin Monnet * Return 838ad4a5223SQuentin Monnet * 0 on success, or a negative error in case of failure. 839c456dec4SQuentin Monnet * 840c456dec4SQuentin Monnet * u64 bpf_get_current_pid_tgid(void) 841c456dec4SQuentin Monnet * Return 842c456dec4SQuentin Monnet * A 64-bit integer containing the current tgid and pid, and 843c456dec4SQuentin Monnet * created as such: 844c456dec4SQuentin Monnet * *current_task*\ **->tgid << 32 \|** 845c456dec4SQuentin Monnet * *current_task*\ **->pid**. 846c456dec4SQuentin Monnet * 847c456dec4SQuentin Monnet * u64 bpf_get_current_uid_gid(void) 848c456dec4SQuentin Monnet * Return 849c456dec4SQuentin Monnet * A 64-bit integer containing the current GID and UID, and 850c456dec4SQuentin Monnet * created as such: *current_gid* **<< 32 \|** *current_uid*. 851c456dec4SQuentin Monnet * 8525f0e5412SAndrii Nakryiko * int bpf_get_current_comm(void *buf, u32 size_of_buf) 853c456dec4SQuentin Monnet * Description 854c456dec4SQuentin Monnet * Copy the **comm** attribute of the current task into *buf* of 855c456dec4SQuentin Monnet * *size_of_buf*. The **comm** attribute contains the name of 856c456dec4SQuentin Monnet * the executable (excluding the path) for the current task. The 857c456dec4SQuentin Monnet * *size_of_buf* must be strictly positive. On success, the 858c456dec4SQuentin Monnet * helper makes sure that the *buf* is NUL-terminated. On failure, 859c456dec4SQuentin Monnet * it is filled with zeroes. 860c456dec4SQuentin Monnet * Return 861c456dec4SQuentin Monnet * 0 on success, or a negative error in case of failure. 862c456dec4SQuentin Monnet * 8631fdd08beSQuentin Monnet * u32 bpf_get_cgroup_classid(struct sk_buff *skb) 8641fdd08beSQuentin Monnet * Description 8651fdd08beSQuentin Monnet * Retrieve the classid for the current task, i.e. for the net_cls 8661fdd08beSQuentin Monnet * cgroup to which *skb* belongs. 8671fdd08beSQuentin Monnet * 8681fdd08beSQuentin Monnet * This helper can be used on TC egress path, but not on ingress. 8691fdd08beSQuentin Monnet * 8701fdd08beSQuentin Monnet * The net_cls cgroup provides an interface to tag network packets 8711fdd08beSQuentin Monnet * based on a user-provided identifier for all traffic coming from 8721fdd08beSQuentin Monnet * the tasks belonging to the related cgroup. See also the related 8731fdd08beSQuentin Monnet * kernel documentation, available from the Linux sources in file 874da82c92fSMauro Carvalho Chehab * *Documentation/admin-guide/cgroup-v1/net_cls.rst*. 8751fdd08beSQuentin Monnet * 8761fdd08beSQuentin Monnet * The Linux kernel has two versions for cgroups: there are 8771fdd08beSQuentin Monnet * cgroups v1 and cgroups v2. Both are available to users, who can 8781fdd08beSQuentin Monnet * use a mixture of them, but note that the net_cls cgroup is for 8791fdd08beSQuentin Monnet * cgroup v1 only. This makes it incompatible with BPF programs 8801fdd08beSQuentin Monnet * run on cgroups, which is a cgroup-v2-only feature (a socket can 8811fdd08beSQuentin Monnet * only hold data for one version of cgroups at a time). 8821fdd08beSQuentin Monnet * 8831fdd08beSQuentin Monnet * This helper is only available is the kernel was compiled with 8841fdd08beSQuentin Monnet * the **CONFIG_CGROUP_NET_CLASSID** configuration option set to 8851fdd08beSQuentin Monnet * "**y**" or to "**m**". 8861fdd08beSQuentin Monnet * Return 8871fdd08beSQuentin Monnet * The classid, or 0 for the default unconfigured classid. 8881fdd08beSQuentin Monnet * 889c456dec4SQuentin Monnet * int bpf_skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci) 890c456dec4SQuentin Monnet * Description 891c456dec4SQuentin Monnet * Push a *vlan_tci* (VLAN tag control information) of protocol 892c456dec4SQuentin Monnet * *vlan_proto* to the packet associated to *skb*, then update 893c456dec4SQuentin Monnet * the checksum. Note that if *vlan_proto* is different from 894c456dec4SQuentin Monnet * **ETH_P_8021Q** and **ETH_P_8021AD**, it is considered to 895c456dec4SQuentin Monnet * be **ETH_P_8021Q**. 896c456dec4SQuentin Monnet * 89732e7dc28SQuentin Monnet * A call to this helper is susceptible to change the underlying 898c456dec4SQuentin Monnet * packet buffer. Therefore, at load time, all checks on pointers 899c456dec4SQuentin Monnet * previously done by the verifier are invalidated and must be 900c456dec4SQuentin Monnet * performed again, if the helper is used in combination with 901c456dec4SQuentin Monnet * direct packet access. 902c456dec4SQuentin Monnet * Return 903c456dec4SQuentin Monnet * 0 on success, or a negative error in case of failure. 904c456dec4SQuentin Monnet * 905c456dec4SQuentin Monnet * int bpf_skb_vlan_pop(struct sk_buff *skb) 906c456dec4SQuentin Monnet * Description 907c456dec4SQuentin Monnet * Pop a VLAN header from the packet associated to *skb*. 908c456dec4SQuentin Monnet * 90932e7dc28SQuentin Monnet * A call to this helper is susceptible to change the underlying 910c456dec4SQuentin Monnet * packet buffer. Therefore, at load time, all checks on pointers 911c456dec4SQuentin Monnet * previously done by the verifier are invalidated and must be 912c456dec4SQuentin Monnet * performed again, if the helper is used in combination with 913c456dec4SQuentin Monnet * direct packet access. 914c456dec4SQuentin Monnet * Return 915c456dec4SQuentin Monnet * 0 on success, or a negative error in case of failure. 916c456dec4SQuentin Monnet * 917c456dec4SQuentin Monnet * int bpf_skb_get_tunnel_key(struct sk_buff *skb, struct bpf_tunnel_key *key, u32 size, u64 flags) 918c456dec4SQuentin Monnet * Description 919c456dec4SQuentin Monnet * Get tunnel metadata. This helper takes a pointer *key* to an 920c456dec4SQuentin Monnet * empty **struct bpf_tunnel_key** of **size**, that will be 921c456dec4SQuentin Monnet * filled with tunnel metadata for the packet associated to *skb*. 922c456dec4SQuentin Monnet * The *flags* can be set to **BPF_F_TUNINFO_IPV6**, which 923c456dec4SQuentin Monnet * indicates that the tunnel is based on IPv6 protocol instead of 924c456dec4SQuentin Monnet * IPv4. 925c456dec4SQuentin Monnet * 926c456dec4SQuentin Monnet * The **struct bpf_tunnel_key** is an object that generalizes the 927c456dec4SQuentin Monnet * principal parameters used by various tunneling protocols into a 928c456dec4SQuentin Monnet * single struct. This way, it can be used to easily make a 929c456dec4SQuentin Monnet * decision based on the contents of the encapsulation header, 930c456dec4SQuentin Monnet * "summarized" in this struct. In particular, it holds the IP 931c456dec4SQuentin Monnet * address of the remote end (IPv4 or IPv6, depending on the case) 932c456dec4SQuentin Monnet * in *key*\ **->remote_ipv4** or *key*\ **->remote_ipv6**. Also, 933c456dec4SQuentin Monnet * this struct exposes the *key*\ **->tunnel_id**, which is 934c456dec4SQuentin Monnet * generally mapped to a VNI (Virtual Network Identifier), making 935c456dec4SQuentin Monnet * it programmable together with the **bpf_skb_set_tunnel_key**\ 936c456dec4SQuentin Monnet * () helper. 937c456dec4SQuentin Monnet * 938c456dec4SQuentin Monnet * Let's imagine that the following code is part of a program 939c456dec4SQuentin Monnet * attached to the TC ingress interface, on one end of a GRE 940c456dec4SQuentin Monnet * tunnel, and is supposed to filter out all messages coming from 941c456dec4SQuentin Monnet * remote ends with IPv4 address other than 10.0.0.1: 942c456dec4SQuentin Monnet * 943c456dec4SQuentin Monnet * :: 944c456dec4SQuentin Monnet * 945c456dec4SQuentin Monnet * int ret; 946c456dec4SQuentin Monnet * struct bpf_tunnel_key key = {}; 947c456dec4SQuentin Monnet * 948c456dec4SQuentin Monnet * ret = bpf_skb_get_tunnel_key(skb, &key, sizeof(key), 0); 949c456dec4SQuentin Monnet * if (ret < 0) 950c456dec4SQuentin Monnet * return TC_ACT_SHOT; // drop packet 951c456dec4SQuentin Monnet * 952c456dec4SQuentin Monnet * if (key.remote_ipv4 != 0x0a000001) 953c456dec4SQuentin Monnet * return TC_ACT_SHOT; // drop packet 954c456dec4SQuentin Monnet * 955c456dec4SQuentin Monnet * return TC_ACT_OK; // accept packet 956c456dec4SQuentin Monnet * 957c456dec4SQuentin Monnet * This interface can also be used with all encapsulation devices 958c456dec4SQuentin Monnet * that can operate in "collect metadata" mode: instead of having 959c456dec4SQuentin Monnet * one network device per specific configuration, the "collect 960c456dec4SQuentin Monnet * metadata" mode only requires a single device where the 961c456dec4SQuentin Monnet * configuration can be extracted from this helper. 962c456dec4SQuentin Monnet * 963c456dec4SQuentin Monnet * This can be used together with various tunnels such as VXLan, 964c456dec4SQuentin Monnet * Geneve, GRE or IP in IP (IPIP). 965c456dec4SQuentin Monnet * Return 966c456dec4SQuentin Monnet * 0 on success, or a negative error in case of failure. 967c456dec4SQuentin Monnet * 968c456dec4SQuentin Monnet * int bpf_skb_set_tunnel_key(struct sk_buff *skb, struct bpf_tunnel_key *key, u32 size, u64 flags) 969c456dec4SQuentin Monnet * Description 970c456dec4SQuentin Monnet * Populate tunnel metadata for packet associated to *skb.* The 971c456dec4SQuentin Monnet * tunnel metadata is set to the contents of *key*, of *size*. The 972c456dec4SQuentin Monnet * *flags* can be set to a combination of the following values: 973c456dec4SQuentin Monnet * 974c456dec4SQuentin Monnet * **BPF_F_TUNINFO_IPV6** 975c456dec4SQuentin Monnet * Indicate that the tunnel is based on IPv6 protocol 976c456dec4SQuentin Monnet * instead of IPv4. 977c456dec4SQuentin Monnet * **BPF_F_ZERO_CSUM_TX** 978c456dec4SQuentin Monnet * For IPv4 packets, add a flag to tunnel metadata 979c456dec4SQuentin Monnet * indicating that checksum computation should be skipped 980c456dec4SQuentin Monnet * and checksum set to zeroes. 981c456dec4SQuentin Monnet * **BPF_F_DONT_FRAGMENT** 982c456dec4SQuentin Monnet * Add a flag to tunnel metadata indicating that the 983c456dec4SQuentin Monnet * packet should not be fragmented. 984c456dec4SQuentin Monnet * **BPF_F_SEQ_NUMBER** 985c456dec4SQuentin Monnet * Add a flag to tunnel metadata indicating that a 986c456dec4SQuentin Monnet * sequence number should be added to tunnel header before 987c456dec4SQuentin Monnet * sending the packet. This flag was added for GRE 988c456dec4SQuentin Monnet * encapsulation, but might be used with other protocols 989c456dec4SQuentin Monnet * as well in the future. 990c456dec4SQuentin Monnet * 991c456dec4SQuentin Monnet * Here is a typical usage on the transmit path: 992c456dec4SQuentin Monnet * 993c456dec4SQuentin Monnet * :: 994c456dec4SQuentin Monnet * 995c456dec4SQuentin Monnet * struct bpf_tunnel_key key; 996c456dec4SQuentin Monnet * populate key ... 997c456dec4SQuentin Monnet * bpf_skb_set_tunnel_key(skb, &key, sizeof(key), 0); 998c456dec4SQuentin Monnet * bpf_clone_redirect(skb, vxlan_dev_ifindex, 0); 999c456dec4SQuentin Monnet * 1000c456dec4SQuentin Monnet * See also the description of the **bpf_skb_get_tunnel_key**\ () 1001c456dec4SQuentin Monnet * helper for additional information. 1002c456dec4SQuentin Monnet * Return 1003c456dec4SQuentin Monnet * 0 on success, or a negative error in case of failure. 1004c456dec4SQuentin Monnet * 1005c6b5fb86SQuentin Monnet * u64 bpf_perf_event_read(struct bpf_map *map, u64 flags) 1006c6b5fb86SQuentin Monnet * Description 1007c6b5fb86SQuentin Monnet * Read the value of a perf event counter. This helper relies on a 1008c6b5fb86SQuentin Monnet * *map* of type **BPF_MAP_TYPE_PERF_EVENT_ARRAY**. The nature of 1009c6b5fb86SQuentin Monnet * the perf event counter is selected when *map* is updated with 1010c6b5fb86SQuentin Monnet * perf event file descriptors. The *map* is an array whose size 1011c6b5fb86SQuentin Monnet * is the number of available CPUs, and each cell contains a value 1012c6b5fb86SQuentin Monnet * relative to one CPU. The value to retrieve is indicated by 1013c6b5fb86SQuentin Monnet * *flags*, that contains the index of the CPU to look up, masked 1014c6b5fb86SQuentin Monnet * with **BPF_F_INDEX_MASK**. Alternatively, *flags* can be set to 1015c6b5fb86SQuentin Monnet * **BPF_F_CURRENT_CPU** to indicate that the value for the 1016c6b5fb86SQuentin Monnet * current CPU should be retrieved. 1017c6b5fb86SQuentin Monnet * 1018c6b5fb86SQuentin Monnet * Note that before Linux 4.13, only hardware perf event can be 1019c6b5fb86SQuentin Monnet * retrieved. 1020c6b5fb86SQuentin Monnet * 1021c6b5fb86SQuentin Monnet * Also, be aware that the newer helper 1022c6b5fb86SQuentin Monnet * **bpf_perf_event_read_value**\ () is recommended over 10233bd5a09bSQuentin Monnet * **bpf_perf_event_read**\ () in general. The latter has some ABI 1024c6b5fb86SQuentin Monnet * quirks where error and counter value are used as a return code 1025c6b5fb86SQuentin Monnet * (which is wrong to do since ranges may overlap). This issue is 10263bd5a09bSQuentin Monnet * fixed with **bpf_perf_event_read_value**\ (), which at the same 10273bd5a09bSQuentin Monnet * time provides more features over the **bpf_perf_event_read**\ 10283bd5a09bSQuentin Monnet * () interface. Please refer to the description of 1029c6b5fb86SQuentin Monnet * **bpf_perf_event_read_value**\ () for details. 1030c6b5fb86SQuentin Monnet * Return 1031c6b5fb86SQuentin Monnet * The value of the perf event counter read from the map, or a 1032c6b5fb86SQuentin Monnet * negative error code in case of failure. 1033c6b5fb86SQuentin Monnet * 1034c456dec4SQuentin Monnet * int bpf_redirect(u32 ifindex, u64 flags) 1035c456dec4SQuentin Monnet * Description 1036c456dec4SQuentin Monnet * Redirect the packet to another net device of index *ifindex*. 1037c456dec4SQuentin Monnet * This helper is somewhat similar to **bpf_clone_redirect**\ 1038c456dec4SQuentin Monnet * (), except that the packet is not cloned, which provides 1039c456dec4SQuentin Monnet * increased performance. 1040c456dec4SQuentin Monnet * 1041c456dec4SQuentin Monnet * Except for XDP, both ingress and egress interfaces can be used 1042c456dec4SQuentin Monnet * for redirection. The **BPF_F_INGRESS** value in *flags* is used 1043c456dec4SQuentin Monnet * to make the distinction (ingress path is selected if the flag 1044c456dec4SQuentin Monnet * is present, egress path otherwise). Currently, XDP only 1045c456dec4SQuentin Monnet * supports redirection to the egress interface, and accepts no 1046c456dec4SQuentin Monnet * flag at all. 1047c456dec4SQuentin Monnet * 1048c456dec4SQuentin Monnet * The same effect can be attained with the more generic 1049c456dec4SQuentin Monnet * **bpf_redirect_map**\ (), which requires specific maps to be 1050c456dec4SQuentin Monnet * used but offers better performance. 1051c456dec4SQuentin Monnet * Return 1052c456dec4SQuentin Monnet * For XDP, the helper returns **XDP_REDIRECT** on success or 1053c456dec4SQuentin Monnet * **XDP_ABORTED** on error. For other program types, the values 1054c456dec4SQuentin Monnet * are **TC_ACT_REDIRECT** on success or **TC_ACT_SHOT** on 1055c456dec4SQuentin Monnet * error. 1056c456dec4SQuentin Monnet * 10571fdd08beSQuentin Monnet * u32 bpf_get_route_realm(struct sk_buff *skb) 10581fdd08beSQuentin Monnet * Description 10591fdd08beSQuentin Monnet * Retrieve the realm or the route, that is to say the 10601fdd08beSQuentin Monnet * **tclassid** field of the destination for the *skb*. The 10611fdd08beSQuentin Monnet * indentifier retrieved is a user-provided tag, similar to the 10621fdd08beSQuentin Monnet * one used with the net_cls cgroup (see description for 10631fdd08beSQuentin Monnet * **bpf_get_cgroup_classid**\ () helper), but here this tag is 10641fdd08beSQuentin Monnet * held by a route (a destination entry), not by a task. 10651fdd08beSQuentin Monnet * 10661fdd08beSQuentin Monnet * Retrieving this identifier works with the clsact TC egress hook 10671fdd08beSQuentin Monnet * (see also **tc-bpf(8)**), or alternatively on conventional 10681fdd08beSQuentin Monnet * classful egress qdiscs, but not on TC ingress path. In case of 10691fdd08beSQuentin Monnet * clsact TC egress hook, this has the advantage that, internally, 10701fdd08beSQuentin Monnet * the destination entry has not been dropped yet in the transmit 10711fdd08beSQuentin Monnet * path. Therefore, the destination entry does not need to be 10721fdd08beSQuentin Monnet * artificially held via **netif_keep_dst**\ () for a classful 10731fdd08beSQuentin Monnet * qdisc until the *skb* is freed. 10741fdd08beSQuentin Monnet * 10751fdd08beSQuentin Monnet * This helper is available only if the kernel was compiled with 10761fdd08beSQuentin Monnet * **CONFIG_IP_ROUTE_CLASSID** configuration option. 10771fdd08beSQuentin Monnet * Return 10781fdd08beSQuentin Monnet * The realm of the route for the packet associated to *skb*, or 0 10791fdd08beSQuentin Monnet * if none was found. 10801fdd08beSQuentin Monnet * 10815f0e5412SAndrii Nakryiko * int bpf_perf_event_output(void *ctx, struct bpf_map *map, u64 flags, void *data, u64 size) 1082c456dec4SQuentin Monnet * Description 1083c456dec4SQuentin Monnet * Write raw *data* blob into a special BPF perf event held by 1084c456dec4SQuentin Monnet * *map* of type **BPF_MAP_TYPE_PERF_EVENT_ARRAY**. This perf 1085c456dec4SQuentin Monnet * event must have the following attributes: **PERF_SAMPLE_RAW** 1086c456dec4SQuentin Monnet * as **sample_type**, **PERF_TYPE_SOFTWARE** as **type**, and 1087c456dec4SQuentin Monnet * **PERF_COUNT_SW_BPF_OUTPUT** as **config**. 1088c456dec4SQuentin Monnet * 1089c456dec4SQuentin Monnet * The *flags* are used to indicate the index in *map* for which 1090c456dec4SQuentin Monnet * the value must be put, masked with **BPF_F_INDEX_MASK**. 1091c456dec4SQuentin Monnet * Alternatively, *flags* can be set to **BPF_F_CURRENT_CPU** 1092c456dec4SQuentin Monnet * to indicate that the index of the current CPU core should be 1093c456dec4SQuentin Monnet * used. 1094c456dec4SQuentin Monnet * 1095c456dec4SQuentin Monnet * The value to write, of *size*, is passed through eBPF stack and 1096c456dec4SQuentin Monnet * pointed by *data*. 1097c456dec4SQuentin Monnet * 1098c456dec4SQuentin Monnet * The context of the program *ctx* needs also be passed to the 1099c456dec4SQuentin Monnet * helper. 1100c456dec4SQuentin Monnet * 1101c456dec4SQuentin Monnet * On user space, a program willing to read the values needs to 1102c456dec4SQuentin Monnet * call **perf_event_open**\ () on the perf event (either for 1103c456dec4SQuentin Monnet * one or for all CPUs) and to store the file descriptor into the 1104c456dec4SQuentin Monnet * *map*. This must be done before the eBPF program can send data 1105c456dec4SQuentin Monnet * into it. An example is available in file 1106c456dec4SQuentin Monnet * *samples/bpf/trace_output_user.c* in the Linux kernel source 1107c456dec4SQuentin Monnet * tree (the eBPF program counterpart is in 1108c456dec4SQuentin Monnet * *samples/bpf/trace_output_kern.c*). 1109c456dec4SQuentin Monnet * 1110c456dec4SQuentin Monnet * **bpf_perf_event_output**\ () achieves better performance 1111c456dec4SQuentin Monnet * than **bpf_trace_printk**\ () for sharing data with user 1112c456dec4SQuentin Monnet * space, and is much better suitable for streaming data from eBPF 1113c456dec4SQuentin Monnet * programs. 1114c456dec4SQuentin Monnet * 1115c456dec4SQuentin Monnet * Note that this helper is not restricted to tracing use cases 1116c456dec4SQuentin Monnet * and can be used with programs attached to TC or XDP as well, 1117c456dec4SQuentin Monnet * where it allows for passing data to user space listeners. Data 1118c456dec4SQuentin Monnet * can be: 1119c456dec4SQuentin Monnet * 1120c456dec4SQuentin Monnet * * Only custom structs, 1121c456dec4SQuentin Monnet * * Only the packet payload, or 1122c456dec4SQuentin Monnet * * A combination of both. 1123c456dec4SQuentin Monnet * Return 1124c456dec4SQuentin Monnet * 0 on success, or a negative error in case of failure. 1125c456dec4SQuentin Monnet * 11265f0e5412SAndrii Nakryiko * int bpf_skb_load_bytes(const void *skb, u32 offset, void *to, u32 len) 11271fdd08beSQuentin Monnet * Description 11281fdd08beSQuentin Monnet * This helper was provided as an easy way to load data from a 11291fdd08beSQuentin Monnet * packet. It can be used to load *len* bytes from *offset* from 11301fdd08beSQuentin Monnet * the packet associated to *skb*, into the buffer pointed by 11311fdd08beSQuentin Monnet * *to*. 11321fdd08beSQuentin Monnet * 11331fdd08beSQuentin Monnet * Since Linux 4.7, usage of this helper has mostly been replaced 11341fdd08beSQuentin Monnet * by "direct packet access", enabling packet data to be 11351fdd08beSQuentin Monnet * manipulated with *skb*\ **->data** and *skb*\ **->data_end** 11361fdd08beSQuentin Monnet * pointing respectively to the first byte of packet data and to 11371fdd08beSQuentin Monnet * the byte after the last byte of packet data. However, it 11381fdd08beSQuentin Monnet * remains useful if one wishes to read large quantities of data 11391fdd08beSQuentin Monnet * at once from a packet into the eBPF stack. 11401fdd08beSQuentin Monnet * Return 11411fdd08beSQuentin Monnet * 0 on success, or a negative error in case of failure. 11421fdd08beSQuentin Monnet * 11435f0e5412SAndrii Nakryiko * int bpf_get_stackid(void *ctx, struct bpf_map *map, u64 flags) 1144c456dec4SQuentin Monnet * Description 1145c456dec4SQuentin Monnet * Walk a user or a kernel stack and return its id. To achieve 1146c456dec4SQuentin Monnet * this, the helper needs *ctx*, which is a pointer to the context 1147c456dec4SQuentin Monnet * on which the tracing program is executed, and a pointer to a 1148c456dec4SQuentin Monnet * *map* of type **BPF_MAP_TYPE_STACK_TRACE**. 1149c456dec4SQuentin Monnet * 1150c456dec4SQuentin Monnet * The last argument, *flags*, holds the number of stack frames to 1151c456dec4SQuentin Monnet * skip (from 0 to 255), masked with 1152c456dec4SQuentin Monnet * **BPF_F_SKIP_FIELD_MASK**. The next bits can be used to set 1153c456dec4SQuentin Monnet * a combination of the following flags: 1154c456dec4SQuentin Monnet * 1155c456dec4SQuentin Monnet * **BPF_F_USER_STACK** 1156c456dec4SQuentin Monnet * Collect a user space stack instead of a kernel stack. 1157c456dec4SQuentin Monnet * **BPF_F_FAST_STACK_CMP** 1158c456dec4SQuentin Monnet * Compare stacks by hash only. 1159c456dec4SQuentin Monnet * **BPF_F_REUSE_STACKID** 1160c456dec4SQuentin Monnet * If two different stacks hash into the same *stackid*, 1161c456dec4SQuentin Monnet * discard the old one. 1162c456dec4SQuentin Monnet * 1163c456dec4SQuentin Monnet * The stack id retrieved is a 32 bit long integer handle which 1164c456dec4SQuentin Monnet * can be further combined with other data (including other stack 1165c456dec4SQuentin Monnet * ids) and used as a key into maps. This can be useful for 1166c456dec4SQuentin Monnet * generating a variety of graphs (such as flame graphs or off-cpu 1167c456dec4SQuentin Monnet * graphs). 1168c456dec4SQuentin Monnet * 1169c456dec4SQuentin Monnet * For walking a stack, this helper is an improvement over 1170c456dec4SQuentin Monnet * **bpf_probe_read**\ (), which can be used with unrolled loops 1171c456dec4SQuentin Monnet * but is not efficient and consumes a lot of eBPF instructions. 1172c456dec4SQuentin Monnet * Instead, **bpf_get_stackid**\ () can collect up to 1173c456dec4SQuentin Monnet * **PERF_MAX_STACK_DEPTH** both kernel and user frames. Note that 1174c456dec4SQuentin Monnet * this limit can be controlled with the **sysctl** program, and 1175c456dec4SQuentin Monnet * that it should be manually increased in order to profile long 1176c456dec4SQuentin Monnet * user stacks (such as stacks for Java programs). To do so, use: 1177c456dec4SQuentin Monnet * 1178c456dec4SQuentin Monnet * :: 1179c456dec4SQuentin Monnet * 1180c456dec4SQuentin Monnet * # sysctl kernel.perf_event_max_stack=<new value> 1181c456dec4SQuentin Monnet * Return 1182c456dec4SQuentin Monnet * The positive or null stack id on success, or a negative error 1183c456dec4SQuentin Monnet * in case of failure. 1184c456dec4SQuentin Monnet * 11851fdd08beSQuentin Monnet * s64 bpf_csum_diff(__be32 *from, u32 from_size, __be32 *to, u32 to_size, __wsum seed) 11861fdd08beSQuentin Monnet * Description 11871fdd08beSQuentin Monnet * Compute a checksum difference, from the raw buffer pointed by 11881fdd08beSQuentin Monnet * *from*, of length *from_size* (that must be a multiple of 4), 11891fdd08beSQuentin Monnet * towards the raw buffer pointed by *to*, of size *to_size* 11901fdd08beSQuentin Monnet * (same remark). An optional *seed* can be added to the value 11911fdd08beSQuentin Monnet * (this can be cascaded, the seed may come from a previous call 11921fdd08beSQuentin Monnet * to the helper). 11931fdd08beSQuentin Monnet * 11941fdd08beSQuentin Monnet * This is flexible enough to be used in several ways: 11951fdd08beSQuentin Monnet * 11961fdd08beSQuentin Monnet * * With *from_size* == 0, *to_size* > 0 and *seed* set to 11971fdd08beSQuentin Monnet * checksum, it can be used when pushing new data. 11981fdd08beSQuentin Monnet * * With *from_size* > 0, *to_size* == 0 and *seed* set to 11991fdd08beSQuentin Monnet * checksum, it can be used when removing data from a packet. 12001fdd08beSQuentin Monnet * * With *from_size* > 0, *to_size* > 0 and *seed* set to 0, it 12011fdd08beSQuentin Monnet * can be used to compute a diff. Note that *from_size* and 12021fdd08beSQuentin Monnet * *to_size* do not need to be equal. 12031fdd08beSQuentin Monnet * 12041fdd08beSQuentin Monnet * This helper can be used in combination with 12051fdd08beSQuentin Monnet * **bpf_l3_csum_replace**\ () and **bpf_l4_csum_replace**\ (), to 12061fdd08beSQuentin Monnet * which one can feed in the difference computed with 12071fdd08beSQuentin Monnet * **bpf_csum_diff**\ (). 12081fdd08beSQuentin Monnet * Return 12091fdd08beSQuentin Monnet * The checksum result, or a negative error code in case of 12101fdd08beSQuentin Monnet * failure. 12111fdd08beSQuentin Monnet * 12125f0e5412SAndrii Nakryiko * int bpf_skb_get_tunnel_opt(struct sk_buff *skb, void *opt, u32 size) 12131fdd08beSQuentin Monnet * Description 12141fdd08beSQuentin Monnet * Retrieve tunnel options metadata for the packet associated to 12151fdd08beSQuentin Monnet * *skb*, and store the raw tunnel option data to the buffer *opt* 12161fdd08beSQuentin Monnet * of *size*. 12171fdd08beSQuentin Monnet * 12181fdd08beSQuentin Monnet * This helper can be used with encapsulation devices that can 12191fdd08beSQuentin Monnet * operate in "collect metadata" mode (please refer to the related 12201fdd08beSQuentin Monnet * note in the description of **bpf_skb_get_tunnel_key**\ () for 12211fdd08beSQuentin Monnet * more details). A particular example where this can be used is 12221fdd08beSQuentin Monnet * in combination with the Geneve encapsulation protocol, where it 12231fdd08beSQuentin Monnet * allows for pushing (with **bpf_skb_get_tunnel_opt**\ () helper) 12241fdd08beSQuentin Monnet * and retrieving arbitrary TLVs (Type-Length-Value headers) from 12251fdd08beSQuentin Monnet * the eBPF program. This allows for full customization of these 12261fdd08beSQuentin Monnet * headers. 12271fdd08beSQuentin Monnet * Return 12281fdd08beSQuentin Monnet * The size of the option data retrieved. 12291fdd08beSQuentin Monnet * 12305f0e5412SAndrii Nakryiko * int bpf_skb_set_tunnel_opt(struct sk_buff *skb, void *opt, u32 size) 12311fdd08beSQuentin Monnet * Description 12321fdd08beSQuentin Monnet * Set tunnel options metadata for the packet associated to *skb* 12331fdd08beSQuentin Monnet * to the option data contained in the raw buffer *opt* of *size*. 12341fdd08beSQuentin Monnet * 12351fdd08beSQuentin Monnet * See also the description of the **bpf_skb_get_tunnel_opt**\ () 12361fdd08beSQuentin Monnet * helper for additional information. 12371fdd08beSQuentin Monnet * Return 12381fdd08beSQuentin Monnet * 0 on success, or a negative error in case of failure. 12391fdd08beSQuentin Monnet * 12401fdd08beSQuentin Monnet * int bpf_skb_change_proto(struct sk_buff *skb, __be16 proto, u64 flags) 12411fdd08beSQuentin Monnet * Description 12421fdd08beSQuentin Monnet * Change the protocol of the *skb* to *proto*. Currently 12431fdd08beSQuentin Monnet * supported are transition from IPv4 to IPv6, and from IPv6 to 12441fdd08beSQuentin Monnet * IPv4. The helper takes care of the groundwork for the 12451fdd08beSQuentin Monnet * transition, including resizing the socket buffer. The eBPF 12461fdd08beSQuentin Monnet * program is expected to fill the new headers, if any, via 12471fdd08beSQuentin Monnet * **skb_store_bytes**\ () and to recompute the checksums with 12481fdd08beSQuentin Monnet * **bpf_l3_csum_replace**\ () and **bpf_l4_csum_replace**\ 12491fdd08beSQuentin Monnet * (). The main case for this helper is to perform NAT64 12501fdd08beSQuentin Monnet * operations out of an eBPF program. 12511fdd08beSQuentin Monnet * 12521fdd08beSQuentin Monnet * Internally, the GSO type is marked as dodgy so that headers are 12531fdd08beSQuentin Monnet * checked and segments are recalculated by the GSO/GRO engine. 12541fdd08beSQuentin Monnet * The size for GSO target is adapted as well. 12551fdd08beSQuentin Monnet * 12561fdd08beSQuentin Monnet * All values for *flags* are reserved for future usage, and must 12571fdd08beSQuentin Monnet * be left at zero. 12581fdd08beSQuentin Monnet * 125932e7dc28SQuentin Monnet * A call to this helper is susceptible to change the underlying 12601fdd08beSQuentin Monnet * packet buffer. Therefore, at load time, all checks on pointers 12611fdd08beSQuentin Monnet * previously done by the verifier are invalidated and must be 12621fdd08beSQuentin Monnet * performed again, if the helper is used in combination with 12631fdd08beSQuentin Monnet * direct packet access. 12641fdd08beSQuentin Monnet * Return 12651fdd08beSQuentin Monnet * 0 on success, or a negative error in case of failure. 12661fdd08beSQuentin Monnet * 12671fdd08beSQuentin Monnet * int bpf_skb_change_type(struct sk_buff *skb, u32 type) 12681fdd08beSQuentin Monnet * Description 12691fdd08beSQuentin Monnet * Change the packet type for the packet associated to *skb*. This 12701fdd08beSQuentin Monnet * comes down to setting *skb*\ **->pkt_type** to *type*, except 12711fdd08beSQuentin Monnet * the eBPF program does not have a write access to *skb*\ 12721fdd08beSQuentin Monnet * **->pkt_type** beside this helper. Using a helper here allows 12731fdd08beSQuentin Monnet * for graceful handling of errors. 12741fdd08beSQuentin Monnet * 12751fdd08beSQuentin Monnet * The major use case is to change incoming *skb*s to 12761fdd08beSQuentin Monnet * **PACKET_HOST** in a programmatic way instead of having to 12771fdd08beSQuentin Monnet * recirculate via **redirect**\ (..., **BPF_F_INGRESS**), for 12781fdd08beSQuentin Monnet * example. 12791fdd08beSQuentin Monnet * 12801fdd08beSQuentin Monnet * Note that *type* only allows certain values. At this time, they 12811fdd08beSQuentin Monnet * are: 12821fdd08beSQuentin Monnet * 12831fdd08beSQuentin Monnet * **PACKET_HOST** 12841fdd08beSQuentin Monnet * Packet is for us. 12851fdd08beSQuentin Monnet * **PACKET_BROADCAST** 12861fdd08beSQuentin Monnet * Send packet to all. 12871fdd08beSQuentin Monnet * **PACKET_MULTICAST** 12881fdd08beSQuentin Monnet * Send packet to group. 12891fdd08beSQuentin Monnet * **PACKET_OTHERHOST** 12901fdd08beSQuentin Monnet * Send packet to someone else. 12911fdd08beSQuentin Monnet * Return 12921fdd08beSQuentin Monnet * 0 on success, or a negative error in case of failure. 12931fdd08beSQuentin Monnet * 1294c6b5fb86SQuentin Monnet * int bpf_skb_under_cgroup(struct sk_buff *skb, struct bpf_map *map, u32 index) 1295c6b5fb86SQuentin Monnet * Description 1296c6b5fb86SQuentin Monnet * Check whether *skb* is a descendant of the cgroup2 held by 1297c6b5fb86SQuentin Monnet * *map* of type **BPF_MAP_TYPE_CGROUP_ARRAY**, at *index*. 1298c6b5fb86SQuentin Monnet * Return 1299c6b5fb86SQuentin Monnet * The return value depends on the result of the test, and can be: 1300c6b5fb86SQuentin Monnet * 1301c6b5fb86SQuentin Monnet * * 0, if the *skb* failed the cgroup2 descendant test. 1302c6b5fb86SQuentin Monnet * * 1, if the *skb* succeeded the cgroup2 descendant test. 1303c6b5fb86SQuentin Monnet * * A negative error code, if an error occurred. 1304c6b5fb86SQuentin Monnet * 1305fa15601aSQuentin Monnet * u32 bpf_get_hash_recalc(struct sk_buff *skb) 1306fa15601aSQuentin Monnet * Description 1307fa15601aSQuentin Monnet * Retrieve the hash of the packet, *skb*\ **->hash**. If it is 1308fa15601aSQuentin Monnet * not set, in particular if the hash was cleared due to mangling, 1309fa15601aSQuentin Monnet * recompute this hash. Later accesses to the hash can be done 1310fa15601aSQuentin Monnet * directly with *skb*\ **->hash**. 1311fa15601aSQuentin Monnet * 1312fa15601aSQuentin Monnet * Calling **bpf_set_hash_invalid**\ (), changing a packet 1313fa15601aSQuentin Monnet * prototype with **bpf_skb_change_proto**\ (), or calling 1314fa15601aSQuentin Monnet * **bpf_skb_store_bytes**\ () with the 1315fa15601aSQuentin Monnet * **BPF_F_INVALIDATE_HASH** are actions susceptible to clear 1316fa15601aSQuentin Monnet * the hash and to trigger a new computation for the next call to 1317fa15601aSQuentin Monnet * **bpf_get_hash_recalc**\ (). 1318fa15601aSQuentin Monnet * Return 1319fa15601aSQuentin Monnet * The 32-bit hash. 1320fa15601aSQuentin Monnet * 1321c456dec4SQuentin Monnet * u64 bpf_get_current_task(void) 1322c456dec4SQuentin Monnet * Return 1323c456dec4SQuentin Monnet * A pointer to the current task struct. 1324fa15601aSQuentin Monnet * 1325c6b5fb86SQuentin Monnet * int bpf_probe_write_user(void *dst, const void *src, u32 len) 1326c6b5fb86SQuentin Monnet * Description 1327c6b5fb86SQuentin Monnet * Attempt in a safe way to write *len* bytes from the buffer 1328c6b5fb86SQuentin Monnet * *src* to *dst* in memory. It only works for threads that are in 1329c6b5fb86SQuentin Monnet * user context, and *dst* must be a valid user space address. 1330c6b5fb86SQuentin Monnet * 1331c6b5fb86SQuentin Monnet * This helper should not be used to implement any kind of 1332c6b5fb86SQuentin Monnet * security mechanism because of TOC-TOU attacks, but rather to 1333c6b5fb86SQuentin Monnet * debug, divert, and manipulate execution of semi-cooperative 1334c6b5fb86SQuentin Monnet * processes. 1335c6b5fb86SQuentin Monnet * 1336c6b5fb86SQuentin Monnet * Keep in mind that this feature is meant for experiments, and it 1337c6b5fb86SQuentin Monnet * has a risk of crashing the system and running programs. 1338c6b5fb86SQuentin Monnet * Therefore, when an eBPF program using this helper is attached, 1339c6b5fb86SQuentin Monnet * a warning including PID and process name is printed to kernel 1340c6b5fb86SQuentin Monnet * logs. 1341c6b5fb86SQuentin Monnet * Return 1342c6b5fb86SQuentin Monnet * 0 on success, or a negative error in case of failure. 1343c6b5fb86SQuentin Monnet * 1344c6b5fb86SQuentin Monnet * int bpf_current_task_under_cgroup(struct bpf_map *map, u32 index) 1345c6b5fb86SQuentin Monnet * Description 1346c6b5fb86SQuentin Monnet * Check whether the probe is being run is the context of a given 1347c6b5fb86SQuentin Monnet * subset of the cgroup2 hierarchy. The cgroup2 to test is held by 1348c6b5fb86SQuentin Monnet * *map* of type **BPF_MAP_TYPE_CGROUP_ARRAY**, at *index*. 1349c6b5fb86SQuentin Monnet * Return 1350c6b5fb86SQuentin Monnet * The return value depends on the result of the test, and can be: 1351c6b5fb86SQuentin Monnet * 1352c6b5fb86SQuentin Monnet * * 0, if the *skb* task belongs to the cgroup2. 1353c6b5fb86SQuentin Monnet * * 1, if the *skb* task does not belong to the cgroup2. 1354c6b5fb86SQuentin Monnet * * A negative error code, if an error occurred. 1355c6b5fb86SQuentin Monnet * 1356fa15601aSQuentin Monnet * int bpf_skb_change_tail(struct sk_buff *skb, u32 len, u64 flags) 1357fa15601aSQuentin Monnet * Description 1358fa15601aSQuentin Monnet * Resize (trim or grow) the packet associated to *skb* to the 1359fa15601aSQuentin Monnet * new *len*. The *flags* are reserved for future usage, and must 1360fa15601aSQuentin Monnet * be left at zero. 1361fa15601aSQuentin Monnet * 1362fa15601aSQuentin Monnet * The basic idea is that the helper performs the needed work to 1363fa15601aSQuentin Monnet * change the size of the packet, then the eBPF program rewrites 1364fa15601aSQuentin Monnet * the rest via helpers like **bpf_skb_store_bytes**\ (), 1365fa15601aSQuentin Monnet * **bpf_l3_csum_replace**\ (), **bpf_l3_csum_replace**\ () 1366fa15601aSQuentin Monnet * and others. This helper is a slow path utility intended for 1367fa15601aSQuentin Monnet * replies with control messages. And because it is targeted for 1368fa15601aSQuentin Monnet * slow path, the helper itself can afford to be slow: it 1369fa15601aSQuentin Monnet * implicitly linearizes, unclones and drops offloads from the 1370fa15601aSQuentin Monnet * *skb*. 1371fa15601aSQuentin Monnet * 137232e7dc28SQuentin Monnet * A call to this helper is susceptible to change the underlying 1373fa15601aSQuentin Monnet * packet buffer. Therefore, at load time, all checks on pointers 1374fa15601aSQuentin Monnet * previously done by the verifier are invalidated and must be 1375fa15601aSQuentin Monnet * performed again, if the helper is used in combination with 1376fa15601aSQuentin Monnet * direct packet access. 1377fa15601aSQuentin Monnet * Return 1378fa15601aSQuentin Monnet * 0 on success, or a negative error in case of failure. 1379fa15601aSQuentin Monnet * 1380fa15601aSQuentin Monnet * int bpf_skb_pull_data(struct sk_buff *skb, u32 len) 1381fa15601aSQuentin Monnet * Description 1382fa15601aSQuentin Monnet * Pull in non-linear data in case the *skb* is non-linear and not 1383fa15601aSQuentin Monnet * all of *len* are part of the linear section. Make *len* bytes 1384fa15601aSQuentin Monnet * from *skb* readable and writable. If a zero value is passed for 1385fa15601aSQuentin Monnet * *len*, then the whole length of the *skb* is pulled. 1386fa15601aSQuentin Monnet * 1387fa15601aSQuentin Monnet * This helper is only needed for reading and writing with direct 1388fa15601aSQuentin Monnet * packet access. 1389fa15601aSQuentin Monnet * 1390fa15601aSQuentin Monnet * For direct packet access, testing that offsets to access 1391fa15601aSQuentin Monnet * are within packet boundaries (test on *skb*\ **->data_end**) is 1392fa15601aSQuentin Monnet * susceptible to fail if offsets are invalid, or if the requested 1393fa15601aSQuentin Monnet * data is in non-linear parts of the *skb*. On failure the 1394fa15601aSQuentin Monnet * program can just bail out, or in the case of a non-linear 1395fa15601aSQuentin Monnet * buffer, use a helper to make the data available. The 1396fa15601aSQuentin Monnet * **bpf_skb_load_bytes**\ () helper is a first solution to access 1397fa15601aSQuentin Monnet * the data. Another one consists in using **bpf_skb_pull_data** 1398fa15601aSQuentin Monnet * to pull in once the non-linear parts, then retesting and 1399fa15601aSQuentin Monnet * eventually access the data. 1400fa15601aSQuentin Monnet * 1401fa15601aSQuentin Monnet * At the same time, this also makes sure the *skb* is uncloned, 1402fa15601aSQuentin Monnet * which is a necessary condition for direct write. As this needs 1403fa15601aSQuentin Monnet * to be an invariant for the write part only, the verifier 1404fa15601aSQuentin Monnet * detects writes and adds a prologue that is calling 1405fa15601aSQuentin Monnet * **bpf_skb_pull_data()** to effectively unclone the *skb* from 1406fa15601aSQuentin Monnet * the very beginning in case it is indeed cloned. 1407fa15601aSQuentin Monnet * 140832e7dc28SQuentin Monnet * A call to this helper is susceptible to change the underlying 1409fa15601aSQuentin Monnet * packet buffer. Therefore, at load time, all checks on pointers 1410fa15601aSQuentin Monnet * previously done by the verifier are invalidated and must be 1411fa15601aSQuentin Monnet * performed again, if the helper is used in combination with 1412fa15601aSQuentin Monnet * direct packet access. 1413fa15601aSQuentin Monnet * Return 1414fa15601aSQuentin Monnet * 0 on success, or a negative error in case of failure. 1415fa15601aSQuentin Monnet * 1416fa15601aSQuentin Monnet * s64 bpf_csum_update(struct sk_buff *skb, __wsum csum) 1417fa15601aSQuentin Monnet * Description 1418fa15601aSQuentin Monnet * Add the checksum *csum* into *skb*\ **->csum** in case the 1419fa15601aSQuentin Monnet * driver has supplied a checksum for the entire packet into that 1420fa15601aSQuentin Monnet * field. Return an error otherwise. This helper is intended to be 1421fa15601aSQuentin Monnet * used in combination with **bpf_csum_diff**\ (), in particular 1422fa15601aSQuentin Monnet * when the checksum needs to be updated after data has been 1423fa15601aSQuentin Monnet * written into the packet through direct packet access. 1424fa15601aSQuentin Monnet * Return 1425fa15601aSQuentin Monnet * The checksum on success, or a negative error code in case of 1426fa15601aSQuentin Monnet * failure. 1427fa15601aSQuentin Monnet * 1428fa15601aSQuentin Monnet * void bpf_set_hash_invalid(struct sk_buff *skb) 1429fa15601aSQuentin Monnet * Description 1430fa15601aSQuentin Monnet * Invalidate the current *skb*\ **->hash**. It can be used after 1431fa15601aSQuentin Monnet * mangling on headers through direct packet access, in order to 1432fa15601aSQuentin Monnet * indicate that the hash is outdated and to trigger a 1433fa15601aSQuentin Monnet * recalculation the next time the kernel tries to access this 1434fa15601aSQuentin Monnet * hash or when the **bpf_get_hash_recalc**\ () helper is called. 1435fa15601aSQuentin Monnet * 1436fa15601aSQuentin Monnet * int bpf_get_numa_node_id(void) 1437fa15601aSQuentin Monnet * Description 1438fa15601aSQuentin Monnet * Return the id of the current NUMA node. The primary use case 1439fa15601aSQuentin Monnet * for this helper is the selection of sockets for the local NUMA 1440fa15601aSQuentin Monnet * node, when the program is attached to sockets using the 1441fa15601aSQuentin Monnet * **SO_ATTACH_REUSEPORT_EBPF** option (see also **socket(7)**), 1442fa15601aSQuentin Monnet * but the helper is also available to other eBPF program types, 1443fa15601aSQuentin Monnet * similarly to **bpf_get_smp_processor_id**\ (). 1444fa15601aSQuentin Monnet * Return 1445fa15601aSQuentin Monnet * The id of current NUMA node. 1446fa15601aSQuentin Monnet * 1447c6b5fb86SQuentin Monnet * int bpf_skb_change_head(struct sk_buff *skb, u32 len, u64 flags) 1448c6b5fb86SQuentin Monnet * Description 1449c6b5fb86SQuentin Monnet * Grows headroom of packet associated to *skb* and adjusts the 1450c6b5fb86SQuentin Monnet * offset of the MAC header accordingly, adding *len* bytes of 1451c6b5fb86SQuentin Monnet * space. It automatically extends and reallocates memory as 1452c6b5fb86SQuentin Monnet * required. 1453c6b5fb86SQuentin Monnet * 1454c6b5fb86SQuentin Monnet * This helper can be used on a layer 3 *skb* to push a MAC header 1455c6b5fb86SQuentin Monnet * for redirection into a layer 2 device. 1456c6b5fb86SQuentin Monnet * 1457c6b5fb86SQuentin Monnet * All values for *flags* are reserved for future usage, and must 1458c6b5fb86SQuentin Monnet * be left at zero. 1459c6b5fb86SQuentin Monnet * 146032e7dc28SQuentin Monnet * A call to this helper is susceptible to change the underlying 1461c6b5fb86SQuentin Monnet * packet buffer. Therefore, at load time, all checks on pointers 1462c6b5fb86SQuentin Monnet * previously done by the verifier are invalidated and must be 1463c6b5fb86SQuentin Monnet * performed again, if the helper is used in combination with 1464c6b5fb86SQuentin Monnet * direct packet access. 1465c6b5fb86SQuentin Monnet * Return 1466c6b5fb86SQuentin Monnet * 0 on success, or a negative error in case of failure. 1467c6b5fb86SQuentin Monnet * 1468c6b5fb86SQuentin Monnet * int bpf_xdp_adjust_head(struct xdp_buff *xdp_md, int delta) 1469c6b5fb86SQuentin Monnet * Description 1470c6b5fb86SQuentin Monnet * Adjust (move) *xdp_md*\ **->data** by *delta* bytes. Note that 1471c6b5fb86SQuentin Monnet * it is possible to use a negative value for *delta*. This helper 1472c6b5fb86SQuentin Monnet * can be used to prepare the packet for pushing or popping 1473c6b5fb86SQuentin Monnet * headers. 1474c6b5fb86SQuentin Monnet * 147532e7dc28SQuentin Monnet * A call to this helper is susceptible to change the underlying 1476c6b5fb86SQuentin Monnet * packet buffer. Therefore, at load time, all checks on pointers 1477c6b5fb86SQuentin Monnet * previously done by the verifier are invalidated and must be 1478c6b5fb86SQuentin Monnet * performed again, if the helper is used in combination with 1479c6b5fb86SQuentin Monnet * direct packet access. 1480c6b5fb86SQuentin Monnet * Return 1481c6b5fb86SQuentin Monnet * 0 on success, or a negative error in case of failure. 1482c6b5fb86SQuentin Monnet * 14836ae08ae3SDaniel Borkmann * int bpf_probe_read_str(void *dst, u32 size, const void *unsafe_ptr) 1484c6b5fb86SQuentin Monnet * Description 14856ae08ae3SDaniel Borkmann * Copy a NUL terminated string from an unsafe kernel address 14866ae08ae3SDaniel Borkmann * *unsafe_ptr* to *dst*. See bpf_probe_read_kernel_str() for 14876ae08ae3SDaniel Borkmann * more details. 1488c6b5fb86SQuentin Monnet * 14896ae08ae3SDaniel Borkmann * Generally, use bpf_probe_read_user_str() or bpf_probe_read_kernel_str() 14906ae08ae3SDaniel Borkmann * instead. 1491c6b5fb86SQuentin Monnet * Return 1492c6b5fb86SQuentin Monnet * On success, the strictly positive length of the string, 1493c6b5fb86SQuentin Monnet * including the trailing NUL character. On error, a negative 1494c6b5fb86SQuentin Monnet * value. 1495c6b5fb86SQuentin Monnet * 1496c6b5fb86SQuentin Monnet * u64 bpf_get_socket_cookie(struct sk_buff *skb) 1497c6b5fb86SQuentin Monnet * Description 1498c6b5fb86SQuentin Monnet * If the **struct sk_buff** pointed by *skb* has a known socket, 1499c6b5fb86SQuentin Monnet * retrieve the cookie (generated by the kernel) of this socket. 1500c6b5fb86SQuentin Monnet * If no cookie has been set yet, generate a new cookie. Once 1501c6b5fb86SQuentin Monnet * generated, the socket cookie remains stable for the life of the 1502c6b5fb86SQuentin Monnet * socket. This helper can be useful for monitoring per socket 1503cd48bddaSDaniel Borkmann * networking traffic statistics as it provides a global socket 1504cd48bddaSDaniel Borkmann * identifier that can be assumed unique. 1505c6b5fb86SQuentin Monnet * Return 1506c6b5fb86SQuentin Monnet * A 8-byte long non-decreasing number on success, or 0 if the 1507c6b5fb86SQuentin Monnet * socket field is missing inside *skb*. 1508c6b5fb86SQuentin Monnet * 1509d692f113SAndrey Ignatov * u64 bpf_get_socket_cookie(struct bpf_sock_addr *ctx) 1510d692f113SAndrey Ignatov * Description 1511d692f113SAndrey Ignatov * Equivalent to bpf_get_socket_cookie() helper that accepts 151262369db2SQuentin Monnet * *skb*, but gets socket from **struct bpf_sock_addr** context. 1513d692f113SAndrey Ignatov * Return 1514d692f113SAndrey Ignatov * A 8-byte long non-decreasing number. 1515d692f113SAndrey Ignatov * 1516d692f113SAndrey Ignatov * u64 bpf_get_socket_cookie(struct bpf_sock_ops *ctx) 1517d692f113SAndrey Ignatov * Description 1518d692f113SAndrey Ignatov * Equivalent to bpf_get_socket_cookie() helper that accepts 151962369db2SQuentin Monnet * *skb*, but gets socket from **struct bpf_sock_ops** context. 1520d692f113SAndrey Ignatov * Return 1521d692f113SAndrey Ignatov * A 8-byte long non-decreasing number. 1522d692f113SAndrey Ignatov * 1523c6b5fb86SQuentin Monnet * u32 bpf_get_socket_uid(struct sk_buff *skb) 1524c6b5fb86SQuentin Monnet * Return 1525c6b5fb86SQuentin Monnet * The owner UID of the socket associated to *skb*. If the socket 1526c6b5fb86SQuentin Monnet * is **NULL**, or if it is not a full socket (i.e. if it is a 1527c6b5fb86SQuentin Monnet * time-wait or a request socket instead), **overflowuid** value 1528c6b5fb86SQuentin Monnet * is returned (note that **overflowuid** might also be the actual 1529c6b5fb86SQuentin Monnet * UID value for the socket). 1530c6b5fb86SQuentin Monnet * 1531fa15601aSQuentin Monnet * u32 bpf_set_hash(struct sk_buff *skb, u32 hash) 1532fa15601aSQuentin Monnet * Description 1533fa15601aSQuentin Monnet * Set the full hash for *skb* (set the field *skb*\ **->hash**) 1534fa15601aSQuentin Monnet * to value *hash*. 1535fa15601aSQuentin Monnet * Return 1536fa15601aSQuentin Monnet * 0 1537fa15601aSQuentin Monnet * 15385f0e5412SAndrii Nakryiko * int bpf_setsockopt(struct bpf_sock_ops *bpf_socket, int level, int optname, void *optval, int optlen) 15397aa79a86SQuentin Monnet * Description 15407aa79a86SQuentin Monnet * Emulate a call to **setsockopt()** on the socket associated to 15417aa79a86SQuentin Monnet * *bpf_socket*, which must be a full socket. The *level* at 15427aa79a86SQuentin Monnet * which the option resides and the name *optname* of the option 15437aa79a86SQuentin Monnet * must be specified, see **setsockopt(2)** for more information. 15447aa79a86SQuentin Monnet * The option value of length *optlen* is pointed by *optval*. 15457aa79a86SQuentin Monnet * 15467aa79a86SQuentin Monnet * This helper actually implements a subset of **setsockopt()**. 15477aa79a86SQuentin Monnet * It supports the following *level*\ s: 15487aa79a86SQuentin Monnet * 15497aa79a86SQuentin Monnet * * **SOL_SOCKET**, which supports the following *optname*\ s: 15507aa79a86SQuentin Monnet * **SO_RCVBUF**, **SO_SNDBUF**, **SO_MAX_PACING_RATE**, 15517aa79a86SQuentin Monnet * **SO_PRIORITY**, **SO_RCVLOWAT**, **SO_MARK**. 15527aa79a86SQuentin Monnet * * **IPPROTO_TCP**, which supports the following *optname*\ s: 15537aa79a86SQuentin Monnet * **TCP_CONGESTION**, **TCP_BPF_IW**, 15547aa79a86SQuentin Monnet * **TCP_BPF_SNDCWND_CLAMP**. 15557aa79a86SQuentin Monnet * * **IPPROTO_IP**, which supports *optname* **IP_TOS**. 15567aa79a86SQuentin Monnet * * **IPPROTO_IPV6**, which supports *optname* **IPV6_TCLASS**. 15577aa79a86SQuentin Monnet * Return 15587aa79a86SQuentin Monnet * 0 on success, or a negative error in case of failure. 15597aa79a86SQuentin Monnet * 1560b55cbc8dSNicolas Dichtel * int bpf_skb_adjust_room(struct sk_buff *skb, s32 len_diff, u32 mode, u64 flags) 1561fa15601aSQuentin Monnet * Description 1562fa15601aSQuentin Monnet * Grow or shrink the room for data in the packet associated to 1563fa15601aSQuentin Monnet * *skb* by *len_diff*, and according to the selected *mode*. 1564fa15601aSQuentin Monnet * 156514aa3192SWillem de Bruijn * There are two supported modes at this time: 156614aa3192SWillem de Bruijn * 156714aa3192SWillem de Bruijn * * **BPF_ADJ_ROOM_MAC**: Adjust room at the mac layer 156814aa3192SWillem de Bruijn * (room space is added or removed below the layer 2 header). 1569fa15601aSQuentin Monnet * 1570fa15601aSQuentin Monnet * * **BPF_ADJ_ROOM_NET**: Adjust room at the network layer 1571fa15601aSQuentin Monnet * (room space is added or removed below the layer 3 header). 1572fa15601aSQuentin Monnet * 1573868d5235SWillem de Bruijn * The following flags are supported at this time: 15742278f6ccSWillem de Bruijn * 15752278f6ccSWillem de Bruijn * * **BPF_F_ADJ_ROOM_FIXED_GSO**: Do not adjust gso_size. 15762278f6ccSWillem de Bruijn * Adjusting mss in this way is not allowed for datagrams. 1577fa15601aSQuentin Monnet * 157880867c5eSQuentin Monnet * * **BPF_F_ADJ_ROOM_ENCAP_L3_IPV4**, 157980867c5eSQuentin Monnet * **BPF_F_ADJ_ROOM_ENCAP_L3_IPV6**: 1580868d5235SWillem de Bruijn * Any new space is reserved to hold a tunnel header. 1581868d5235SWillem de Bruijn * Configure skb offsets and other fields accordingly. 1582868d5235SWillem de Bruijn * 158380867c5eSQuentin Monnet * * **BPF_F_ADJ_ROOM_ENCAP_L4_GRE**, 158480867c5eSQuentin Monnet * **BPF_F_ADJ_ROOM_ENCAP_L4_UDP**: 1585868d5235SWillem de Bruijn * Use with ENCAP_L3 flags to further specify the tunnel type. 1586868d5235SWillem de Bruijn * 158780867c5eSQuentin Monnet * * **BPF_F_ADJ_ROOM_ENCAP_L2**\ (*len*): 158858dfc900SAlan Maguire * Use with ENCAP_L3/L4 flags to further specify the tunnel 158980867c5eSQuentin Monnet * type; *len* is the length of the inner MAC header. 159058dfc900SAlan Maguire * 159132e7dc28SQuentin Monnet * A call to this helper is susceptible to change the underlying 1592fa15601aSQuentin Monnet * packet buffer. Therefore, at load time, all checks on pointers 1593fa15601aSQuentin Monnet * previously done by the verifier are invalidated and must be 1594fa15601aSQuentin Monnet * performed again, if the helper is used in combination with 1595fa15601aSQuentin Monnet * direct packet access. 1596fa15601aSQuentin Monnet * Return 1597fa15601aSQuentin Monnet * 0 on success, or a negative error in case of failure. 1598fa15601aSQuentin Monnet * 1599ab127040SQuentin Monnet * int bpf_redirect_map(struct bpf_map *map, u32 key, u64 flags) 1600ab127040SQuentin Monnet * Description 1601ab127040SQuentin Monnet * Redirect the packet to the endpoint referenced by *map* at 1602ab127040SQuentin Monnet * index *key*. Depending on its type, this *map* can contain 1603ab127040SQuentin Monnet * references to net devices (for forwarding packets through other 1604ab127040SQuentin Monnet * ports), or to CPUs (for redirecting XDP frames to another CPU; 1605ab127040SQuentin Monnet * but this is only implemented for native XDP (with driver 1606ab127040SQuentin Monnet * support) as of this writing). 1607ab127040SQuentin Monnet * 160843e74c02SToke Høiland-Jørgensen * The lower two bits of *flags* are used as the return code if 160943e74c02SToke Høiland-Jørgensen * the map lookup fails. This is so that the return value can be 161043e74c02SToke Høiland-Jørgensen * one of the XDP program return codes up to XDP_TX, as chosen by 161143e74c02SToke Høiland-Jørgensen * the caller. Any higher bits in the *flags* argument must be 161243e74c02SToke Høiland-Jørgensen * unset. 1613ab127040SQuentin Monnet * 1614ab127040SQuentin Monnet * When used to redirect packets to net devices, this helper 1615ab127040SQuentin Monnet * provides a high performance increase over **bpf_redirect**\ (). 1616ab127040SQuentin Monnet * This is due to various implementation details of the underlying 1617ab127040SQuentin Monnet * mechanisms, one of which is the fact that **bpf_redirect_map**\ 1618ab127040SQuentin Monnet * () tries to send packet as a "bulk" to the device. 1619ab127040SQuentin Monnet * Return 1620ab127040SQuentin Monnet * **XDP_REDIRECT** on success, or **XDP_ABORTED** on error. 1621ab127040SQuentin Monnet * 16225f0e5412SAndrii Nakryiko * int bpf_sk_redirect_map(struct sk_buff *skb, struct bpf_map *map, u32 key, u64 flags) 1623ab127040SQuentin Monnet * Description 1624ab127040SQuentin Monnet * Redirect the packet to the socket referenced by *map* (of type 1625ab127040SQuentin Monnet * **BPF_MAP_TYPE_SOCKMAP**) at index *key*. Both ingress and 1626ab127040SQuentin Monnet * egress interfaces can be used for redirection. The 1627ab127040SQuentin Monnet * **BPF_F_INGRESS** value in *flags* is used to make the 1628ab127040SQuentin Monnet * distinction (ingress path is selected if the flag is present, 1629ab127040SQuentin Monnet * egress path otherwise). This is the only flag supported for now. 1630ab127040SQuentin Monnet * Return 1631ab127040SQuentin Monnet * **SK_PASS** on success, or **SK_DROP** on error. 1632ab127040SQuentin Monnet * 1633a3ef8e9aSAndrey Ignatov * int bpf_sock_map_update(struct bpf_sock_ops *skops, struct bpf_map *map, void *key, u64 flags) 1634ab127040SQuentin Monnet * Description 1635ab127040SQuentin Monnet * Add an entry to, or update a *map* referencing sockets. The 1636ab127040SQuentin Monnet * *skops* is used as a new value for the entry associated to 1637ab127040SQuentin Monnet * *key*. *flags* is one of: 1638ab127040SQuentin Monnet * 1639ab127040SQuentin Monnet * **BPF_NOEXIST** 1640ab127040SQuentin Monnet * The entry for *key* must not exist in the map. 1641ab127040SQuentin Monnet * **BPF_EXIST** 1642ab127040SQuentin Monnet * The entry for *key* must already exist in the map. 1643ab127040SQuentin Monnet * **BPF_ANY** 1644ab127040SQuentin Monnet * No condition on the existence of the entry for *key*. 1645ab127040SQuentin Monnet * 1646ab127040SQuentin Monnet * If the *map* has eBPF programs (parser and verdict), those will 1647ab127040SQuentin Monnet * be inherited by the socket being added. If the socket is 1648ab127040SQuentin Monnet * already attached to eBPF programs, this results in an error. 1649ab127040SQuentin Monnet * Return 1650ab127040SQuentin Monnet * 0 on success, or a negative error in case of failure. 1651ab127040SQuentin Monnet * 1652fa15601aSQuentin Monnet * int bpf_xdp_adjust_meta(struct xdp_buff *xdp_md, int delta) 1653fa15601aSQuentin Monnet * Description 1654fa15601aSQuentin Monnet * Adjust the address pointed by *xdp_md*\ **->data_meta** by 1655fa15601aSQuentin Monnet * *delta* (which can be positive or negative). Note that this 1656fa15601aSQuentin Monnet * operation modifies the address stored in *xdp_md*\ **->data**, 1657fa15601aSQuentin Monnet * so the latter must be loaded only after the helper has been 1658fa15601aSQuentin Monnet * called. 1659fa15601aSQuentin Monnet * 1660fa15601aSQuentin Monnet * The use of *xdp_md*\ **->data_meta** is optional and programs 1661fa15601aSQuentin Monnet * are not required to use it. The rationale is that when the 1662fa15601aSQuentin Monnet * packet is processed with XDP (e.g. as DoS filter), it is 1663fa15601aSQuentin Monnet * possible to push further meta data along with it before passing 1664fa15601aSQuentin Monnet * to the stack, and to give the guarantee that an ingress eBPF 1665fa15601aSQuentin Monnet * program attached as a TC classifier on the same device can pick 1666fa15601aSQuentin Monnet * this up for further post-processing. Since TC works with socket 1667fa15601aSQuentin Monnet * buffers, it remains possible to set from XDP the **mark** or 1668fa15601aSQuentin Monnet * **priority** pointers, or other pointers for the socket buffer. 1669fa15601aSQuentin Monnet * Having this scratch space generic and programmable allows for 1670fa15601aSQuentin Monnet * more flexibility as the user is free to store whatever meta 1671fa15601aSQuentin Monnet * data they need. 1672fa15601aSQuentin Monnet * 167332e7dc28SQuentin Monnet * A call to this helper is susceptible to change the underlying 1674fa15601aSQuentin Monnet * packet buffer. Therefore, at load time, all checks on pointers 1675fa15601aSQuentin Monnet * previously done by the verifier are invalidated and must be 1676fa15601aSQuentin Monnet * performed again, if the helper is used in combination with 1677fa15601aSQuentin Monnet * direct packet access. 1678fa15601aSQuentin Monnet * Return 1679fa15601aSQuentin Monnet * 0 on success, or a negative error in case of failure. 16807aa79a86SQuentin Monnet * 16817aa79a86SQuentin Monnet * int bpf_perf_event_read_value(struct bpf_map *map, u64 flags, struct bpf_perf_event_value *buf, u32 buf_size) 16827aa79a86SQuentin Monnet * Description 16837aa79a86SQuentin Monnet * Read the value of a perf event counter, and store it into *buf* 16847aa79a86SQuentin Monnet * of size *buf_size*. This helper relies on a *map* of type 16857aa79a86SQuentin Monnet * **BPF_MAP_TYPE_PERF_EVENT_ARRAY**. The nature of the perf event 16867aa79a86SQuentin Monnet * counter is selected when *map* is updated with perf event file 16877aa79a86SQuentin Monnet * descriptors. The *map* is an array whose size is the number of 16887aa79a86SQuentin Monnet * available CPUs, and each cell contains a value relative to one 16897aa79a86SQuentin Monnet * CPU. The value to retrieve is indicated by *flags*, that 16907aa79a86SQuentin Monnet * contains the index of the CPU to look up, masked with 16917aa79a86SQuentin Monnet * **BPF_F_INDEX_MASK**. Alternatively, *flags* can be set to 16927aa79a86SQuentin Monnet * **BPF_F_CURRENT_CPU** to indicate that the value for the 16937aa79a86SQuentin Monnet * current CPU should be retrieved. 16947aa79a86SQuentin Monnet * 16957aa79a86SQuentin Monnet * This helper behaves in a way close to 16967aa79a86SQuentin Monnet * **bpf_perf_event_read**\ () helper, save that instead of 16977aa79a86SQuentin Monnet * just returning the value observed, it fills the *buf* 16987aa79a86SQuentin Monnet * structure. This allows for additional data to be retrieved: in 16997aa79a86SQuentin Monnet * particular, the enabled and running times (in *buf*\ 17007aa79a86SQuentin Monnet * **->enabled** and *buf*\ **->running**, respectively) are 17017aa79a86SQuentin Monnet * copied. In general, **bpf_perf_event_read_value**\ () is 17027aa79a86SQuentin Monnet * recommended over **bpf_perf_event_read**\ (), which has some 17037aa79a86SQuentin Monnet * ABI issues and provides fewer functionalities. 17047aa79a86SQuentin Monnet * 17057aa79a86SQuentin Monnet * These values are interesting, because hardware PMU (Performance 17067aa79a86SQuentin Monnet * Monitoring Unit) counters are limited resources. When there are 17077aa79a86SQuentin Monnet * more PMU based perf events opened than available counters, 17087aa79a86SQuentin Monnet * kernel will multiplex these events so each event gets certain 17097aa79a86SQuentin Monnet * percentage (but not all) of the PMU time. In case that 17107aa79a86SQuentin Monnet * multiplexing happens, the number of samples or counter value 17117aa79a86SQuentin Monnet * will not reflect the case compared to when no multiplexing 17127aa79a86SQuentin Monnet * occurs. This makes comparison between different runs difficult. 17137aa79a86SQuentin Monnet * Typically, the counter value should be normalized before 17147aa79a86SQuentin Monnet * comparing to other experiments. The usual normalization is done 17157aa79a86SQuentin Monnet * as follows. 17167aa79a86SQuentin Monnet * 17177aa79a86SQuentin Monnet * :: 17187aa79a86SQuentin Monnet * 17197aa79a86SQuentin Monnet * normalized_counter = counter * t_enabled / t_running 17207aa79a86SQuentin Monnet * 17217aa79a86SQuentin Monnet * Where t_enabled is the time enabled for event and t_running is 17227aa79a86SQuentin Monnet * the time running for event since last normalization. The 17237aa79a86SQuentin Monnet * enabled and running times are accumulated since the perf event 17247aa79a86SQuentin Monnet * open. To achieve scaling factor between two invocations of an 17257aa79a86SQuentin Monnet * eBPF program, users can can use CPU id as the key (which is 17267aa79a86SQuentin Monnet * typical for perf array usage model) to remember the previous 17277aa79a86SQuentin Monnet * value and do the calculation inside the eBPF program. 17287aa79a86SQuentin Monnet * Return 17297aa79a86SQuentin Monnet * 0 on success, or a negative error in case of failure. 17307aa79a86SQuentin Monnet * 1731a3ef8e9aSAndrey Ignatov * int bpf_perf_prog_read_value(struct bpf_perf_event_data *ctx, struct bpf_perf_event_value *buf, u32 buf_size) 17327aa79a86SQuentin Monnet * Description 17337aa79a86SQuentin Monnet * For en eBPF program attached to a perf event, retrieve the 17347aa79a86SQuentin Monnet * value of the event counter associated to *ctx* and store it in 17357aa79a86SQuentin Monnet * the structure pointed by *buf* and of size *buf_size*. Enabled 17367aa79a86SQuentin Monnet * and running times are also stored in the structure (see 17377aa79a86SQuentin Monnet * description of helper **bpf_perf_event_read_value**\ () for 17387aa79a86SQuentin Monnet * more details). 17397aa79a86SQuentin Monnet * Return 17407aa79a86SQuentin Monnet * 0 on success, or a negative error in case of failure. 17417aa79a86SQuentin Monnet * 17425f0e5412SAndrii Nakryiko * int bpf_getsockopt(struct bpf_sock_ops *bpf_socket, int level, int optname, void *optval, int optlen) 17437aa79a86SQuentin Monnet * Description 17447aa79a86SQuentin Monnet * Emulate a call to **getsockopt()** on the socket associated to 17457aa79a86SQuentin Monnet * *bpf_socket*, which must be a full socket. The *level* at 17467aa79a86SQuentin Monnet * which the option resides and the name *optname* of the option 17477aa79a86SQuentin Monnet * must be specified, see **getsockopt(2)** for more information. 17487aa79a86SQuentin Monnet * The retrieved value is stored in the structure pointed by 17497aa79a86SQuentin Monnet * *opval* and of length *optlen*. 17507aa79a86SQuentin Monnet * 17517aa79a86SQuentin Monnet * This helper actually implements a subset of **getsockopt()**. 17527aa79a86SQuentin Monnet * It supports the following *level*\ s: 17537aa79a86SQuentin Monnet * 17547aa79a86SQuentin Monnet * * **IPPROTO_TCP**, which supports *optname* 17557aa79a86SQuentin Monnet * **TCP_CONGESTION**. 17567aa79a86SQuentin Monnet * * **IPPROTO_IP**, which supports *optname* **IP_TOS**. 17577aa79a86SQuentin Monnet * * **IPPROTO_IPV6**, which supports *optname* **IPV6_TCLASS**. 17587aa79a86SQuentin Monnet * Return 17597aa79a86SQuentin Monnet * 0 on success, or a negative error in case of failure. 17607aa79a86SQuentin Monnet * 17618050a395SPeter Wu * int bpf_override_return(struct pt_regs *regs, u64 rc) 17627aa79a86SQuentin Monnet * Description 17637aa79a86SQuentin Monnet * Used for error injection, this helper uses kprobes to override 17647aa79a86SQuentin Monnet * the return value of the probed function, and to set it to *rc*. 17657aa79a86SQuentin Monnet * The first argument is the context *regs* on which the kprobe 17667aa79a86SQuentin Monnet * works. 17677aa79a86SQuentin Monnet * 17687aa79a86SQuentin Monnet * This helper works by setting setting the PC (program counter) 17697aa79a86SQuentin Monnet * to an override function which is run in place of the original 17707aa79a86SQuentin Monnet * probed function. This means the probed function is not run at 17717aa79a86SQuentin Monnet * all. The replacement function just returns with the required 17727aa79a86SQuentin Monnet * value. 17737aa79a86SQuentin Monnet * 17747aa79a86SQuentin Monnet * This helper has security implications, and thus is subject to 17757aa79a86SQuentin Monnet * restrictions. It is only available if the kernel was compiled 17767aa79a86SQuentin Monnet * with the **CONFIG_BPF_KPROBE_OVERRIDE** configuration 17777aa79a86SQuentin Monnet * option, and in this case it only works on functions tagged with 17787aa79a86SQuentin Monnet * **ALLOW_ERROR_INJECTION** in the kernel code. 17797aa79a86SQuentin Monnet * 17807aa79a86SQuentin Monnet * Also, the helper is only available for the architectures having 17817aa79a86SQuentin Monnet * the CONFIG_FUNCTION_ERROR_INJECTION option. As of this writing, 17827aa79a86SQuentin Monnet * x86 architecture is the only one to support this feature. 17837aa79a86SQuentin Monnet * Return 17847aa79a86SQuentin Monnet * 0 17857aa79a86SQuentin Monnet * 1786a3ef8e9aSAndrey Ignatov * int bpf_sock_ops_cb_flags_set(struct bpf_sock_ops *bpf_sock, int argval) 17877aa79a86SQuentin Monnet * Description 17887aa79a86SQuentin Monnet * Attempt to set the value of the **bpf_sock_ops_cb_flags** field 17897aa79a86SQuentin Monnet * for the full TCP socket associated to *bpf_sock_ops* to 17907aa79a86SQuentin Monnet * *argval*. 17917aa79a86SQuentin Monnet * 17927aa79a86SQuentin Monnet * The primary use of this field is to determine if there should 17937aa79a86SQuentin Monnet * be calls to eBPF programs of type 17947aa79a86SQuentin Monnet * **BPF_PROG_TYPE_SOCK_OPS** at various points in the TCP 17957aa79a86SQuentin Monnet * code. A program of the same type can change its value, per 17967aa79a86SQuentin Monnet * connection and as necessary, when the connection is 17977aa79a86SQuentin Monnet * established. This field is directly accessible for reading, but 17987aa79a86SQuentin Monnet * this helper must be used for updates in order to return an 17997aa79a86SQuentin Monnet * error if an eBPF program tries to set a callback that is not 18007aa79a86SQuentin Monnet * supported in the current kernel. 18017aa79a86SQuentin Monnet * 1802725721a6SViet Hoang Tran * *argval* is a flag array which can combine these flags: 18037aa79a86SQuentin Monnet * 18047aa79a86SQuentin Monnet * * **BPF_SOCK_OPS_RTO_CB_FLAG** (retransmission time out) 18057aa79a86SQuentin Monnet * * **BPF_SOCK_OPS_RETRANS_CB_FLAG** (retransmission) 18067aa79a86SQuentin Monnet * * **BPF_SOCK_OPS_STATE_CB_FLAG** (TCP state change) 180723729ff2SStanislav Fomichev * * **BPF_SOCK_OPS_RTT_CB_FLAG** (every RTT) 18087aa79a86SQuentin Monnet * 1809725721a6SViet Hoang Tran * Therefore, this function can be used to clear a callback flag by 1810725721a6SViet Hoang Tran * setting the appropriate bit to zero. e.g. to disable the RTO 1811725721a6SViet Hoang Tran * callback: 1812725721a6SViet Hoang Tran * 1813725721a6SViet Hoang Tran * **bpf_sock_ops_cb_flags_set(bpf_sock,** 1814725721a6SViet Hoang Tran * **bpf_sock->bpf_sock_ops_cb_flags & ~BPF_SOCK_OPS_RTO_CB_FLAG)** 1815725721a6SViet Hoang Tran * 18167aa79a86SQuentin Monnet * Here are some examples of where one could call such eBPF 18177aa79a86SQuentin Monnet * program: 18187aa79a86SQuentin Monnet * 18197aa79a86SQuentin Monnet * * When RTO fires. 18207aa79a86SQuentin Monnet * * When a packet is retransmitted. 18217aa79a86SQuentin Monnet * * When the connection terminates. 18227aa79a86SQuentin Monnet * * When a packet is sent. 18237aa79a86SQuentin Monnet * * When a packet is received. 18247aa79a86SQuentin Monnet * Return 18257aa79a86SQuentin Monnet * Code **-EINVAL** if the socket is not a full TCP socket; 18267aa79a86SQuentin Monnet * otherwise, a positive number containing the bits that could not 18277aa79a86SQuentin Monnet * be set is returned (which comes down to 0 if all bits were set 18287aa79a86SQuentin Monnet * as required). 18297aa79a86SQuentin Monnet * 1830ab127040SQuentin Monnet * int bpf_msg_redirect_map(struct sk_msg_buff *msg, struct bpf_map *map, u32 key, u64 flags) 1831ab127040SQuentin Monnet * Description 1832ab127040SQuentin Monnet * This helper is used in programs implementing policies at the 1833ab127040SQuentin Monnet * socket level. If the message *msg* is allowed to pass (i.e. if 1834ab127040SQuentin Monnet * the verdict eBPF program returns **SK_PASS**), redirect it to 1835ab127040SQuentin Monnet * the socket referenced by *map* (of type 1836ab127040SQuentin Monnet * **BPF_MAP_TYPE_SOCKMAP**) at index *key*. Both ingress and 1837ab127040SQuentin Monnet * egress interfaces can be used for redirection. The 1838ab127040SQuentin Monnet * **BPF_F_INGRESS** value in *flags* is used to make the 1839ab127040SQuentin Monnet * distinction (ingress path is selected if the flag is present, 1840ab127040SQuentin Monnet * egress path otherwise). This is the only flag supported for now. 1841ab127040SQuentin Monnet * Return 1842ab127040SQuentin Monnet * **SK_PASS** on success, or **SK_DROP** on error. 1843ab127040SQuentin Monnet * 1844ab127040SQuentin Monnet * int bpf_msg_apply_bytes(struct sk_msg_buff *msg, u32 bytes) 1845ab127040SQuentin Monnet * Description 1846ab127040SQuentin Monnet * For socket policies, apply the verdict of the eBPF program to 1847ab127040SQuentin Monnet * the next *bytes* (number of bytes) of message *msg*. 1848ab127040SQuentin Monnet * 1849ab127040SQuentin Monnet * For example, this helper can be used in the following cases: 1850ab127040SQuentin Monnet * 1851ab127040SQuentin Monnet * * A single **sendmsg**\ () or **sendfile**\ () system call 1852ab127040SQuentin Monnet * contains multiple logical messages that the eBPF program is 1853ab127040SQuentin Monnet * supposed to read and for which it should apply a verdict. 1854ab127040SQuentin Monnet * * An eBPF program only cares to read the first *bytes* of a 1855ab127040SQuentin Monnet * *msg*. If the message has a large payload, then setting up 1856ab127040SQuentin Monnet * and calling the eBPF program repeatedly for all bytes, even 1857ab127040SQuentin Monnet * though the verdict is already known, would create unnecessary 1858ab127040SQuentin Monnet * overhead. 1859ab127040SQuentin Monnet * 1860ab127040SQuentin Monnet * When called from within an eBPF program, the helper sets a 1861ab127040SQuentin Monnet * counter internal to the BPF infrastructure, that is used to 1862ab127040SQuentin Monnet * apply the last verdict to the next *bytes*. If *bytes* is 1863ab127040SQuentin Monnet * smaller than the current data being processed from a 1864ab127040SQuentin Monnet * **sendmsg**\ () or **sendfile**\ () system call, the first 1865ab127040SQuentin Monnet * *bytes* will be sent and the eBPF program will be re-run with 1866ab127040SQuentin Monnet * the pointer for start of data pointing to byte number *bytes* 1867ab127040SQuentin Monnet * **+ 1**. If *bytes* is larger than the current data being 1868ab127040SQuentin Monnet * processed, then the eBPF verdict will be applied to multiple 1869ab127040SQuentin Monnet * **sendmsg**\ () or **sendfile**\ () calls until *bytes* are 1870ab127040SQuentin Monnet * consumed. 1871ab127040SQuentin Monnet * 1872ab127040SQuentin Monnet * Note that if a socket closes with the internal counter holding 1873ab127040SQuentin Monnet * a non-zero value, this is not a problem because data is not 1874ab127040SQuentin Monnet * being buffered for *bytes* and is sent as it is received. 1875ab127040SQuentin Monnet * Return 1876ab127040SQuentin Monnet * 0 1877ab127040SQuentin Monnet * 1878ab127040SQuentin Monnet * int bpf_msg_cork_bytes(struct sk_msg_buff *msg, u32 bytes) 1879ab127040SQuentin Monnet * Description 1880ab127040SQuentin Monnet * For socket policies, prevent the execution of the verdict eBPF 1881ab127040SQuentin Monnet * program for message *msg* until *bytes* (byte number) have been 1882ab127040SQuentin Monnet * accumulated. 1883ab127040SQuentin Monnet * 1884ab127040SQuentin Monnet * This can be used when one needs a specific number of bytes 1885ab127040SQuentin Monnet * before a verdict can be assigned, even if the data spans 1886ab127040SQuentin Monnet * multiple **sendmsg**\ () or **sendfile**\ () calls. The extreme 1887ab127040SQuentin Monnet * case would be a user calling **sendmsg**\ () repeatedly with 1888ab127040SQuentin Monnet * 1-byte long message segments. Obviously, this is bad for 1889ab127040SQuentin Monnet * performance, but it is still valid. If the eBPF program needs 1890ab127040SQuentin Monnet * *bytes* bytes to validate a header, this helper can be used to 1891ab127040SQuentin Monnet * prevent the eBPF program to be called again until *bytes* have 1892ab127040SQuentin Monnet * been accumulated. 1893ab127040SQuentin Monnet * Return 1894ab127040SQuentin Monnet * 0 1895ab127040SQuentin Monnet * 1896ab127040SQuentin Monnet * int bpf_msg_pull_data(struct sk_msg_buff *msg, u32 start, u32 end, u64 flags) 1897ab127040SQuentin Monnet * Description 1898ab127040SQuentin Monnet * For socket policies, pull in non-linear data from user space 1899ab127040SQuentin Monnet * for *msg* and set pointers *msg*\ **->data** and *msg*\ 1900ab127040SQuentin Monnet * **->data_end** to *start* and *end* bytes offsets into *msg*, 1901ab127040SQuentin Monnet * respectively. 1902ab127040SQuentin Monnet * 1903ab127040SQuentin Monnet * If a program of type **BPF_PROG_TYPE_SK_MSG** is run on a 1904ab127040SQuentin Monnet * *msg* it can only parse data that the (**data**, **data_end**) 1905ab127040SQuentin Monnet * pointers have already consumed. For **sendmsg**\ () hooks this 1906ab127040SQuentin Monnet * is likely the first scatterlist element. But for calls relying 1907ab127040SQuentin Monnet * on the **sendpage** handler (e.g. **sendfile**\ ()) this will 1908ab127040SQuentin Monnet * be the range (**0**, **0**) because the data is shared with 1909ab127040SQuentin Monnet * user space and by default the objective is to avoid allowing 1910ab127040SQuentin Monnet * user space to modify data while (or after) eBPF verdict is 1911ab127040SQuentin Monnet * being decided. This helper can be used to pull in data and to 1912ab127040SQuentin Monnet * set the start and end pointer to given values. Data will be 1913ab127040SQuentin Monnet * copied if necessary (i.e. if data was not linear and if start 1914ab127040SQuentin Monnet * and end pointers do not point to the same chunk). 1915ab127040SQuentin Monnet * 191632e7dc28SQuentin Monnet * A call to this helper is susceptible to change the underlying 1917ab127040SQuentin Monnet * packet buffer. Therefore, at load time, all checks on pointers 1918ab127040SQuentin Monnet * previously done by the verifier are invalidated and must be 1919ab127040SQuentin Monnet * performed again, if the helper is used in combination with 1920ab127040SQuentin Monnet * direct packet access. 1921ab127040SQuentin Monnet * 1922ab127040SQuentin Monnet * All values for *flags* are reserved for future usage, and must 1923ab127040SQuentin Monnet * be left at zero. 1924ab127040SQuentin Monnet * Return 1925ab127040SQuentin Monnet * 0 on success, or a negative error in case of failure. 1926ab127040SQuentin Monnet * 1927a3ef8e9aSAndrey Ignatov * int bpf_bind(struct bpf_sock_addr *ctx, struct sockaddr *addr, int addr_len) 19287aa79a86SQuentin Monnet * Description 19297aa79a86SQuentin Monnet * Bind the socket associated to *ctx* to the address pointed by 19307aa79a86SQuentin Monnet * *addr*, of length *addr_len*. This allows for making outgoing 19317aa79a86SQuentin Monnet * connection from the desired IP address, which can be useful for 19327aa79a86SQuentin Monnet * example when all processes inside a cgroup should use one 19337aa79a86SQuentin Monnet * single IP address on a host that has multiple IP configured. 19347aa79a86SQuentin Monnet * 19357aa79a86SQuentin Monnet * This helper works for IPv4 and IPv6, TCP and UDP sockets. The 19367aa79a86SQuentin Monnet * domain (*addr*\ **->sa_family**) must be **AF_INET** (or 19377aa79a86SQuentin Monnet * **AF_INET6**). Looking for a free port to bind to can be 19387aa79a86SQuentin Monnet * expensive, therefore binding to port is not permitted by the 19397aa79a86SQuentin Monnet * helper: *addr*\ **->sin_port** (or **sin6_port**, respectively) 19407aa79a86SQuentin Monnet * must be set to zero. 19417aa79a86SQuentin Monnet * Return 19427aa79a86SQuentin Monnet * 0 on success, or a negative error in case of failure. 19432d020dd7SQuentin Monnet * 19442d020dd7SQuentin Monnet * int bpf_xdp_adjust_tail(struct xdp_buff *xdp_md, int delta) 19452d020dd7SQuentin Monnet * Description 19462d020dd7SQuentin Monnet * Adjust (move) *xdp_md*\ **->data_end** by *delta* bytes. It is 19472d020dd7SQuentin Monnet * only possible to shrink the packet as of this writing, 19482d020dd7SQuentin Monnet * therefore *delta* must be a negative integer. 19492d020dd7SQuentin Monnet * 195032e7dc28SQuentin Monnet * A call to this helper is susceptible to change the underlying 19512d020dd7SQuentin Monnet * packet buffer. Therefore, at load time, all checks on pointers 19522d020dd7SQuentin Monnet * previously done by the verifier are invalidated and must be 19532d020dd7SQuentin Monnet * performed again, if the helper is used in combination with 19542d020dd7SQuentin Monnet * direct packet access. 19552d020dd7SQuentin Monnet * Return 19562d020dd7SQuentin Monnet * 0 on success, or a negative error in case of failure. 19572d020dd7SQuentin Monnet * 19582d020dd7SQuentin Monnet * int bpf_skb_get_xfrm_state(struct sk_buff *skb, u32 index, struct bpf_xfrm_state *xfrm_state, u32 size, u64 flags) 19592d020dd7SQuentin Monnet * Description 19602d020dd7SQuentin Monnet * Retrieve the XFRM state (IP transform framework, see also 19612d020dd7SQuentin Monnet * **ip-xfrm(8)**) at *index* in XFRM "security path" for *skb*. 19622d020dd7SQuentin Monnet * 19632d020dd7SQuentin Monnet * The retrieved value is stored in the **struct bpf_xfrm_state** 19642d020dd7SQuentin Monnet * pointed by *xfrm_state* and of length *size*. 19652d020dd7SQuentin Monnet * 19662d020dd7SQuentin Monnet * All values for *flags* are reserved for future usage, and must 19672d020dd7SQuentin Monnet * be left at zero. 19682d020dd7SQuentin Monnet * 19692d020dd7SQuentin Monnet * This helper is available only if the kernel was compiled with 19702d020dd7SQuentin Monnet * **CONFIG_XFRM** configuration option. 19712d020dd7SQuentin Monnet * Return 19722d020dd7SQuentin Monnet * 0 on success, or a negative error in case of failure. 1973c195651eSYonghong Song * 19745f0e5412SAndrii Nakryiko * int bpf_get_stack(void *ctx, void *buf, u32 size, u64 flags) 1975c195651eSYonghong Song * Description 1976c195651eSYonghong Song * Return a user or a kernel stack in bpf program provided buffer. 1977c195651eSYonghong Song * To achieve this, the helper needs *ctx*, which is a pointer 1978c195651eSYonghong Song * to the context on which the tracing program is executed. 1979c195651eSYonghong Song * To store the stacktrace, the bpf program provides *buf* with 1980c195651eSYonghong Song * a nonnegative *size*. 1981c195651eSYonghong Song * 1982c195651eSYonghong Song * The last argument, *flags*, holds the number of stack frames to 1983c195651eSYonghong Song * skip (from 0 to 255), masked with 1984c195651eSYonghong Song * **BPF_F_SKIP_FIELD_MASK**. The next bits can be used to set 1985c195651eSYonghong Song * the following flags: 1986c195651eSYonghong Song * 1987c195651eSYonghong Song * **BPF_F_USER_STACK** 1988c195651eSYonghong Song * Collect a user space stack instead of a kernel stack. 1989c195651eSYonghong Song * **BPF_F_USER_BUILD_ID** 1990c195651eSYonghong Song * Collect buildid+offset instead of ips for user stack, 1991c195651eSYonghong Song * only valid if **BPF_F_USER_STACK** is also specified. 1992c195651eSYonghong Song * 1993c195651eSYonghong Song * **bpf_get_stack**\ () can collect up to 1994c195651eSYonghong Song * **PERF_MAX_STACK_DEPTH** both kernel and user frames, subject 1995c195651eSYonghong Song * to sufficient large buffer size. Note that 1996c195651eSYonghong Song * this limit can be controlled with the **sysctl** program, and 1997c195651eSYonghong Song * that it should be manually increased in order to profile long 1998c195651eSYonghong Song * user stacks (such as stacks for Java programs). To do so, use: 1999c195651eSYonghong Song * 2000c195651eSYonghong Song * :: 2001c195651eSYonghong Song * 2002c195651eSYonghong Song * # sysctl kernel.perf_event_max_stack=<new value> 2003c195651eSYonghong Song * Return 20047a279e93SQuentin Monnet * A non-negative value equal to or less than *size* on success, 20057a279e93SQuentin Monnet * or a negative error in case of failure. 20064e1ec56cSDaniel Borkmann * 20075f0e5412SAndrii Nakryiko * int bpf_skb_load_bytes_relative(const void *skb, u32 offset, void *to, u32 len, u32 start_header) 20084e1ec56cSDaniel Borkmann * Description 20094e1ec56cSDaniel Borkmann * This helper is similar to **bpf_skb_load_bytes**\ () in that 20104e1ec56cSDaniel Borkmann * it provides an easy way to load *len* bytes from *offset* 20114e1ec56cSDaniel Borkmann * from the packet associated to *skb*, into the buffer pointed 20124e1ec56cSDaniel Borkmann * by *to*. The difference to **bpf_skb_load_bytes**\ () is that 20134e1ec56cSDaniel Borkmann * a fifth argument *start_header* exists in order to select a 20144e1ec56cSDaniel Borkmann * base offset to start from. *start_header* can be one of: 20154e1ec56cSDaniel Borkmann * 20164e1ec56cSDaniel Borkmann * **BPF_HDR_START_MAC** 20174e1ec56cSDaniel Borkmann * Base offset to load data from is *skb*'s mac header. 20184e1ec56cSDaniel Borkmann * **BPF_HDR_START_NET** 20194e1ec56cSDaniel Borkmann * Base offset to load data from is *skb*'s network header. 20204e1ec56cSDaniel Borkmann * 20214e1ec56cSDaniel Borkmann * In general, "direct packet access" is the preferred method to 20224e1ec56cSDaniel Borkmann * access packet data, however, this helper is in particular useful 20234e1ec56cSDaniel Borkmann * in socket filters where *skb*\ **->data** does not always point 20244e1ec56cSDaniel Borkmann * to the start of the mac header and where "direct packet access" 20254e1ec56cSDaniel Borkmann * is not available. 20264e1ec56cSDaniel Borkmann * Return 20274e1ec56cSDaniel Borkmann * 0 on success, or a negative error in case of failure. 20284e1ec56cSDaniel Borkmann * 202987f5fc7eSDavid Ahern * int bpf_fib_lookup(void *ctx, struct bpf_fib_lookup *params, int plen, u32 flags) 203087f5fc7eSDavid Ahern * Description 203187f5fc7eSDavid Ahern * Do FIB lookup in kernel tables using parameters in *params*. 203287f5fc7eSDavid Ahern * If lookup is successful and result shows packet is to be 203387f5fc7eSDavid Ahern * forwarded, the neighbor tables are searched for the nexthop. 203487f5fc7eSDavid Ahern * If successful (ie., FIB lookup shows forwarding and nexthop 2035fa898d76SDavid Ahern * is resolved), the nexthop address is returned in ipv4_dst 2036fa898d76SDavid Ahern * or ipv6_dst based on family, smac is set to mac address of 2037fa898d76SDavid Ahern * egress device, dmac is set to nexthop mac address, rt_metric 20384c79579bSDavid Ahern * is set to metric from route (IPv4/IPv6 only), and ifindex 20394c79579bSDavid Ahern * is set to the device index of the nexthop from the FIB lookup. 204087f5fc7eSDavid Ahern * 204187f5fc7eSDavid Ahern * *plen* argument is the size of the passed in struct. 20427a279e93SQuentin Monnet * *flags* argument can be a combination of one or more of the 20437a279e93SQuentin Monnet * following values: 204487f5fc7eSDavid Ahern * 20457a279e93SQuentin Monnet * **BPF_FIB_LOOKUP_DIRECT** 20467a279e93SQuentin Monnet * Do a direct table lookup vs full lookup using FIB 20477a279e93SQuentin Monnet * rules. 20487a279e93SQuentin Monnet * **BPF_FIB_LOOKUP_OUTPUT** 20497a279e93SQuentin Monnet * Perform lookup from an egress perspective (default is 20507a279e93SQuentin Monnet * ingress). 205187f5fc7eSDavid Ahern * 205287f5fc7eSDavid Ahern * *ctx* is either **struct xdp_md** for XDP programs or 205387f5fc7eSDavid Ahern * **struct sk_buff** tc cls_act programs. 205487f5fc7eSDavid Ahern * Return 20554c79579bSDavid Ahern * * < 0 if any input argument is invalid 20564c79579bSDavid Ahern * * 0 on success (packet is forwarded, nexthop neighbor exists) 20574c79579bSDavid Ahern * * > 0 one of **BPF_FIB_LKUP_RET_** codes explaining why the 20582bae79d2SQuentin Monnet * packet is not forwarded or needs assist from full stack 205981110384SJohn Fastabend * 20605f0e5412SAndrii Nakryiko * int bpf_sock_hash_update(struct bpf_sock_ops *skops, struct bpf_map *map, void *key, u64 flags) 206181110384SJohn Fastabend * Description 206281110384SJohn Fastabend * Add an entry to, or update a sockhash *map* referencing sockets. 206381110384SJohn Fastabend * The *skops* is used as a new value for the entry associated to 206481110384SJohn Fastabend * *key*. *flags* is one of: 206581110384SJohn Fastabend * 206681110384SJohn Fastabend * **BPF_NOEXIST** 206781110384SJohn Fastabend * The entry for *key* must not exist in the map. 206881110384SJohn Fastabend * **BPF_EXIST** 206981110384SJohn Fastabend * The entry for *key* must already exist in the map. 207081110384SJohn Fastabend * **BPF_ANY** 207181110384SJohn Fastabend * No condition on the existence of the entry for *key*. 207281110384SJohn Fastabend * 207381110384SJohn Fastabend * If the *map* has eBPF programs (parser and verdict), those will 207481110384SJohn Fastabend * be inherited by the socket being added. If the socket is 207581110384SJohn Fastabend * already attached to eBPF programs, this results in an error. 207681110384SJohn Fastabend * Return 207781110384SJohn Fastabend * 0 on success, or a negative error in case of failure. 207881110384SJohn Fastabend * 207981110384SJohn Fastabend * int bpf_msg_redirect_hash(struct sk_msg_buff *msg, struct bpf_map *map, void *key, u64 flags) 208081110384SJohn Fastabend * Description 208181110384SJohn Fastabend * This helper is used in programs implementing policies at the 208281110384SJohn Fastabend * socket level. If the message *msg* is allowed to pass (i.e. if 208381110384SJohn Fastabend * the verdict eBPF program returns **SK_PASS**), redirect it to 208481110384SJohn Fastabend * the socket referenced by *map* (of type 208581110384SJohn Fastabend * **BPF_MAP_TYPE_SOCKHASH**) using hash *key*. Both ingress and 208681110384SJohn Fastabend * egress interfaces can be used for redirection. The 208781110384SJohn Fastabend * **BPF_F_INGRESS** value in *flags* is used to make the 208881110384SJohn Fastabend * distinction (ingress path is selected if the flag is present, 208981110384SJohn Fastabend * egress path otherwise). This is the only flag supported for now. 209081110384SJohn Fastabend * Return 209181110384SJohn Fastabend * **SK_PASS** on success, or **SK_DROP** on error. 209281110384SJohn Fastabend * 209381110384SJohn Fastabend * int bpf_sk_redirect_hash(struct sk_buff *skb, struct bpf_map *map, void *key, u64 flags) 209481110384SJohn Fastabend * Description 209581110384SJohn Fastabend * This helper is used in programs implementing policies at the 209681110384SJohn Fastabend * skb socket level. If the sk_buff *skb* is allowed to pass (i.e. 209781110384SJohn Fastabend * if the verdeict eBPF program returns **SK_PASS**), redirect it 209881110384SJohn Fastabend * to the socket referenced by *map* (of type 209981110384SJohn Fastabend * **BPF_MAP_TYPE_SOCKHASH**) using hash *key*. Both ingress and 210081110384SJohn Fastabend * egress interfaces can be used for redirection. The 210181110384SJohn Fastabend * **BPF_F_INGRESS** value in *flags* is used to make the 210281110384SJohn Fastabend * distinction (ingress path is selected if the flag is present, 210381110384SJohn Fastabend * egress otherwise). This is the only flag supported for now. 210481110384SJohn Fastabend * Return 210581110384SJohn Fastabend * **SK_PASS** on success, or **SK_DROP** on error. 2106fe94cc29SMathieu Xhonneux * 2107fe94cc29SMathieu Xhonneux * int bpf_lwt_push_encap(struct sk_buff *skb, u32 type, void *hdr, u32 len) 2108fe94cc29SMathieu Xhonneux * Description 2109fe94cc29SMathieu Xhonneux * Encapsulate the packet associated to *skb* within a Layer 3 2110fe94cc29SMathieu Xhonneux * protocol header. This header is provided in the buffer at 2111fe94cc29SMathieu Xhonneux * address *hdr*, with *len* its size in bytes. *type* indicates 2112fe94cc29SMathieu Xhonneux * the protocol of the header and can be one of: 2113fe94cc29SMathieu Xhonneux * 2114fe94cc29SMathieu Xhonneux * **BPF_LWT_ENCAP_SEG6** 2115fe94cc29SMathieu Xhonneux * IPv6 encapsulation with Segment Routing Header 2116fe94cc29SMathieu Xhonneux * (**struct ipv6_sr_hdr**). *hdr* only contains the SRH, 2117fe94cc29SMathieu Xhonneux * the IPv6 header is computed by the kernel. 2118fe94cc29SMathieu Xhonneux * **BPF_LWT_ENCAP_SEG6_INLINE** 2119fe94cc29SMathieu Xhonneux * Only works if *skb* contains an IPv6 packet. Insert a 2120fe94cc29SMathieu Xhonneux * Segment Routing Header (**struct ipv6_sr_hdr**) inside 2121fe94cc29SMathieu Xhonneux * the IPv6 header. 21223e0bd37cSPeter Oskolkov * **BPF_LWT_ENCAP_IP** 21233e0bd37cSPeter Oskolkov * IP encapsulation (GRE/GUE/IPIP/etc). The outer header 21243e0bd37cSPeter Oskolkov * must be IPv4 or IPv6, followed by zero or more 212580867c5eSQuentin Monnet * additional headers, up to **LWT_BPF_MAX_HEADROOM** 212680867c5eSQuentin Monnet * total bytes in all prepended headers. Please note that 212780867c5eSQuentin Monnet * if **skb_is_gso**\ (*skb*) is true, no more than two 212880867c5eSQuentin Monnet * headers can be prepended, and the inner header, if 212980867c5eSQuentin Monnet * present, should be either GRE or UDP/GUE. 21303e0bd37cSPeter Oskolkov * 213180867c5eSQuentin Monnet * **BPF_LWT_ENCAP_SEG6**\ \* types can be called by BPF programs 213280867c5eSQuentin Monnet * of type **BPF_PROG_TYPE_LWT_IN**; **BPF_LWT_ENCAP_IP** type can 213380867c5eSQuentin Monnet * be called by bpf programs of types **BPF_PROG_TYPE_LWT_IN** and 213480867c5eSQuentin Monnet * **BPF_PROG_TYPE_LWT_XMIT**. 2135fe94cc29SMathieu Xhonneux * 213632e7dc28SQuentin Monnet * A call to this helper is susceptible to change the underlying 2137fe94cc29SMathieu Xhonneux * packet buffer. Therefore, at load time, all checks on pointers 2138fe94cc29SMathieu Xhonneux * previously done by the verifier are invalidated and must be 2139fe94cc29SMathieu Xhonneux * performed again, if the helper is used in combination with 2140fe94cc29SMathieu Xhonneux * direct packet access. 2141fe94cc29SMathieu Xhonneux * Return 2142fe94cc29SMathieu Xhonneux * 0 on success, or a negative error in case of failure. 2143fe94cc29SMathieu Xhonneux * 2144fe94cc29SMathieu Xhonneux * int bpf_lwt_seg6_store_bytes(struct sk_buff *skb, u32 offset, const void *from, u32 len) 2145fe94cc29SMathieu Xhonneux * Description 2146fe94cc29SMathieu Xhonneux * Store *len* bytes from address *from* into the packet 2147fe94cc29SMathieu Xhonneux * associated to *skb*, at *offset*. Only the flags, tag and TLVs 2148fe94cc29SMathieu Xhonneux * inside the outermost IPv6 Segment Routing Header can be 2149fe94cc29SMathieu Xhonneux * modified through this helper. 2150fe94cc29SMathieu Xhonneux * 215132e7dc28SQuentin Monnet * A call to this helper is susceptible to change the underlying 2152fe94cc29SMathieu Xhonneux * packet buffer. Therefore, at load time, all checks on pointers 2153fe94cc29SMathieu Xhonneux * previously done by the verifier are invalidated and must be 2154fe94cc29SMathieu Xhonneux * performed again, if the helper is used in combination with 2155fe94cc29SMathieu Xhonneux * direct packet access. 2156fe94cc29SMathieu Xhonneux * Return 2157fe94cc29SMathieu Xhonneux * 0 on success, or a negative error in case of failure. 2158fe94cc29SMathieu Xhonneux * 2159fe94cc29SMathieu Xhonneux * int bpf_lwt_seg6_adjust_srh(struct sk_buff *skb, u32 offset, s32 delta) 2160fe94cc29SMathieu Xhonneux * Description 2161fe94cc29SMathieu Xhonneux * Adjust the size allocated to TLVs in the outermost IPv6 2162fe94cc29SMathieu Xhonneux * Segment Routing Header contained in the packet associated to 2163fe94cc29SMathieu Xhonneux * *skb*, at position *offset* by *delta* bytes. Only offsets 2164fe94cc29SMathieu Xhonneux * after the segments are accepted. *delta* can be as well 2165fe94cc29SMathieu Xhonneux * positive (growing) as negative (shrinking). 2166fe94cc29SMathieu Xhonneux * 216732e7dc28SQuentin Monnet * A call to this helper is susceptible to change the underlying 2168fe94cc29SMathieu Xhonneux * packet buffer. Therefore, at load time, all checks on pointers 2169fe94cc29SMathieu Xhonneux * previously done by the verifier are invalidated and must be 2170fe94cc29SMathieu Xhonneux * performed again, if the helper is used in combination with 2171fe94cc29SMathieu Xhonneux * direct packet access. 2172fe94cc29SMathieu Xhonneux * Return 2173fe94cc29SMathieu Xhonneux * 0 on success, or a negative error in case of failure. 2174fe94cc29SMathieu Xhonneux * 2175fe94cc29SMathieu Xhonneux * int bpf_lwt_seg6_action(struct sk_buff *skb, u32 action, void *param, u32 param_len) 2176fe94cc29SMathieu Xhonneux * Description 2177fe94cc29SMathieu Xhonneux * Apply an IPv6 Segment Routing action of type *action* to the 2178fe94cc29SMathieu Xhonneux * packet associated to *skb*. Each action takes a parameter 2179fe94cc29SMathieu Xhonneux * contained at address *param*, and of length *param_len* bytes. 2180fe94cc29SMathieu Xhonneux * *action* can be one of: 2181fe94cc29SMathieu Xhonneux * 2182fe94cc29SMathieu Xhonneux * **SEG6_LOCAL_ACTION_END_X** 2183fe94cc29SMathieu Xhonneux * End.X action: Endpoint with Layer-3 cross-connect. 2184fe94cc29SMathieu Xhonneux * Type of *param*: **struct in6_addr**. 2185fe94cc29SMathieu Xhonneux * **SEG6_LOCAL_ACTION_END_T** 2186fe94cc29SMathieu Xhonneux * End.T action: Endpoint with specific IPv6 table lookup. 2187fe94cc29SMathieu Xhonneux * Type of *param*: **int**. 2188fe94cc29SMathieu Xhonneux * **SEG6_LOCAL_ACTION_END_B6** 2189fe94cc29SMathieu Xhonneux * End.B6 action: Endpoint bound to an SRv6 policy. 219080867c5eSQuentin Monnet * Type of *param*: **struct ipv6_sr_hdr**. 2191fe94cc29SMathieu Xhonneux * **SEG6_LOCAL_ACTION_END_B6_ENCAP** 2192fe94cc29SMathieu Xhonneux * End.B6.Encap action: Endpoint bound to an SRv6 2193fe94cc29SMathieu Xhonneux * encapsulation policy. 219480867c5eSQuentin Monnet * Type of *param*: **struct ipv6_sr_hdr**. 2195fe94cc29SMathieu Xhonneux * 219632e7dc28SQuentin Monnet * A call to this helper is susceptible to change the underlying 2197fe94cc29SMathieu Xhonneux * packet buffer. Therefore, at load time, all checks on pointers 2198fe94cc29SMathieu Xhonneux * previously done by the verifier are invalidated and must be 2199fe94cc29SMathieu Xhonneux * performed again, if the helper is used in combination with 2200fe94cc29SMathieu Xhonneux * direct packet access. 2201fe94cc29SMathieu Xhonneux * Return 2202fe94cc29SMathieu Xhonneux * 0 on success, or a negative error in case of failure. 2203f4364dcfSSean Young * 220462369db2SQuentin Monnet * int bpf_rc_repeat(void *ctx) 220562369db2SQuentin Monnet * Description 220662369db2SQuentin Monnet * This helper is used in programs implementing IR decoding, to 220762369db2SQuentin Monnet * report a successfully decoded repeat key message. This delays 220862369db2SQuentin Monnet * the generation of a key up event for previously generated 220962369db2SQuentin Monnet * key down event. 221062369db2SQuentin Monnet * 221162369db2SQuentin Monnet * Some IR protocols like NEC have a special IR message for 221262369db2SQuentin Monnet * repeating last button, for when a button is held down. 221362369db2SQuentin Monnet * 221462369db2SQuentin Monnet * The *ctx* should point to the lirc sample as passed into 221562369db2SQuentin Monnet * the program. 221662369db2SQuentin Monnet * 221762369db2SQuentin Monnet * This helper is only available is the kernel was compiled with 221862369db2SQuentin Monnet * the **CONFIG_BPF_LIRC_MODE2** configuration option set to 221962369db2SQuentin Monnet * "**y**". 222062369db2SQuentin Monnet * Return 222162369db2SQuentin Monnet * 0 222262369db2SQuentin Monnet * 2223f4364dcfSSean Young * int bpf_rc_keydown(void *ctx, u32 protocol, u64 scancode, u32 toggle) 2224f4364dcfSSean Young * Description 2225f4364dcfSSean Young * This helper is used in programs implementing IR decoding, to 2226f4364dcfSSean Young * report a successfully decoded key press with *scancode*, 2227f4364dcfSSean Young * *toggle* value in the given *protocol*. The scancode will be 2228f4364dcfSSean Young * translated to a keycode using the rc keymap, and reported as 2229f4364dcfSSean Young * an input key down event. After a period a key up event is 2230f4364dcfSSean Young * generated. This period can be extended by calling either 223190b1023fSQuentin Monnet * **bpf_rc_keydown**\ () again with the same values, or calling 223290b1023fSQuentin Monnet * **bpf_rc_repeat**\ (). 2233f4364dcfSSean Young * 2234f4364dcfSSean Young * Some protocols include a toggle bit, in case the button was 2235f4364dcfSSean Young * released and pressed again between consecutive scancodes. 2236f4364dcfSSean Young * 2237f4364dcfSSean Young * The *ctx* should point to the lirc sample as passed into 2238f4364dcfSSean Young * the program. 2239f4364dcfSSean Young * 2240f4364dcfSSean Young * The *protocol* is the decoded protocol number (see 2241f4364dcfSSean Young * **enum rc_proto** for some predefined values). 2242f4364dcfSSean Young * 2243f4364dcfSSean Young * This helper is only available is the kernel was compiled with 2244f4364dcfSSean Young * the **CONFIG_BPF_LIRC_MODE2** configuration option set to 2245f4364dcfSSean Young * "**y**". 2246f4364dcfSSean Young * Return 2247f4364dcfSSean Young * 0 2248f4364dcfSSean Young * 224962369db2SQuentin Monnet * u64 bpf_skb_cgroup_id(struct sk_buff *skb) 2250cb20b08eSDaniel Borkmann * Description 2251cb20b08eSDaniel Borkmann * Return the cgroup v2 id of the socket associated with the *skb*. 2252cb20b08eSDaniel Borkmann * This is roughly similar to the **bpf_get_cgroup_classid**\ () 2253cb20b08eSDaniel Borkmann * helper for cgroup v1 by providing a tag resp. identifier that 2254cb20b08eSDaniel Borkmann * can be matched on or used for map lookups e.g. to implement 2255cb20b08eSDaniel Borkmann * policy. The cgroup v2 id of a given path in the hierarchy is 2256cb20b08eSDaniel Borkmann * exposed in user space through the f_handle API in order to get 2257cb20b08eSDaniel Borkmann * to the same 64-bit id. 2258cb20b08eSDaniel Borkmann * 2259cb20b08eSDaniel Borkmann * This helper can be used on TC egress path, but not on ingress, 2260cb20b08eSDaniel Borkmann * and is available only if the kernel was compiled with the 2261cb20b08eSDaniel Borkmann * **CONFIG_SOCK_CGROUP_DATA** configuration option. 2262cb20b08eSDaniel Borkmann * Return 2263cb20b08eSDaniel Borkmann * The id is returned or 0 in case the id could not be retrieved. 2264bf6fa2c8SYonghong Song * 2265bf6fa2c8SYonghong Song * u64 bpf_get_current_cgroup_id(void) 2266bf6fa2c8SYonghong Song * Return 2267bf6fa2c8SYonghong Song * A 64-bit integer containing the current cgroup id based 2268bf6fa2c8SYonghong Song * on the cgroup within which the current task is running. 2269cd339431SRoman Gushchin * 227062369db2SQuentin Monnet * void *bpf_get_local_storage(void *map, u64 flags) 2271cd339431SRoman Gushchin * Description 2272cd339431SRoman Gushchin * Get the pointer to the local storage area. 2273cd339431SRoman Gushchin * The type and the size of the local storage is defined 2274cd339431SRoman Gushchin * by the *map* argument. 2275cd339431SRoman Gushchin * The *flags* meaning is specific for each map type, 2276cd339431SRoman Gushchin * and has to be 0 for cgroup local storage. 2277cd339431SRoman Gushchin * 227890b1023fSQuentin Monnet * Depending on the BPF program type, a local storage area 227990b1023fSQuentin Monnet * can be shared between multiple instances of the BPF program, 2280cd339431SRoman Gushchin * running simultaneously. 2281cd339431SRoman Gushchin * 2282cd339431SRoman Gushchin * A user should care about the synchronization by himself. 228390b1023fSQuentin Monnet * For example, by using the **BPF_STX_XADD** instruction to alter 2284cd339431SRoman Gushchin * the shared data. 2285cd339431SRoman Gushchin * Return 228690b1023fSQuentin Monnet * A pointer to the local storage area. 22872dbb9b9eSMartin KaFai Lau * 22882dbb9b9eSMartin KaFai Lau * int bpf_sk_select_reuseport(struct sk_reuseport_md *reuse, struct bpf_map *map, void *key, u64 flags) 22892dbb9b9eSMartin KaFai Lau * Description 229090b1023fSQuentin Monnet * Select a **SO_REUSEPORT** socket from a 229190b1023fSQuentin Monnet * **BPF_MAP_TYPE_REUSEPORT_ARRAY** *map*. 229290b1023fSQuentin Monnet * It checks the selected socket is matching the incoming 229390b1023fSQuentin Monnet * request in the socket buffer. 22942dbb9b9eSMartin KaFai Lau * Return 22952dbb9b9eSMartin KaFai Lau * 0 on success, or a negative error in case of failure. 22966acc9b43SJoe Stringer * 229762369db2SQuentin Monnet * u64 bpf_skb_ancestor_cgroup_id(struct sk_buff *skb, int ancestor_level) 229862369db2SQuentin Monnet * Description 229962369db2SQuentin Monnet * Return id of cgroup v2 that is ancestor of cgroup associated 230062369db2SQuentin Monnet * with the *skb* at the *ancestor_level*. The root cgroup is at 230162369db2SQuentin Monnet * *ancestor_level* zero and each step down the hierarchy 230262369db2SQuentin Monnet * increments the level. If *ancestor_level* == level of cgroup 230362369db2SQuentin Monnet * associated with *skb*, then return value will be same as that 230462369db2SQuentin Monnet * of **bpf_skb_cgroup_id**\ (). 230562369db2SQuentin Monnet * 230662369db2SQuentin Monnet * The helper is useful to implement policies based on cgroups 230762369db2SQuentin Monnet * that are upper in hierarchy than immediate cgroup associated 230862369db2SQuentin Monnet * with *skb*. 230962369db2SQuentin Monnet * 231062369db2SQuentin Monnet * The format of returned id and helper limitations are same as in 231162369db2SQuentin Monnet * **bpf_skb_cgroup_id**\ (). 231262369db2SQuentin Monnet * Return 231362369db2SQuentin Monnet * The id is returned or 0 in case the id could not be retrieved. 231462369db2SQuentin Monnet * 2315f71c6143SJoe Stringer * struct bpf_sock *bpf_sk_lookup_tcp(void *ctx, struct bpf_sock_tuple *tuple, u32 tuple_size, u64 netns, u64 flags) 23166acc9b43SJoe Stringer * Description 23176acc9b43SJoe Stringer * Look for TCP socket matching *tuple*, optionally in a child 23186acc9b43SJoe Stringer * network namespace *netns*. The return value must be checked, 231990b1023fSQuentin Monnet * and if non-**NULL**, released via **bpf_sk_release**\ (). 23206acc9b43SJoe Stringer * 23216acc9b43SJoe Stringer * The *ctx* should point to the context of the program, such as 23226acc9b43SJoe Stringer * the skb or socket (depending on the hook in use). This is used 23236acc9b43SJoe Stringer * to determine the base network namespace for the lookup. 23246acc9b43SJoe Stringer * 23256acc9b43SJoe Stringer * *tuple_size* must be one of: 23266acc9b43SJoe Stringer * 23276acc9b43SJoe Stringer * **sizeof**\ (*tuple*\ **->ipv4**) 23286acc9b43SJoe Stringer * Look for an IPv4 socket. 23296acc9b43SJoe Stringer * **sizeof**\ (*tuple*\ **->ipv6**) 23306acc9b43SJoe Stringer * Look for an IPv6 socket. 23316acc9b43SJoe Stringer * 2332f71c6143SJoe Stringer * If the *netns* is a negative signed 32-bit integer, then the 2333f71c6143SJoe Stringer * socket lookup table in the netns associated with the *ctx* will 2334f71c6143SJoe Stringer * will be used. For the TC hooks, this is the netns of the device 2335f71c6143SJoe Stringer * in the skb. For socket hooks, this is the netns of the socket. 2336f71c6143SJoe Stringer * If *netns* is any other signed 32-bit value greater than or 2337f71c6143SJoe Stringer * equal to zero then it specifies the ID of the netns relative to 2338f71c6143SJoe Stringer * the netns associated with the *ctx*. *netns* values beyond the 2339f71c6143SJoe Stringer * range of 32-bit integers are reserved for future use. 23406acc9b43SJoe Stringer * 23416acc9b43SJoe Stringer * All values for *flags* are reserved for future usage, and must 23426acc9b43SJoe Stringer * be left at zero. 23436acc9b43SJoe Stringer * 23446acc9b43SJoe Stringer * This helper is available only if the kernel was compiled with 23456acc9b43SJoe Stringer * **CONFIG_NET** configuration option. 23466acc9b43SJoe Stringer * Return 23470bd72117SDaniel Borkmann * Pointer to **struct bpf_sock**, or **NULL** in case of failure. 23480bd72117SDaniel Borkmann * For sockets with reuseport option, the **struct bpf_sock** 234980867c5eSQuentin Monnet * result is from *reuse*\ **->socks**\ [] using the hash of the 235080867c5eSQuentin Monnet * tuple. 23516acc9b43SJoe Stringer * 2352f71c6143SJoe Stringer * struct bpf_sock *bpf_sk_lookup_udp(void *ctx, struct bpf_sock_tuple *tuple, u32 tuple_size, u64 netns, u64 flags) 23536acc9b43SJoe Stringer * Description 23546acc9b43SJoe Stringer * Look for UDP socket matching *tuple*, optionally in a child 23556acc9b43SJoe Stringer * network namespace *netns*. The return value must be checked, 235690b1023fSQuentin Monnet * and if non-**NULL**, released via **bpf_sk_release**\ (). 23576acc9b43SJoe Stringer * 23586acc9b43SJoe Stringer * The *ctx* should point to the context of the program, such as 23596acc9b43SJoe Stringer * the skb or socket (depending on the hook in use). This is used 23606acc9b43SJoe Stringer * to determine the base network namespace for the lookup. 23616acc9b43SJoe Stringer * 23626acc9b43SJoe Stringer * *tuple_size* must be one of: 23636acc9b43SJoe Stringer * 23646acc9b43SJoe Stringer * **sizeof**\ (*tuple*\ **->ipv4**) 23656acc9b43SJoe Stringer * Look for an IPv4 socket. 23666acc9b43SJoe Stringer * **sizeof**\ (*tuple*\ **->ipv6**) 23676acc9b43SJoe Stringer * Look for an IPv6 socket. 23686acc9b43SJoe Stringer * 2369f71c6143SJoe Stringer * If the *netns* is a negative signed 32-bit integer, then the 2370f71c6143SJoe Stringer * socket lookup table in the netns associated with the *ctx* will 2371f71c6143SJoe Stringer * will be used. For the TC hooks, this is the netns of the device 2372f71c6143SJoe Stringer * in the skb. For socket hooks, this is the netns of the socket. 2373f71c6143SJoe Stringer * If *netns* is any other signed 32-bit value greater than or 2374f71c6143SJoe Stringer * equal to zero then it specifies the ID of the netns relative to 2375f71c6143SJoe Stringer * the netns associated with the *ctx*. *netns* values beyond the 2376f71c6143SJoe Stringer * range of 32-bit integers are reserved for future use. 23776acc9b43SJoe Stringer * 23786acc9b43SJoe Stringer * All values for *flags* are reserved for future usage, and must 23796acc9b43SJoe Stringer * be left at zero. 23806acc9b43SJoe Stringer * 23816acc9b43SJoe Stringer * This helper is available only if the kernel was compiled with 23826acc9b43SJoe Stringer * **CONFIG_NET** configuration option. 23836acc9b43SJoe Stringer * Return 23840bd72117SDaniel Borkmann * Pointer to **struct bpf_sock**, or **NULL** in case of failure. 23850bd72117SDaniel Borkmann * For sockets with reuseport option, the **struct bpf_sock** 238680867c5eSQuentin Monnet * result is from *reuse*\ **->socks**\ [] using the hash of the 238780867c5eSQuentin Monnet * tuple. 23886acc9b43SJoe Stringer * 238990b1023fSQuentin Monnet * int bpf_sk_release(struct bpf_sock *sock) 23906acc9b43SJoe Stringer * Description 239190b1023fSQuentin Monnet * Release the reference held by *sock*. *sock* must be a 239290b1023fSQuentin Monnet * non-**NULL** pointer that was returned from 239390b1023fSQuentin Monnet * **bpf_sk_lookup_xxx**\ (). 239490b1023fSQuentin Monnet * Return 239590b1023fSQuentin Monnet * 0 on success, or a negative error in case of failure. 239690b1023fSQuentin Monnet * 239762369db2SQuentin Monnet * int bpf_map_push_elem(struct bpf_map *map, const void *value, u64 flags) 239862369db2SQuentin Monnet * Description 239962369db2SQuentin Monnet * Push an element *value* in *map*. *flags* is one of: 240062369db2SQuentin Monnet * 240162369db2SQuentin Monnet * **BPF_EXIST** 240262369db2SQuentin Monnet * If the queue/stack is full, the oldest element is 240362369db2SQuentin Monnet * removed to make room for this. 240462369db2SQuentin Monnet * Return 240562369db2SQuentin Monnet * 0 on success, or a negative error in case of failure. 240662369db2SQuentin Monnet * 240790b1023fSQuentin Monnet * int bpf_map_pop_elem(struct bpf_map *map, void *value) 240890b1023fSQuentin Monnet * Description 240990b1023fSQuentin Monnet * Pop an element from *map*. 241090b1023fSQuentin Monnet * Return 241190b1023fSQuentin Monnet * 0 on success, or a negative error in case of failure. 241290b1023fSQuentin Monnet * 241390b1023fSQuentin Monnet * int bpf_map_peek_elem(struct bpf_map *map, void *value) 241490b1023fSQuentin Monnet * Description 241590b1023fSQuentin Monnet * Get an element from *map* without removing it. 24166acc9b43SJoe Stringer * Return 24176acc9b43SJoe Stringer * 0 on success, or a negative error in case of failure. 24186fff607eSJohn Fastabend * 24195f0e5412SAndrii Nakryiko * int bpf_msg_push_data(struct sk_msg_buff *msg, u32 start, u32 len, u64 flags) 24206fff607eSJohn Fastabend * Description 242190b1023fSQuentin Monnet * For socket policies, insert *len* bytes into *msg* at offset 24226fff607eSJohn Fastabend * *start*. 24236fff607eSJohn Fastabend * 24246fff607eSJohn Fastabend * If a program of type **BPF_PROG_TYPE_SK_MSG** is run on a 242590b1023fSQuentin Monnet * *msg* it may want to insert metadata or options into the *msg*. 24266fff607eSJohn Fastabend * This can later be read and used by any of the lower layer BPF 24276fff607eSJohn Fastabend * hooks. 24286fff607eSJohn Fastabend * 24296fff607eSJohn Fastabend * This helper may fail if under memory pressure (a malloc 24306fff607eSJohn Fastabend * fails) in these cases BPF programs will get an appropriate 24316fff607eSJohn Fastabend * error and BPF programs will need to handle them. 24326fff607eSJohn Fastabend * Return 24336fff607eSJohn Fastabend * 0 on success, or a negative error in case of failure. 24347246d8edSJohn Fastabend * 24355f0e5412SAndrii Nakryiko * int bpf_msg_pop_data(struct sk_msg_buff *msg, u32 start, u32 len, u64 flags) 24367246d8edSJohn Fastabend * Description 24375f0e5412SAndrii Nakryiko * Will remove *len* bytes from a *msg* starting at byte *start*. 24387246d8edSJohn Fastabend * This may result in **ENOMEM** errors under certain situations if 24397246d8edSJohn Fastabend * an allocation and copy are required due to a full ring buffer. 24407246d8edSJohn Fastabend * However, the helper will try to avoid doing the allocation 24417246d8edSJohn Fastabend * if possible. Other errors can occur if input parameters are 244290b1023fSQuentin Monnet * invalid either due to *start* byte not being valid part of *msg* 24437246d8edSJohn Fastabend * payload and/or *pop* value being to large. 24447246d8edSJohn Fastabend * Return 244590b1023fSQuentin Monnet * 0 on success, or a negative error in case of failure. 244601d3240aSSean Young * 244701d3240aSSean Young * int bpf_rc_pointer_rel(void *ctx, s32 rel_x, s32 rel_y) 244801d3240aSSean Young * Description 244901d3240aSSean Young * This helper is used in programs implementing IR decoding, to 245001d3240aSSean Young * report a successfully decoded pointer movement. 245101d3240aSSean Young * 245201d3240aSSean Young * The *ctx* should point to the lirc sample as passed into 245301d3240aSSean Young * the program. 245401d3240aSSean Young * 245501d3240aSSean Young * This helper is only available is the kernel was compiled with 245601d3240aSSean Young * the **CONFIG_BPF_LIRC_MODE2** configuration option set to 245701d3240aSSean Young * "**y**". 245801d3240aSSean Young * Return 245901d3240aSSean Young * 0 246046f8bc92SMartin KaFai Lau * 24610eb09785SQuentin Monnet * int bpf_spin_lock(struct bpf_spin_lock *lock) 24620eb09785SQuentin Monnet * Description 24630eb09785SQuentin Monnet * Acquire a spinlock represented by the pointer *lock*, which is 24640eb09785SQuentin Monnet * stored as part of a value of a map. Taking the lock allows to 24650eb09785SQuentin Monnet * safely update the rest of the fields in that value. The 24660eb09785SQuentin Monnet * spinlock can (and must) later be released with a call to 24670eb09785SQuentin Monnet * **bpf_spin_unlock**\ (\ *lock*\ ). 24680eb09785SQuentin Monnet * 24690eb09785SQuentin Monnet * Spinlocks in BPF programs come with a number of restrictions 24700eb09785SQuentin Monnet * and constraints: 24710eb09785SQuentin Monnet * 24720eb09785SQuentin Monnet * * **bpf_spin_lock** objects are only allowed inside maps of 24730eb09785SQuentin Monnet * types **BPF_MAP_TYPE_HASH** and **BPF_MAP_TYPE_ARRAY** (this 24740eb09785SQuentin Monnet * list could be extended in the future). 24750eb09785SQuentin Monnet * * BTF description of the map is mandatory. 24760eb09785SQuentin Monnet * * The BPF program can take ONE lock at a time, since taking two 24770eb09785SQuentin Monnet * or more could cause dead locks. 24780eb09785SQuentin Monnet * * Only one **struct bpf_spin_lock** is allowed per map element. 24790eb09785SQuentin Monnet * * When the lock is taken, calls (either BPF to BPF or helpers) 24800eb09785SQuentin Monnet * are not allowed. 24810eb09785SQuentin Monnet * * The **BPF_LD_ABS** and **BPF_LD_IND** instructions are not 24820eb09785SQuentin Monnet * allowed inside a spinlock-ed region. 24830eb09785SQuentin Monnet * * The BPF program MUST call **bpf_spin_unlock**\ () to release 24840eb09785SQuentin Monnet * the lock, on all execution paths, before it returns. 24850eb09785SQuentin Monnet * * The BPF program can access **struct bpf_spin_lock** only via 24860eb09785SQuentin Monnet * the **bpf_spin_lock**\ () and **bpf_spin_unlock**\ () 24870eb09785SQuentin Monnet * helpers. Loading or storing data into the **struct 24880eb09785SQuentin Monnet * bpf_spin_lock** *lock*\ **;** field of a map is not allowed. 24890eb09785SQuentin Monnet * * To use the **bpf_spin_lock**\ () helper, the BTF description 24900eb09785SQuentin Monnet * of the map value must be a struct and have **struct 24910eb09785SQuentin Monnet * bpf_spin_lock** *anyname*\ **;** field at the top level. 24920eb09785SQuentin Monnet * Nested lock inside another struct is not allowed. 24930eb09785SQuentin Monnet * * The **struct bpf_spin_lock** *lock* field in a map value must 24940eb09785SQuentin Monnet * be aligned on a multiple of 4 bytes in that value. 24950eb09785SQuentin Monnet * * Syscall with command **BPF_MAP_LOOKUP_ELEM** does not copy 24960eb09785SQuentin Monnet * the **bpf_spin_lock** field to user space. 24970eb09785SQuentin Monnet * * Syscall with command **BPF_MAP_UPDATE_ELEM**, or update from 24980eb09785SQuentin Monnet * a BPF program, do not update the **bpf_spin_lock** field. 24990eb09785SQuentin Monnet * * **bpf_spin_lock** cannot be on the stack or inside a 25000eb09785SQuentin Monnet * networking packet (it can only be inside of a map values). 25010eb09785SQuentin Monnet * * **bpf_spin_lock** is available to root only. 25020eb09785SQuentin Monnet * * Tracing programs and socket filter programs cannot use 25030eb09785SQuentin Monnet * **bpf_spin_lock**\ () due to insufficient preemption checks 25040eb09785SQuentin Monnet * (but this may change in the future). 25050eb09785SQuentin Monnet * * **bpf_spin_lock** is not allowed in inner maps of map-in-map. 25060eb09785SQuentin Monnet * Return 25070eb09785SQuentin Monnet * 0 25080eb09785SQuentin Monnet * 25090eb09785SQuentin Monnet * int bpf_spin_unlock(struct bpf_spin_lock *lock) 25100eb09785SQuentin Monnet * Description 25110eb09785SQuentin Monnet * Release the *lock* previously locked by a call to 25120eb09785SQuentin Monnet * **bpf_spin_lock**\ (\ *lock*\ ). 25130eb09785SQuentin Monnet * Return 25140eb09785SQuentin Monnet * 0 25150eb09785SQuentin Monnet * 251646f8bc92SMartin KaFai Lau * struct bpf_sock *bpf_sk_fullsock(struct bpf_sock *sk) 251746f8bc92SMartin KaFai Lau * Description 251846f8bc92SMartin KaFai Lau * This helper gets a **struct bpf_sock** pointer such 251962369db2SQuentin Monnet * that all the fields in this **bpf_sock** can be accessed. 252046f8bc92SMartin KaFai Lau * Return 252162369db2SQuentin Monnet * A **struct bpf_sock** pointer on success, or **NULL** in 252246f8bc92SMartin KaFai Lau * case of failure. 2523655a51e5SMartin KaFai Lau * 2524655a51e5SMartin KaFai Lau * struct bpf_tcp_sock *bpf_tcp_sock(struct bpf_sock *sk) 2525655a51e5SMartin KaFai Lau * Description 2526655a51e5SMartin KaFai Lau * This helper gets a **struct bpf_tcp_sock** pointer from a 2527655a51e5SMartin KaFai Lau * **struct bpf_sock** pointer. 2528655a51e5SMartin KaFai Lau * Return 252962369db2SQuentin Monnet * A **struct bpf_tcp_sock** pointer on success, or **NULL** in 2530655a51e5SMartin KaFai Lau * case of failure. 2531f7c917baSbrakmo * 25325f0e5412SAndrii Nakryiko * int bpf_skb_ecn_set_ce(struct sk_buff *skb) 2533f7c917baSbrakmo * Description 253462369db2SQuentin Monnet * Set ECN (Explicit Congestion Notification) field of IP header 253562369db2SQuentin Monnet * to **CE** (Congestion Encountered) if current value is **ECT** 253662369db2SQuentin Monnet * (ECN Capable Transport). Otherwise, do nothing. Works with IPv6 253762369db2SQuentin Monnet * and IPv4. 2538f7c917baSbrakmo * Return 253962369db2SQuentin Monnet * 1 if the **CE** flag is set (either by the current helper call 254062369db2SQuentin Monnet * or because it was already present), 0 if it is not set. 2541dbafd7ddSMartin KaFai Lau * 2542dbafd7ddSMartin KaFai Lau * struct bpf_sock *bpf_get_listener_sock(struct bpf_sock *sk) 2543dbafd7ddSMartin KaFai Lau * Description 254462369db2SQuentin Monnet * Return a **struct bpf_sock** pointer in **TCP_LISTEN** state. 254562369db2SQuentin Monnet * **bpf_sk_release**\ () is unnecessary and not allowed. 2546dbafd7ddSMartin KaFai Lau * Return 254762369db2SQuentin Monnet * A **struct bpf_sock** pointer on success, or **NULL** in 2548dbafd7ddSMartin KaFai Lau * case of failure. 2549edbf8c01SLorenz Bauer * 2550edbf8c01SLorenz Bauer * struct bpf_sock *bpf_skc_lookup_tcp(void *ctx, struct bpf_sock_tuple *tuple, u32 tuple_size, u64 netns, u64 flags) 2551edbf8c01SLorenz Bauer * Description 2552edbf8c01SLorenz Bauer * Look for TCP socket matching *tuple*, optionally in a child 2553edbf8c01SLorenz Bauer * network namespace *netns*. The return value must be checked, 2554edbf8c01SLorenz Bauer * and if non-**NULL**, released via **bpf_sk_release**\ (). 2555edbf8c01SLorenz Bauer * 255680867c5eSQuentin Monnet * This function is identical to **bpf_sk_lookup_tcp**\ (), except 255780867c5eSQuentin Monnet * that it also returns timewait or request sockets. Use 255880867c5eSQuentin Monnet * **bpf_sk_fullsock**\ () or **bpf_tcp_sock**\ () to access the 255980867c5eSQuentin Monnet * full structure. 2560edbf8c01SLorenz Bauer * 2561edbf8c01SLorenz Bauer * This helper is available only if the kernel was compiled with 2562edbf8c01SLorenz Bauer * **CONFIG_NET** configuration option. 2563edbf8c01SLorenz Bauer * Return 2564edbf8c01SLorenz Bauer * Pointer to **struct bpf_sock**, or **NULL** in case of failure. 2565edbf8c01SLorenz Bauer * For sockets with reuseport option, the **struct bpf_sock** 256680867c5eSQuentin Monnet * result is from *reuse*\ **->socks**\ [] using the hash of the 256780867c5eSQuentin Monnet * tuple. 256839904084SLorenz Bauer * 256939904084SLorenz Bauer * int bpf_tcp_check_syncookie(struct bpf_sock *sk, void *iph, u32 iph_len, struct tcphdr *th, u32 th_len) 257039904084SLorenz Bauer * Description 257180867c5eSQuentin Monnet * Check whether *iph* and *th* contain a valid SYN cookie ACK for 257280867c5eSQuentin Monnet * the listening socket in *sk*. 257339904084SLorenz Bauer * 257480867c5eSQuentin Monnet * *iph* points to the start of the IPv4 or IPv6 header, while 257580867c5eSQuentin Monnet * *iph_len* contains **sizeof**\ (**struct iphdr**) or 257680867c5eSQuentin Monnet * **sizeof**\ (**struct ip6hdr**). 257739904084SLorenz Bauer * 257880867c5eSQuentin Monnet * *th* points to the start of the TCP header, while *th_len* 257980867c5eSQuentin Monnet * contains **sizeof**\ (**struct tcphdr**). 258039904084SLorenz Bauer * 258139904084SLorenz Bauer * Return 258280867c5eSQuentin Monnet * 0 if *iph* and *th* are a valid SYN cookie ACK, or a negative 258380867c5eSQuentin Monnet * error otherwise. 2584808649fbSAndrey Ignatov * 2585808649fbSAndrey Ignatov * int bpf_sysctl_get_name(struct bpf_sysctl *ctx, char *buf, size_t buf_len, u64 flags) 2586808649fbSAndrey Ignatov * Description 2587808649fbSAndrey Ignatov * Get name of sysctl in /proc/sys/ and copy it into provided by 2588808649fbSAndrey Ignatov * program buffer *buf* of size *buf_len*. 2589808649fbSAndrey Ignatov * 2590808649fbSAndrey Ignatov * The buffer is always NUL terminated, unless it's zero-sized. 2591808649fbSAndrey Ignatov * 2592808649fbSAndrey Ignatov * If *flags* is zero, full name (e.g. "net/ipv4/tcp_mem") is 2593808649fbSAndrey Ignatov * copied. Use **BPF_F_SYSCTL_BASE_NAME** flag to copy base name 2594808649fbSAndrey Ignatov * only (e.g. "tcp_mem"). 2595808649fbSAndrey Ignatov * Return 2596808649fbSAndrey Ignatov * Number of character copied (not including the trailing NUL). 2597808649fbSAndrey Ignatov * 2598808649fbSAndrey Ignatov * **-E2BIG** if the buffer wasn't big enough (*buf* will contain 2599808649fbSAndrey Ignatov * truncated name in this case). 26001d11b301SAndrey Ignatov * 26011d11b301SAndrey Ignatov * int bpf_sysctl_get_current_value(struct bpf_sysctl *ctx, char *buf, size_t buf_len) 26021d11b301SAndrey Ignatov * Description 26031d11b301SAndrey Ignatov * Get current value of sysctl as it is presented in /proc/sys 26041d11b301SAndrey Ignatov * (incl. newline, etc), and copy it as a string into provided 26051d11b301SAndrey Ignatov * by program buffer *buf* of size *buf_len*. 26061d11b301SAndrey Ignatov * 26071d11b301SAndrey Ignatov * The whole value is copied, no matter what file position user 26081d11b301SAndrey Ignatov * space issued e.g. sys_read at. 26091d11b301SAndrey Ignatov * 26101d11b301SAndrey Ignatov * The buffer is always NUL terminated, unless it's zero-sized. 26111d11b301SAndrey Ignatov * Return 26121d11b301SAndrey Ignatov * Number of character copied (not including the trailing NUL). 26131d11b301SAndrey Ignatov * 26141d11b301SAndrey Ignatov * **-E2BIG** if the buffer wasn't big enough (*buf* will contain 26151d11b301SAndrey Ignatov * truncated name in this case). 26161d11b301SAndrey Ignatov * 26171d11b301SAndrey Ignatov * **-EINVAL** if current value was unavailable, e.g. because 26181d11b301SAndrey Ignatov * sysctl is uninitialized and read returns -EIO for it. 26194e63acdfSAndrey Ignatov * 26204e63acdfSAndrey Ignatov * int bpf_sysctl_get_new_value(struct bpf_sysctl *ctx, char *buf, size_t buf_len) 26214e63acdfSAndrey Ignatov * Description 26224e63acdfSAndrey Ignatov * Get new value being written by user space to sysctl (before 26234e63acdfSAndrey Ignatov * the actual write happens) and copy it as a string into 26244e63acdfSAndrey Ignatov * provided by program buffer *buf* of size *buf_len*. 26254e63acdfSAndrey Ignatov * 26264e63acdfSAndrey Ignatov * User space may write new value at file position > 0. 26274e63acdfSAndrey Ignatov * 26284e63acdfSAndrey Ignatov * The buffer is always NUL terminated, unless it's zero-sized. 26294e63acdfSAndrey Ignatov * Return 26304e63acdfSAndrey Ignatov * Number of character copied (not including the trailing NUL). 26314e63acdfSAndrey Ignatov * 26324e63acdfSAndrey Ignatov * **-E2BIG** if the buffer wasn't big enough (*buf* will contain 26334e63acdfSAndrey Ignatov * truncated name in this case). 26344e63acdfSAndrey Ignatov * 26354e63acdfSAndrey Ignatov * **-EINVAL** if sysctl is being read. 26364e63acdfSAndrey Ignatov * 26374e63acdfSAndrey Ignatov * int bpf_sysctl_set_new_value(struct bpf_sysctl *ctx, const char *buf, size_t buf_len) 26384e63acdfSAndrey Ignatov * Description 26394e63acdfSAndrey Ignatov * Override new value being written by user space to sysctl with 26404e63acdfSAndrey Ignatov * value provided by program in buffer *buf* of size *buf_len*. 26414e63acdfSAndrey Ignatov * 26424e63acdfSAndrey Ignatov * *buf* should contain a string in same form as provided by user 26434e63acdfSAndrey Ignatov * space on sysctl write. 26444e63acdfSAndrey Ignatov * 26454e63acdfSAndrey Ignatov * User space may write new value at file position > 0. To override 26464e63acdfSAndrey Ignatov * the whole sysctl value file position should be set to zero. 26474e63acdfSAndrey Ignatov * Return 26484e63acdfSAndrey Ignatov * 0 on success. 26494e63acdfSAndrey Ignatov * 26504e63acdfSAndrey Ignatov * **-E2BIG** if the *buf_len* is too big. 26514e63acdfSAndrey Ignatov * 26524e63acdfSAndrey Ignatov * **-EINVAL** if sysctl is being read. 2653d7a4cb9bSAndrey Ignatov * 2654d7a4cb9bSAndrey Ignatov * int bpf_strtol(const char *buf, size_t buf_len, u64 flags, long *res) 2655d7a4cb9bSAndrey Ignatov * Description 2656d7a4cb9bSAndrey Ignatov * Convert the initial part of the string from buffer *buf* of 2657d7a4cb9bSAndrey Ignatov * size *buf_len* to a long integer according to the given base 2658d7a4cb9bSAndrey Ignatov * and save the result in *res*. 2659d7a4cb9bSAndrey Ignatov * 2660d7a4cb9bSAndrey Ignatov * The string may begin with an arbitrary amount of white space 266180867c5eSQuentin Monnet * (as determined by **isspace**\ (3)) followed by a single 266280867c5eSQuentin Monnet * optional '**-**' sign. 2663d7a4cb9bSAndrey Ignatov * 2664d7a4cb9bSAndrey Ignatov * Five least significant bits of *flags* encode base, other bits 2665d7a4cb9bSAndrey Ignatov * are currently unused. 2666d7a4cb9bSAndrey Ignatov * 2667d7a4cb9bSAndrey Ignatov * Base must be either 8, 10, 16 or 0 to detect it automatically 266880867c5eSQuentin Monnet * similar to user space **strtol**\ (3). 2669d7a4cb9bSAndrey Ignatov * Return 2670d7a4cb9bSAndrey Ignatov * Number of characters consumed on success. Must be positive but 267180867c5eSQuentin Monnet * no more than *buf_len*. 2672d7a4cb9bSAndrey Ignatov * 2673d7a4cb9bSAndrey Ignatov * **-EINVAL** if no valid digits were found or unsupported base 2674d7a4cb9bSAndrey Ignatov * was provided. 2675d7a4cb9bSAndrey Ignatov * 2676d7a4cb9bSAndrey Ignatov * **-ERANGE** if resulting value was out of range. 2677d7a4cb9bSAndrey Ignatov * 2678d7a4cb9bSAndrey Ignatov * int bpf_strtoul(const char *buf, size_t buf_len, u64 flags, unsigned long *res) 2679d7a4cb9bSAndrey Ignatov * Description 2680d7a4cb9bSAndrey Ignatov * Convert the initial part of the string from buffer *buf* of 2681d7a4cb9bSAndrey Ignatov * size *buf_len* to an unsigned long integer according to the 2682d7a4cb9bSAndrey Ignatov * given base and save the result in *res*. 2683d7a4cb9bSAndrey Ignatov * 2684d7a4cb9bSAndrey Ignatov * The string may begin with an arbitrary amount of white space 268580867c5eSQuentin Monnet * (as determined by **isspace**\ (3)). 2686d7a4cb9bSAndrey Ignatov * 2687d7a4cb9bSAndrey Ignatov * Five least significant bits of *flags* encode base, other bits 2688d7a4cb9bSAndrey Ignatov * are currently unused. 2689d7a4cb9bSAndrey Ignatov * 2690d7a4cb9bSAndrey Ignatov * Base must be either 8, 10, 16 or 0 to detect it automatically 269180867c5eSQuentin Monnet * similar to user space **strtoul**\ (3). 2692d7a4cb9bSAndrey Ignatov * Return 2693d7a4cb9bSAndrey Ignatov * Number of characters consumed on success. Must be positive but 269480867c5eSQuentin Monnet * no more than *buf_len*. 2695d7a4cb9bSAndrey Ignatov * 2696d7a4cb9bSAndrey Ignatov * **-EINVAL** if no valid digits were found or unsupported base 2697d7a4cb9bSAndrey Ignatov * was provided. 2698d7a4cb9bSAndrey Ignatov * 2699d7a4cb9bSAndrey Ignatov * **-ERANGE** if resulting value was out of range. 27006ac99e8fSMartin KaFai Lau * 27016ac99e8fSMartin KaFai Lau * void *bpf_sk_storage_get(struct bpf_map *map, struct bpf_sock *sk, void *value, u64 flags) 27026ac99e8fSMartin KaFai Lau * Description 270380867c5eSQuentin Monnet * Get a bpf-local-storage from a *sk*. 27046ac99e8fSMartin KaFai Lau * 27056ac99e8fSMartin KaFai Lau * Logically, it could be thought of getting the value from 27066ac99e8fSMartin KaFai Lau * a *map* with *sk* as the **key**. From this 27076ac99e8fSMartin KaFai Lau * perspective, the usage is not much different from 270880867c5eSQuentin Monnet * **bpf_map_lookup_elem**\ (*map*, **&**\ *sk*) except this 270980867c5eSQuentin Monnet * helper enforces the key must be a full socket and the map must 271080867c5eSQuentin Monnet * be a **BPF_MAP_TYPE_SK_STORAGE** also. 27116ac99e8fSMartin KaFai Lau * 27126ac99e8fSMartin KaFai Lau * Underneath, the value is stored locally at *sk* instead of 271380867c5eSQuentin Monnet * the *map*. The *map* is used as the bpf-local-storage 271480867c5eSQuentin Monnet * "type". The bpf-local-storage "type" (i.e. the *map*) is 271580867c5eSQuentin Monnet * searched against all bpf-local-storages residing at *sk*. 27166ac99e8fSMartin KaFai Lau * 271780867c5eSQuentin Monnet * An optional *flags* (**BPF_SK_STORAGE_GET_F_CREATE**) can be 27186ac99e8fSMartin KaFai Lau * used such that a new bpf-local-storage will be 27196ac99e8fSMartin KaFai Lau * created if one does not exist. *value* can be used 272080867c5eSQuentin Monnet * together with **BPF_SK_STORAGE_GET_F_CREATE** to specify 27216ac99e8fSMartin KaFai Lau * the initial value of a bpf-local-storage. If *value* is 272280867c5eSQuentin Monnet * **NULL**, the new bpf-local-storage will be zero initialized. 27236ac99e8fSMartin KaFai Lau * Return 27246ac99e8fSMartin KaFai Lau * A bpf-local-storage pointer is returned on success. 27256ac99e8fSMartin KaFai Lau * 27266ac99e8fSMartin KaFai Lau * **NULL** if not found or there was an error in adding 27276ac99e8fSMartin KaFai Lau * a new bpf-local-storage. 27286ac99e8fSMartin KaFai Lau * 27296ac99e8fSMartin KaFai Lau * int bpf_sk_storage_delete(struct bpf_map *map, struct bpf_sock *sk) 27306ac99e8fSMartin KaFai Lau * Description 273180867c5eSQuentin Monnet * Delete a bpf-local-storage from a *sk*. 27326ac99e8fSMartin KaFai Lau * Return 27336ac99e8fSMartin KaFai Lau * 0 on success. 27346ac99e8fSMartin KaFai Lau * 27356ac99e8fSMartin KaFai Lau * **-ENOENT** if the bpf-local-storage cannot be found. 27368b401f9eSYonghong Song * 27378b401f9eSYonghong Song * int bpf_send_signal(u32 sig) 27388b401f9eSYonghong Song * Description 27398482941fSYonghong Song * Send signal *sig* to the process of the current task. 27408482941fSYonghong Song * The signal may be delivered to any of this process's threads. 27418b401f9eSYonghong Song * Return 27428b401f9eSYonghong Song * 0 on success or successfully queued. 27438b401f9eSYonghong Song * 27448b401f9eSYonghong Song * **-EBUSY** if work queue under nmi is full. 27458b401f9eSYonghong Song * 27468b401f9eSYonghong Song * **-EINVAL** if *sig* is invalid. 27478b401f9eSYonghong Song * 27488b401f9eSYonghong Song * **-EPERM** if no permission to send the *sig*. 27498b401f9eSYonghong Song * 27508b401f9eSYonghong Song * **-EAGAIN** if bpf program can try again. 275170d66244SPetar Penkov * 275270d66244SPetar Penkov * s64 bpf_tcp_gen_syncookie(struct bpf_sock *sk, void *iph, u32 iph_len, struct tcphdr *th, u32 th_len) 275370d66244SPetar Penkov * Description 275470d66244SPetar Penkov * Try to issue a SYN cookie for the packet with corresponding 275570d66244SPetar Penkov * IP/TCP headers, *iph* and *th*, on the listening socket in *sk*. 275670d66244SPetar Penkov * 275770d66244SPetar Penkov * *iph* points to the start of the IPv4 or IPv6 header, while 275870d66244SPetar Penkov * *iph_len* contains **sizeof**\ (**struct iphdr**) or 275970d66244SPetar Penkov * **sizeof**\ (**struct ip6hdr**). 276070d66244SPetar Penkov * 276170d66244SPetar Penkov * *th* points to the start of the TCP header, while *th_len* 276270d66244SPetar Penkov * contains the length of the TCP header. 276370d66244SPetar Penkov * 276470d66244SPetar Penkov * Return 276570d66244SPetar Penkov * On success, lower 32 bits hold the generated SYN cookie in 276670d66244SPetar Penkov * followed by 16 bits which hold the MSS value for that cookie, 276770d66244SPetar Penkov * and the top 16 bits are unused. 276870d66244SPetar Penkov * 276970d66244SPetar Penkov * On failure, the returned value is one of the following: 277070d66244SPetar Penkov * 277170d66244SPetar Penkov * **-EINVAL** SYN cookie cannot be issued due to error 277270d66244SPetar Penkov * 277370d66244SPetar Penkov * **-ENOENT** SYN cookie should not be issued (no SYN flood) 277470d66244SPetar Penkov * 277570d66244SPetar Penkov * **-EOPNOTSUPP** kernel configuration does not enable SYN cookies 277670d66244SPetar Penkov * 277770d66244SPetar Penkov * **-EPROTONOSUPPORT** IP packet version is not 4 or 6 2778a7658e1aSAlexei Starovoitov * 2779a7658e1aSAlexei Starovoitov * int bpf_skb_output(void *ctx, struct bpf_map *map, u64 flags, void *data, u64 size) 2780a7658e1aSAlexei Starovoitov * Description 2781a7658e1aSAlexei Starovoitov * Write raw *data* blob into a special BPF perf event held by 2782a7658e1aSAlexei Starovoitov * *map* of type **BPF_MAP_TYPE_PERF_EVENT_ARRAY**. This perf 2783a7658e1aSAlexei Starovoitov * event must have the following attributes: **PERF_SAMPLE_RAW** 2784a7658e1aSAlexei Starovoitov * as **sample_type**, **PERF_TYPE_SOFTWARE** as **type**, and 2785a7658e1aSAlexei Starovoitov * **PERF_COUNT_SW_BPF_OUTPUT** as **config**. 2786a7658e1aSAlexei Starovoitov * 2787a7658e1aSAlexei Starovoitov * The *flags* are used to indicate the index in *map* for which 2788a7658e1aSAlexei Starovoitov * the value must be put, masked with **BPF_F_INDEX_MASK**. 2789a7658e1aSAlexei Starovoitov * Alternatively, *flags* can be set to **BPF_F_CURRENT_CPU** 2790a7658e1aSAlexei Starovoitov * to indicate that the index of the current CPU core should be 2791a7658e1aSAlexei Starovoitov * used. 2792a7658e1aSAlexei Starovoitov * 2793a7658e1aSAlexei Starovoitov * The value to write, of *size*, is passed through eBPF stack and 2794a7658e1aSAlexei Starovoitov * pointed by *data*. 2795a7658e1aSAlexei Starovoitov * 2796a7658e1aSAlexei Starovoitov * *ctx* is a pointer to in-kernel struct sk_buff. 2797a7658e1aSAlexei Starovoitov * 2798a7658e1aSAlexei Starovoitov * This helper is similar to **bpf_perf_event_output**\ () but 2799a7658e1aSAlexei Starovoitov * restricted to raw_tracepoint bpf programs. 2800a7658e1aSAlexei Starovoitov * Return 2801a7658e1aSAlexei Starovoitov * 0 on success, or a negative error in case of failure. 28026ae08ae3SDaniel Borkmann * 28036ae08ae3SDaniel Borkmann * int bpf_probe_read_user(void *dst, u32 size, const void *unsafe_ptr) 28046ae08ae3SDaniel Borkmann * Description 28056ae08ae3SDaniel Borkmann * Safely attempt to read *size* bytes from user space address 28066ae08ae3SDaniel Borkmann * *unsafe_ptr* and store the data in *dst*. 28076ae08ae3SDaniel Borkmann * Return 28086ae08ae3SDaniel Borkmann * 0 on success, or a negative error in case of failure. 28096ae08ae3SDaniel Borkmann * 28106ae08ae3SDaniel Borkmann * int bpf_probe_read_kernel(void *dst, u32 size, const void *unsafe_ptr) 28116ae08ae3SDaniel Borkmann * Description 28126ae08ae3SDaniel Borkmann * Safely attempt to read *size* bytes from kernel space address 28136ae08ae3SDaniel Borkmann * *unsafe_ptr* and store the data in *dst*. 28146ae08ae3SDaniel Borkmann * Return 28156ae08ae3SDaniel Borkmann * 0 on success, or a negative error in case of failure. 28166ae08ae3SDaniel Borkmann * 28176ae08ae3SDaniel Borkmann * int bpf_probe_read_user_str(void *dst, u32 size, const void *unsafe_ptr) 28186ae08ae3SDaniel Borkmann * Description 28196ae08ae3SDaniel Borkmann * Copy a NUL terminated string from an unsafe user address 28206ae08ae3SDaniel Borkmann * *unsafe_ptr* to *dst*. The *size* should include the 28216ae08ae3SDaniel Borkmann * terminating NUL byte. In case the string length is smaller than 28226ae08ae3SDaniel Borkmann * *size*, the target is not padded with further NUL bytes. If the 28236ae08ae3SDaniel Borkmann * string length is larger than *size*, just *size*-1 bytes are 28246ae08ae3SDaniel Borkmann * copied and the last byte is set to NUL. 28256ae08ae3SDaniel Borkmann * 28266ae08ae3SDaniel Borkmann * On success, the length of the copied string is returned. This 28276ae08ae3SDaniel Borkmann * makes this helper useful in tracing programs for reading 28286ae08ae3SDaniel Borkmann * strings, and more importantly to get its length at runtime. See 28296ae08ae3SDaniel Borkmann * the following snippet: 28306ae08ae3SDaniel Borkmann * 28316ae08ae3SDaniel Borkmann * :: 28326ae08ae3SDaniel Borkmann * 28336ae08ae3SDaniel Borkmann * SEC("kprobe/sys_open") 28346ae08ae3SDaniel Borkmann * void bpf_sys_open(struct pt_regs *ctx) 28356ae08ae3SDaniel Borkmann * { 28366ae08ae3SDaniel Borkmann * char buf[PATHLEN]; // PATHLEN is defined to 256 28376ae08ae3SDaniel Borkmann * int res = bpf_probe_read_user_str(buf, sizeof(buf), 28386ae08ae3SDaniel Borkmann * ctx->di); 28396ae08ae3SDaniel Borkmann * 28406ae08ae3SDaniel Borkmann * // Consume buf, for example push it to 28416ae08ae3SDaniel Borkmann * // userspace via bpf_perf_event_output(); we 28426ae08ae3SDaniel Borkmann * // can use res (the string length) as event 28436ae08ae3SDaniel Borkmann * // size, after checking its boundaries. 28446ae08ae3SDaniel Borkmann * } 28456ae08ae3SDaniel Borkmann * 28466ae08ae3SDaniel Borkmann * In comparison, using **bpf_probe_read_user()** helper here 28476ae08ae3SDaniel Borkmann * instead to read the string would require to estimate the length 28486ae08ae3SDaniel Borkmann * at compile time, and would often result in copying more memory 28496ae08ae3SDaniel Borkmann * than necessary. 28506ae08ae3SDaniel Borkmann * 28516ae08ae3SDaniel Borkmann * Another useful use case is when parsing individual process 28526ae08ae3SDaniel Borkmann * arguments or individual environment variables navigating 28536ae08ae3SDaniel Borkmann * *current*\ **->mm->arg_start** and *current*\ 28546ae08ae3SDaniel Borkmann * **->mm->env_start**: using this helper and the return value, 28556ae08ae3SDaniel Borkmann * one can quickly iterate at the right offset of the memory area. 28566ae08ae3SDaniel Borkmann * Return 28576ae08ae3SDaniel Borkmann * On success, the strictly positive length of the string, 28586ae08ae3SDaniel Borkmann * including the trailing NUL character. On error, a negative 28596ae08ae3SDaniel Borkmann * value. 28606ae08ae3SDaniel Borkmann * 28616ae08ae3SDaniel Borkmann * int bpf_probe_read_kernel_str(void *dst, u32 size, const void *unsafe_ptr) 28626ae08ae3SDaniel Borkmann * Description 28636ae08ae3SDaniel Borkmann * Copy a NUL terminated string from an unsafe kernel address *unsafe_ptr* 28646ae08ae3SDaniel Borkmann * to *dst*. Same semantics as with bpf_probe_read_user_str() apply. 28656ae08ae3SDaniel Borkmann * Return 28666ae08ae3SDaniel Borkmann * On success, the strictly positive length of the string, including 28676ae08ae3SDaniel Borkmann * the trailing NUL character. On error, a negative value. 2868206057feSMartin KaFai Lau * 2869206057feSMartin KaFai Lau * int bpf_tcp_send_ack(void *tp, u32 rcv_nxt) 2870206057feSMartin KaFai Lau * Description 2871206057feSMartin KaFai Lau * Send out a tcp-ack. *tp* is the in-kernel struct tcp_sock. 2872206057feSMartin KaFai Lau * *rcv_nxt* is the ack_seq to be sent out. 2873206057feSMartin KaFai Lau * Return 2874206057feSMartin KaFai Lau * 0 on success, or a negative error in case of failure. 2875206057feSMartin KaFai Lau * 28768482941fSYonghong Song * int bpf_send_signal_thread(u32 sig) 28778482941fSYonghong Song * Description 28788482941fSYonghong Song * Send signal *sig* to the thread corresponding to the current task. 28798482941fSYonghong Song * Return 28808482941fSYonghong Song * 0 on success or successfully queued. 28818482941fSYonghong Song * 28828482941fSYonghong Song * **-EBUSY** if work queue under nmi is full. 28838482941fSYonghong Song * 28848482941fSYonghong Song * **-EINVAL** if *sig* is invalid. 28858482941fSYonghong Song * 28868482941fSYonghong Song * **-EPERM** if no permission to send the *sig*. 28878482941fSYonghong Song * 28888482941fSYonghong Song * **-EAGAIN** if bpf program can try again. 28895576b991SMartin KaFai Lau * 28905576b991SMartin KaFai Lau * u64 bpf_jiffies64(void) 28915576b991SMartin KaFai Lau * Description 28925576b991SMartin KaFai Lau * Obtain the 64bit jiffies 28935576b991SMartin KaFai Lau * Return 28945576b991SMartin KaFai Lau * The 64 bit jiffies 2895*fff7b643SDaniel Xu * 2896*fff7b643SDaniel Xu * int bpf_read_branch_records(struct bpf_perf_event_data *ctx, void *buf, u32 size, u64 flags) 2897*fff7b643SDaniel Xu * Description 2898*fff7b643SDaniel Xu * For an eBPF program attached to a perf event, retrieve the 2899*fff7b643SDaniel Xu * branch records (struct perf_branch_entry) associated to *ctx* 2900*fff7b643SDaniel Xu * and store it in the buffer pointed by *buf* up to size 2901*fff7b643SDaniel Xu * *size* bytes. 2902*fff7b643SDaniel Xu * Return 2903*fff7b643SDaniel Xu * On success, number of bytes written to *buf*. On error, a 2904*fff7b643SDaniel Xu * negative value. 2905*fff7b643SDaniel Xu * 2906*fff7b643SDaniel Xu * The *flags* can be set to **BPF_F_GET_BRANCH_RECORDS_SIZE** to 2907*fff7b643SDaniel Xu * instead return the number of bytes required to store all the 2908*fff7b643SDaniel Xu * branch entries. If this flag is set, *buf* may be NULL. 2909*fff7b643SDaniel Xu * 2910*fff7b643SDaniel Xu * **-EINVAL** if arguments invalid or **size** not a multiple 2911*fff7b643SDaniel Xu * of sizeof(struct perf_branch_entry). 2912*fff7b643SDaniel Xu * 2913*fff7b643SDaniel Xu * **-ENOENT** if architecture does not support branch records. 29147a4b28c6SDaniel Borkmann */ 2915ebb676daSThomas Graf #define __BPF_FUNC_MAPPER(FN) \ 2916ebb676daSThomas Graf FN(unspec), \ 2917ebb676daSThomas Graf FN(map_lookup_elem), \ 2918ebb676daSThomas Graf FN(map_update_elem), \ 2919ebb676daSThomas Graf FN(map_delete_elem), \ 2920ebb676daSThomas Graf FN(probe_read), \ 2921ebb676daSThomas Graf FN(ktime_get_ns), \ 2922ebb676daSThomas Graf FN(trace_printk), \ 2923ebb676daSThomas Graf FN(get_prandom_u32), \ 2924ebb676daSThomas Graf FN(get_smp_processor_id), \ 2925ebb676daSThomas Graf FN(skb_store_bytes), \ 2926ebb676daSThomas Graf FN(l3_csum_replace), \ 2927ebb676daSThomas Graf FN(l4_csum_replace), \ 2928ebb676daSThomas Graf FN(tail_call), \ 2929ebb676daSThomas Graf FN(clone_redirect), \ 2930ebb676daSThomas Graf FN(get_current_pid_tgid), \ 2931ebb676daSThomas Graf FN(get_current_uid_gid), \ 2932ebb676daSThomas Graf FN(get_current_comm), \ 2933ebb676daSThomas Graf FN(get_cgroup_classid), \ 2934ebb676daSThomas Graf FN(skb_vlan_push), \ 2935ebb676daSThomas Graf FN(skb_vlan_pop), \ 2936ebb676daSThomas Graf FN(skb_get_tunnel_key), \ 2937ebb676daSThomas Graf FN(skb_set_tunnel_key), \ 2938ebb676daSThomas Graf FN(perf_event_read), \ 2939ebb676daSThomas Graf FN(redirect), \ 2940ebb676daSThomas Graf FN(get_route_realm), \ 2941ebb676daSThomas Graf FN(perf_event_output), \ 2942ebb676daSThomas Graf FN(skb_load_bytes), \ 2943ebb676daSThomas Graf FN(get_stackid), \ 2944ebb676daSThomas Graf FN(csum_diff), \ 2945ebb676daSThomas Graf FN(skb_get_tunnel_opt), \ 2946ebb676daSThomas Graf FN(skb_set_tunnel_opt), \ 2947ebb676daSThomas Graf FN(skb_change_proto), \ 2948ebb676daSThomas Graf FN(skb_change_type), \ 2949ebb676daSThomas Graf FN(skb_under_cgroup), \ 2950ebb676daSThomas Graf FN(get_hash_recalc), \ 2951ebb676daSThomas Graf FN(get_current_task), \ 2952ebb676daSThomas Graf FN(probe_write_user), \ 2953ebb676daSThomas Graf FN(current_task_under_cgroup), \ 2954ebb676daSThomas Graf FN(skb_change_tail), \ 2955ebb676daSThomas Graf FN(skb_pull_data), \ 2956ebb676daSThomas Graf FN(csum_update), \ 2957ebb676daSThomas Graf FN(set_hash_invalid), \ 29583a0af8fdSThomas Graf FN(get_numa_node_id), \ 295917bedab2SMartin KaFai Lau FN(skb_change_head), \ 2960a5e8c070SGianluca Borello FN(xdp_adjust_head), \ 296191b8270fSChenbo Feng FN(probe_read_str), \ 29626acc5c29SChenbo Feng FN(get_socket_cookie), \ 2963ded092cdSDaniel Borkmann FN(get_socket_uid), \ 29648c4b4c7eSLawrence Brakmo FN(set_hash), \ 29652be7e212SDaniel Borkmann FN(setsockopt), \ 296697f91a7cSJohn Fastabend FN(skb_adjust_room), \ 2967174a79ffSJohn Fastabend FN(redirect_map), \ 2968174a79ffSJohn Fastabend FN(sk_redirect_map), \ 2969174a79ffSJohn Fastabend FN(sock_map_update), \ 2970908432caSYonghong Song FN(xdp_adjust_meta), \ 29714bebdc7aSYonghong Song FN(perf_event_read_value), \ 2972cd86d1fdSLawrence Brakmo FN(perf_prog_read_value), \ 29739802d865SJosef Bacik FN(getsockopt), \ 2974b13d8807SLawrence Brakmo FN(override_return), \ 29754f738adbSJohn Fastabend FN(sock_ops_cb_flags_set), \ 29762a100317SJohn Fastabend FN(msg_redirect_map), \ 297791843d54SJohn Fastabend FN(msg_apply_bytes), \ 2978015632bbSJohn Fastabend FN(msg_cork_bytes), \ 2979d74bad4eSAndrey Ignatov FN(msg_pull_data), \ 2980b32cc5b9SNikita V. Shirokov FN(bind), \ 298112bed760SEyal Birger FN(xdp_adjust_tail), \ 2982c195651eSYonghong Song FN(skb_get_xfrm_state), \ 29834e1ec56cSDaniel Borkmann FN(get_stack), \ 298487f5fc7eSDavid Ahern FN(skb_load_bytes_relative), \ 298581110384SJohn Fastabend FN(fib_lookup), \ 298681110384SJohn Fastabend FN(sock_hash_update), \ 298781110384SJohn Fastabend FN(msg_redirect_hash), \ 2988fe94cc29SMathieu Xhonneux FN(sk_redirect_hash), \ 2989fe94cc29SMathieu Xhonneux FN(lwt_push_encap), \ 2990fe94cc29SMathieu Xhonneux FN(lwt_seg6_store_bytes), \ 2991fe94cc29SMathieu Xhonneux FN(lwt_seg6_adjust_srh), \ 2992f4364dcfSSean Young FN(lwt_seg6_action), \ 2993f4364dcfSSean Young FN(rc_repeat), \ 2994cb20b08eSDaniel Borkmann FN(rc_keydown), \ 2995bf6fa2c8SYonghong Song FN(skb_cgroup_id), \ 2996cd339431SRoman Gushchin FN(get_current_cgroup_id), \ 29972dbb9b9eSMartin KaFai Lau FN(get_local_storage), \ 299877236281SAndrey Ignatov FN(sk_select_reuseport), \ 29996acc9b43SJoe Stringer FN(skb_ancestor_cgroup_id), \ 30006acc9b43SJoe Stringer FN(sk_lookup_tcp), \ 30016acc9b43SJoe Stringer FN(sk_lookup_udp), \ 3002f1a2e44aSMauricio Vasquez B FN(sk_release), \ 3003f1a2e44aSMauricio Vasquez B FN(map_push_elem), \ 3004f1a2e44aSMauricio Vasquez B FN(map_pop_elem), \ 30056fff607eSJohn Fastabend FN(map_peek_elem), \ 30067246d8edSJohn Fastabend FN(msg_push_data), \ 300701d3240aSSean Young FN(msg_pop_data), \ 3008d83525caSAlexei Starovoitov FN(rc_pointer_rel), \ 3009d83525caSAlexei Starovoitov FN(spin_lock), \ 301046f8bc92SMartin KaFai Lau FN(spin_unlock), \ 3011655a51e5SMartin KaFai Lau FN(sk_fullsock), \ 3012f7c917baSbrakmo FN(tcp_sock), \ 3013dbafd7ddSMartin KaFai Lau FN(skb_ecn_set_ce), \ 3014edbf8c01SLorenz Bauer FN(get_listener_sock), \ 301539904084SLorenz Bauer FN(skc_lookup_tcp), \ 3016808649fbSAndrey Ignatov FN(tcp_check_syncookie), \ 30171d11b301SAndrey Ignatov FN(sysctl_get_name), \ 30184e63acdfSAndrey Ignatov FN(sysctl_get_current_value), \ 30194e63acdfSAndrey Ignatov FN(sysctl_get_new_value), \ 3020d7a4cb9bSAndrey Ignatov FN(sysctl_set_new_value), \ 3021d7a4cb9bSAndrey Ignatov FN(strtol), \ 30226ac99e8fSMartin KaFai Lau FN(strtoul), \ 30236ac99e8fSMartin KaFai Lau FN(sk_storage_get), \ 30248b401f9eSYonghong Song FN(sk_storage_delete), \ 302570d66244SPetar Penkov FN(send_signal), \ 3026a7658e1aSAlexei Starovoitov FN(tcp_gen_syncookie), \ 30276ae08ae3SDaniel Borkmann FN(skb_output), \ 30286ae08ae3SDaniel Borkmann FN(probe_read_user), \ 30296ae08ae3SDaniel Borkmann FN(probe_read_kernel), \ 30306ae08ae3SDaniel Borkmann FN(probe_read_user_str), \ 3031206057feSMartin KaFai Lau FN(probe_read_kernel_str), \ 30328482941fSYonghong Song FN(tcp_send_ack), \ 30335576b991SMartin KaFai Lau FN(send_signal_thread), \ 3034*fff7b643SDaniel Xu FN(jiffies64), \ 3035*fff7b643SDaniel Xu FN(read_branch_records), 30367a4b28c6SDaniel Borkmann 3037ebb676daSThomas Graf /* integer value in 'imm' field of BPF_CALL instruction selects which helper 3038ebb676daSThomas Graf * function eBPF program intends to call 30392d0e30c3SDaniel Borkmann */ 3040ebb676daSThomas Graf #define __BPF_ENUM_FN(x) BPF_FUNC_ ## x 3041ebb676daSThomas Graf enum bpf_func_id { 3042ebb676daSThomas Graf __BPF_FUNC_MAPPER(__BPF_ENUM_FN) 304309756af4SAlexei Starovoitov __BPF_FUNC_MAX_ID, 304409756af4SAlexei Starovoitov }; 3045ebb676daSThomas Graf #undef __BPF_ENUM_FN 304609756af4SAlexei Starovoitov 3047781c53bcSDaniel Borkmann /* All flags used by eBPF helper functions, placed here. */ 3048781c53bcSDaniel Borkmann 3049781c53bcSDaniel Borkmann /* BPF_FUNC_skb_store_bytes flags. */ 3050781c53bcSDaniel Borkmann #define BPF_F_RECOMPUTE_CSUM (1ULL << 0) 30518afd54c8SDaniel Borkmann #define BPF_F_INVALIDATE_HASH (1ULL << 1) 3052781c53bcSDaniel Borkmann 3053781c53bcSDaniel Borkmann /* BPF_FUNC_l3_csum_replace and BPF_FUNC_l4_csum_replace flags. 3054781c53bcSDaniel Borkmann * First 4 bits are for passing the header field size. 3055781c53bcSDaniel Borkmann */ 3056781c53bcSDaniel Borkmann #define BPF_F_HDR_FIELD_MASK 0xfULL 3057781c53bcSDaniel Borkmann 3058781c53bcSDaniel Borkmann /* BPF_FUNC_l4_csum_replace flags. */ 3059781c53bcSDaniel Borkmann #define BPF_F_PSEUDO_HDR (1ULL << 4) 30602f72959aSDaniel Borkmann #define BPF_F_MARK_MANGLED_0 (1ULL << 5) 3061d1b662adSDaniel Borkmann #define BPF_F_MARK_ENFORCE (1ULL << 6) 3062781c53bcSDaniel Borkmann 3063781c53bcSDaniel Borkmann /* BPF_FUNC_clone_redirect and BPF_FUNC_redirect flags. */ 3064781c53bcSDaniel Borkmann #define BPF_F_INGRESS (1ULL << 0) 3065781c53bcSDaniel Borkmann 3066c6c33454SDaniel Borkmann /* BPF_FUNC_skb_set_tunnel_key and BPF_FUNC_skb_get_tunnel_key flags. */ 3067c6c33454SDaniel Borkmann #define BPF_F_TUNINFO_IPV6 (1ULL << 0) 3068c6c33454SDaniel Borkmann 3069c195651eSYonghong Song /* flags for both BPF_FUNC_get_stackid and BPF_FUNC_get_stack. */ 3070d5a3b1f6SAlexei Starovoitov #define BPF_F_SKIP_FIELD_MASK 0xffULL 3071d5a3b1f6SAlexei Starovoitov #define BPF_F_USER_STACK (1ULL << 8) 3072c195651eSYonghong Song /* flags used by BPF_FUNC_get_stackid only. */ 3073d5a3b1f6SAlexei Starovoitov #define BPF_F_FAST_STACK_CMP (1ULL << 9) 3074d5a3b1f6SAlexei Starovoitov #define BPF_F_REUSE_STACKID (1ULL << 10) 3075c195651eSYonghong Song /* flags used by BPF_FUNC_get_stack only. */ 3076c195651eSYonghong Song #define BPF_F_USER_BUILD_ID (1ULL << 11) 3077d5a3b1f6SAlexei Starovoitov 30782da897e5SDaniel Borkmann /* BPF_FUNC_skb_set_tunnel_key flags. */ 30792da897e5SDaniel Borkmann #define BPF_F_ZERO_CSUM_TX (1ULL << 1) 308022080870SDaniel Borkmann #define BPF_F_DONT_FRAGMENT (1ULL << 2) 308177a5196aSWilliam Tu #define BPF_F_SEQ_NUMBER (1ULL << 3) 30822da897e5SDaniel Borkmann 3083908432caSYonghong Song /* BPF_FUNC_perf_event_output, BPF_FUNC_perf_event_read and 3084908432caSYonghong Song * BPF_FUNC_perf_event_read_value flags. 3085908432caSYonghong Song */ 30861e33759cSDaniel Borkmann #define BPF_F_INDEX_MASK 0xffffffffULL 30871e33759cSDaniel Borkmann #define BPF_F_CURRENT_CPU BPF_F_INDEX_MASK 3088555c8a86SDaniel Borkmann /* BPF_FUNC_perf_event_output for sk_buff input context. */ 3089555c8a86SDaniel Borkmann #define BPF_F_CTXLEN_MASK (0xfffffULL << 32) 30901e33759cSDaniel Borkmann 3091f71c6143SJoe Stringer /* Current network namespace */ 3092f71c6143SJoe Stringer #define BPF_F_CURRENT_NETNS (-1L) 3093f71c6143SJoe Stringer 30942278f6ccSWillem de Bruijn /* BPF_FUNC_skb_adjust_room flags. */ 30952278f6ccSWillem de Bruijn #define BPF_F_ADJ_ROOM_FIXED_GSO (1ULL << 0) 30962278f6ccSWillem de Bruijn 309758dfc900SAlan Maguire #define BPF_ADJ_ROOM_ENCAP_L2_MASK 0xff 309858dfc900SAlan Maguire #define BPF_ADJ_ROOM_ENCAP_L2_SHIFT 56 309958dfc900SAlan Maguire 3100868d5235SWillem de Bruijn #define BPF_F_ADJ_ROOM_ENCAP_L3_IPV4 (1ULL << 1) 3101868d5235SWillem de Bruijn #define BPF_F_ADJ_ROOM_ENCAP_L3_IPV6 (1ULL << 2) 3102868d5235SWillem de Bruijn #define BPF_F_ADJ_ROOM_ENCAP_L4_GRE (1ULL << 3) 3103868d5235SWillem de Bruijn #define BPF_F_ADJ_ROOM_ENCAP_L4_UDP (1ULL << 4) 310458dfc900SAlan Maguire #define BPF_F_ADJ_ROOM_ENCAP_L2(len) (((__u64)len & \ 310558dfc900SAlan Maguire BPF_ADJ_ROOM_ENCAP_L2_MASK) \ 310658dfc900SAlan Maguire << BPF_ADJ_ROOM_ENCAP_L2_SHIFT) 3107868d5235SWillem de Bruijn 3108808649fbSAndrey Ignatov /* BPF_FUNC_sysctl_get_name flags. */ 3109808649fbSAndrey Ignatov #define BPF_F_SYSCTL_BASE_NAME (1ULL << 0) 3110808649fbSAndrey Ignatov 31116ac99e8fSMartin KaFai Lau /* BPF_FUNC_sk_storage_get flags */ 31126ac99e8fSMartin KaFai Lau #define BPF_SK_STORAGE_GET_F_CREATE (1ULL << 0) 31136ac99e8fSMartin KaFai Lau 3114*fff7b643SDaniel Xu /* BPF_FUNC_read_branch_records flags. */ 3115*fff7b643SDaniel Xu #define BPF_F_GET_BRANCH_RECORDS_SIZE (1ULL << 0) 3116*fff7b643SDaniel Xu 31172be7e212SDaniel Borkmann /* Mode for BPF_FUNC_skb_adjust_room helper. */ 31182be7e212SDaniel Borkmann enum bpf_adj_room_mode { 31192be7e212SDaniel Borkmann BPF_ADJ_ROOM_NET, 312014aa3192SWillem de Bruijn BPF_ADJ_ROOM_MAC, 31212be7e212SDaniel Borkmann }; 31222be7e212SDaniel Borkmann 31234e1ec56cSDaniel Borkmann /* Mode for BPF_FUNC_skb_load_bytes_relative helper. */ 31244e1ec56cSDaniel Borkmann enum bpf_hdr_start_off { 31254e1ec56cSDaniel Borkmann BPF_HDR_START_MAC, 31264e1ec56cSDaniel Borkmann BPF_HDR_START_NET, 31274e1ec56cSDaniel Borkmann }; 31284e1ec56cSDaniel Borkmann 3129fe94cc29SMathieu Xhonneux /* Encapsulation type for BPF_FUNC_lwt_push_encap helper. */ 3130fe94cc29SMathieu Xhonneux enum bpf_lwt_encap_mode { 3131fe94cc29SMathieu Xhonneux BPF_LWT_ENCAP_SEG6, 31323e0bd37cSPeter Oskolkov BPF_LWT_ENCAP_SEG6_INLINE, 31333e0bd37cSPeter Oskolkov BPF_LWT_ENCAP_IP, 3134fe94cc29SMathieu Xhonneux }; 3135fe94cc29SMathieu Xhonneux 3136b7df9adaSDaniel Borkmann #define __bpf_md_ptr(type, name) \ 3137b7df9adaSDaniel Borkmann union { \ 3138b7df9adaSDaniel Borkmann type name; \ 3139b7df9adaSDaniel Borkmann __u64 :64; \ 3140b7df9adaSDaniel Borkmann } __attribute__((aligned(8))) 3141b7df9adaSDaniel Borkmann 31429bac3d6dSAlexei Starovoitov /* user accessible mirror of in-kernel sk_buff. 31439bac3d6dSAlexei Starovoitov * new fields can only be added to the end of this structure 31449bac3d6dSAlexei Starovoitov */ 31459bac3d6dSAlexei Starovoitov struct __sk_buff { 31469bac3d6dSAlexei Starovoitov __u32 len; 31479bac3d6dSAlexei Starovoitov __u32 pkt_type; 31489bac3d6dSAlexei Starovoitov __u32 mark; 31499bac3d6dSAlexei Starovoitov __u32 queue_mapping; 3150c2497395SAlexei Starovoitov __u32 protocol; 3151c2497395SAlexei Starovoitov __u32 vlan_present; 3152c2497395SAlexei Starovoitov __u32 vlan_tci; 315327cd5452SMichal Sekletar __u32 vlan_proto; 3154bcad5718SDaniel Borkmann __u32 priority; 315537e82c2fSAlexei Starovoitov __u32 ingress_ifindex; 315637e82c2fSAlexei Starovoitov __u32 ifindex; 3157d691f9e8SAlexei Starovoitov __u32 tc_index; 3158d691f9e8SAlexei Starovoitov __u32 cb[5]; 3159ba7591d8SDaniel Borkmann __u32 hash; 3160045efa82SDaniel Borkmann __u32 tc_classid; 3161969bf05eSAlexei Starovoitov __u32 data; 3162969bf05eSAlexei Starovoitov __u32 data_end; 3163b1d9fc41SDaniel Borkmann __u32 napi_id; 31648a31db56SJohn Fastabend 3165de8f3a83SDaniel Borkmann /* Accessed by BPF_PROG_TYPE_sk_skb types from here to ... */ 31668a31db56SJohn Fastabend __u32 family; 31678a31db56SJohn Fastabend __u32 remote_ip4; /* Stored in network byte order */ 31688a31db56SJohn Fastabend __u32 local_ip4; /* Stored in network byte order */ 31698a31db56SJohn Fastabend __u32 remote_ip6[4]; /* Stored in network byte order */ 31708a31db56SJohn Fastabend __u32 local_ip6[4]; /* Stored in network byte order */ 31718a31db56SJohn Fastabend __u32 remote_port; /* Stored in network byte order */ 31728a31db56SJohn Fastabend __u32 local_port; /* stored in host byte order */ 3173de8f3a83SDaniel Borkmann /* ... here. */ 3174de8f3a83SDaniel Borkmann 3175de8f3a83SDaniel Borkmann __u32 data_meta; 3176b7df9adaSDaniel Borkmann __bpf_md_ptr(struct bpf_flow_keys *, flow_keys); 3177f11216b2SVlad Dumitrescu __u64 tstamp; 3178e3da08d0SPetar Penkov __u32 wire_len; 3179d9ff286aSEric Dumazet __u32 gso_segs; 318046f8bc92SMartin KaFai Lau __bpf_md_ptr(struct bpf_sock *, sk); 31819bac3d6dSAlexei Starovoitov }; 31829bac3d6dSAlexei Starovoitov 3183d3aa45ceSAlexei Starovoitov struct bpf_tunnel_key { 3184d3aa45ceSAlexei Starovoitov __u32 tunnel_id; 3185c6c33454SDaniel Borkmann union { 3186d3aa45ceSAlexei Starovoitov __u32 remote_ipv4; 3187c6c33454SDaniel Borkmann __u32 remote_ipv6[4]; 3188c6c33454SDaniel Borkmann }; 3189c6c33454SDaniel Borkmann __u8 tunnel_tos; 3190c6c33454SDaniel Borkmann __u8 tunnel_ttl; 31911fbc2e0cSDaniel Borkmann __u16 tunnel_ext; /* Padding, future use. */ 31924018ab18SDaniel Borkmann __u32 tunnel_label; 3193d3aa45ceSAlexei Starovoitov }; 3194d3aa45ceSAlexei Starovoitov 319512bed760SEyal Birger /* user accessible mirror of in-kernel xfrm_state. 319612bed760SEyal Birger * new fields can only be added to the end of this structure 319712bed760SEyal Birger */ 319812bed760SEyal Birger struct bpf_xfrm_state { 319912bed760SEyal Birger __u32 reqid; 320012bed760SEyal Birger __u32 spi; /* Stored in network byte order */ 320112bed760SEyal Birger __u16 family; 32021fbc2e0cSDaniel Borkmann __u16 ext; /* Padding, future use. */ 320312bed760SEyal Birger union { 320412bed760SEyal Birger __u32 remote_ipv4; /* Stored in network byte order */ 320512bed760SEyal Birger __u32 remote_ipv6[4]; /* Stored in network byte order */ 320612bed760SEyal Birger }; 320712bed760SEyal Birger }; 320812bed760SEyal Birger 32093a0af8fdSThomas Graf /* Generic BPF return codes which all BPF program types may support. 32103a0af8fdSThomas Graf * The values are binary compatible with their TC_ACT_* counter-part to 32113a0af8fdSThomas Graf * provide backwards compatibility with existing SCHED_CLS and SCHED_ACT 32123a0af8fdSThomas Graf * programs. 32133a0af8fdSThomas Graf * 32143a0af8fdSThomas Graf * XDP is handled seprately, see XDP_*. 32153a0af8fdSThomas Graf */ 32163a0af8fdSThomas Graf enum bpf_ret_code { 32173a0af8fdSThomas Graf BPF_OK = 0, 32183a0af8fdSThomas Graf /* 1 reserved */ 32193a0af8fdSThomas Graf BPF_DROP = 2, 32203a0af8fdSThomas Graf /* 3-6 reserved */ 32213a0af8fdSThomas Graf BPF_REDIRECT = 7, 32223e0bd37cSPeter Oskolkov /* >127 are reserved for prog type specific return codes. 32233e0bd37cSPeter Oskolkov * 32243e0bd37cSPeter Oskolkov * BPF_LWT_REROUTE: used by BPF_PROG_TYPE_LWT_IN and 32253e0bd37cSPeter Oskolkov * BPF_PROG_TYPE_LWT_XMIT to indicate that skb had been 32263e0bd37cSPeter Oskolkov * changed and should be routed based on its new L3 header. 32273e0bd37cSPeter Oskolkov * (This is an L3 redirect, as opposed to L2 redirect 32283e0bd37cSPeter Oskolkov * represented by BPF_REDIRECT above). 32293e0bd37cSPeter Oskolkov */ 32303e0bd37cSPeter Oskolkov BPF_LWT_REROUTE = 128, 32313a0af8fdSThomas Graf }; 32323a0af8fdSThomas Graf 323361023658SDavid Ahern struct bpf_sock { 323461023658SDavid Ahern __u32 bound_dev_if; 3235aa4c1037SDavid Ahern __u32 family; 3236aa4c1037SDavid Ahern __u32 type; 3237aa4c1037SDavid Ahern __u32 protocol; 3238482dca93SDavid Ahern __u32 mark; 3239482dca93SDavid Ahern __u32 priority; 3240aa65d696SMartin KaFai Lau /* IP address also allows 1 and 2 bytes access */ 3241aa65d696SMartin KaFai Lau __u32 src_ip4; 3242aa65d696SMartin KaFai Lau __u32 src_ip6[4]; 3243aa65d696SMartin KaFai Lau __u32 src_port; /* host byte order */ 3244aa65d696SMartin KaFai Lau __u32 dst_port; /* network byte order */ 3245aa65d696SMartin KaFai Lau __u32 dst_ip4; 3246aa65d696SMartin KaFai Lau __u32 dst_ip6[4]; 3247aa65d696SMartin KaFai Lau __u32 state; 324861023658SDavid Ahern }; 324961023658SDavid Ahern 3250655a51e5SMartin KaFai Lau struct bpf_tcp_sock { 3251655a51e5SMartin KaFai Lau __u32 snd_cwnd; /* Sending congestion window */ 3252655a51e5SMartin KaFai Lau __u32 srtt_us; /* smoothed round trip time << 3 in usecs */ 3253655a51e5SMartin KaFai Lau __u32 rtt_min; 3254655a51e5SMartin KaFai Lau __u32 snd_ssthresh; /* Slow start size threshold */ 3255655a51e5SMartin KaFai Lau __u32 rcv_nxt; /* What we want to receive next */ 3256655a51e5SMartin KaFai Lau __u32 snd_nxt; /* Next sequence we send */ 3257655a51e5SMartin KaFai Lau __u32 snd_una; /* First byte we want an ack for */ 3258655a51e5SMartin KaFai Lau __u32 mss_cache; /* Cached effective mss, not including SACKS */ 3259655a51e5SMartin KaFai Lau __u32 ecn_flags; /* ECN status bits. */ 3260655a51e5SMartin KaFai Lau __u32 rate_delivered; /* saved rate sample: packets delivered */ 3261655a51e5SMartin KaFai Lau __u32 rate_interval_us; /* saved rate sample: time elapsed */ 3262655a51e5SMartin KaFai Lau __u32 packets_out; /* Packets which are "in flight" */ 3263655a51e5SMartin KaFai Lau __u32 retrans_out; /* Retransmitted packets out */ 3264655a51e5SMartin KaFai Lau __u32 total_retrans; /* Total retransmits for entire connection */ 3265655a51e5SMartin KaFai Lau __u32 segs_in; /* RFC4898 tcpEStatsPerfSegsIn 3266655a51e5SMartin KaFai Lau * total number of segments in. 3267655a51e5SMartin KaFai Lau */ 3268655a51e5SMartin KaFai Lau __u32 data_segs_in; /* RFC4898 tcpEStatsPerfDataSegsIn 3269655a51e5SMartin KaFai Lau * total number of data segments in. 3270655a51e5SMartin KaFai Lau */ 3271655a51e5SMartin KaFai Lau __u32 segs_out; /* RFC4898 tcpEStatsPerfSegsOut 3272655a51e5SMartin KaFai Lau * The total number of segments sent. 3273655a51e5SMartin KaFai Lau */ 3274655a51e5SMartin KaFai Lau __u32 data_segs_out; /* RFC4898 tcpEStatsPerfDataSegsOut 3275655a51e5SMartin KaFai Lau * total number of data segments sent. 3276655a51e5SMartin KaFai Lau */ 3277655a51e5SMartin KaFai Lau __u32 lost_out; /* Lost packets */ 3278655a51e5SMartin KaFai Lau __u32 sacked_out; /* SACK'd packets */ 3279655a51e5SMartin KaFai Lau __u64 bytes_received; /* RFC4898 tcpEStatsAppHCThruOctetsReceived 3280655a51e5SMartin KaFai Lau * sum(delta(rcv_nxt)), or how many bytes 3281655a51e5SMartin KaFai Lau * were acked. 3282655a51e5SMartin KaFai Lau */ 3283655a51e5SMartin KaFai Lau __u64 bytes_acked; /* RFC4898 tcpEStatsAppHCThruOctetsAcked 3284655a51e5SMartin KaFai Lau * sum(delta(snd_una)), or how many bytes 3285655a51e5SMartin KaFai Lau * were acked. 3286655a51e5SMartin KaFai Lau */ 32870357746dSStanislav Fomichev __u32 dsack_dups; /* RFC4898 tcpEStatsStackDSACKDups 32880357746dSStanislav Fomichev * total number of DSACK blocks received 32890357746dSStanislav Fomichev */ 32900357746dSStanislav Fomichev __u32 delivered; /* Total data packets delivered incl. rexmits */ 32910357746dSStanislav Fomichev __u32 delivered_ce; /* Like the above but only ECE marked packets */ 3292c2cb5e82SStanislav Fomichev __u32 icsk_retransmits; /* Number of unrecovered [RTO] timeouts */ 3293655a51e5SMartin KaFai Lau }; 3294655a51e5SMartin KaFai Lau 32956acc9b43SJoe Stringer struct bpf_sock_tuple { 32966acc9b43SJoe Stringer union { 32976acc9b43SJoe Stringer struct { 32986acc9b43SJoe Stringer __be32 saddr; 32996acc9b43SJoe Stringer __be32 daddr; 33006acc9b43SJoe Stringer __be16 sport; 33016acc9b43SJoe Stringer __be16 dport; 33026acc9b43SJoe Stringer } ipv4; 33036acc9b43SJoe Stringer struct { 33046acc9b43SJoe Stringer __be32 saddr[4]; 33056acc9b43SJoe Stringer __be32 daddr[4]; 33066acc9b43SJoe Stringer __be16 sport; 33076acc9b43SJoe Stringer __be16 dport; 33086acc9b43SJoe Stringer } ipv6; 33096acc9b43SJoe Stringer }; 33106acc9b43SJoe Stringer }; 33116acc9b43SJoe Stringer 3312fada7fdcSJonathan Lemon struct bpf_xdp_sock { 3313fada7fdcSJonathan Lemon __u32 queue_id; 3314fada7fdcSJonathan Lemon }; 3315fada7fdcSJonathan Lemon 331617bedab2SMartin KaFai Lau #define XDP_PACKET_HEADROOM 256 331717bedab2SMartin KaFai Lau 33186a773a15SBrenden Blanco /* User return codes for XDP prog type. 33196a773a15SBrenden Blanco * A valid XDP program must return one of these defined values. All other 33209beb8bedSDaniel Borkmann * return codes are reserved for future use. Unknown return codes will 33219beb8bedSDaniel Borkmann * result in packet drops and a warning via bpf_warn_invalid_xdp_action(). 33226a773a15SBrenden Blanco */ 33236a773a15SBrenden Blanco enum xdp_action { 33246a773a15SBrenden Blanco XDP_ABORTED = 0, 33256a773a15SBrenden Blanco XDP_DROP, 33266a773a15SBrenden Blanco XDP_PASS, 33276ce96ca3SBrenden Blanco XDP_TX, 3328814abfabSJohn Fastabend XDP_REDIRECT, 33296a773a15SBrenden Blanco }; 33306a773a15SBrenden Blanco 33316a773a15SBrenden Blanco /* user accessible metadata for XDP packet hook 33326a773a15SBrenden Blanco * new fields must be added to the end of this structure 33336a773a15SBrenden Blanco */ 33346a773a15SBrenden Blanco struct xdp_md { 33356a773a15SBrenden Blanco __u32 data; 33366a773a15SBrenden Blanco __u32 data_end; 3337de8f3a83SDaniel Borkmann __u32 data_meta; 3338daaf24c6SJesper Dangaard Brouer /* Below access go through struct xdp_rxq_info */ 333902dd3291SJesper Dangaard Brouer __u32 ingress_ifindex; /* rxq->dev->ifindex */ 334002dd3291SJesper Dangaard Brouer __u32 rx_queue_index; /* rxq->queue_index */ 33416a773a15SBrenden Blanco }; 33426a773a15SBrenden Blanco 3343174a79ffSJohn Fastabend enum sk_action { 3344bfa64075SJohn Fastabend SK_DROP = 0, 3345bfa64075SJohn Fastabend SK_PASS, 3346174a79ffSJohn Fastabend }; 3347174a79ffSJohn Fastabend 33484f738adbSJohn Fastabend /* user accessible metadata for SK_MSG packet hook, new fields must 33494f738adbSJohn Fastabend * be added to the end of this structure 33504f738adbSJohn Fastabend */ 33514f738adbSJohn Fastabend struct sk_msg_md { 3352b7df9adaSDaniel Borkmann __bpf_md_ptr(void *, data); 3353b7df9adaSDaniel Borkmann __bpf_md_ptr(void *, data_end); 3354303def35SJohn Fastabend 3355303def35SJohn Fastabend __u32 family; 3356303def35SJohn Fastabend __u32 remote_ip4; /* Stored in network byte order */ 3357303def35SJohn Fastabend __u32 local_ip4; /* Stored in network byte order */ 3358303def35SJohn Fastabend __u32 remote_ip6[4]; /* Stored in network byte order */ 3359303def35SJohn Fastabend __u32 local_ip6[4]; /* Stored in network byte order */ 3360303def35SJohn Fastabend __u32 remote_port; /* Stored in network byte order */ 3361303def35SJohn Fastabend __u32 local_port; /* stored in host byte order */ 33623bdbd022SJohn Fastabend __u32 size; /* Total size of sk_msg */ 33634f738adbSJohn Fastabend }; 33644f738adbSJohn Fastabend 33652dbb9b9eSMartin KaFai Lau struct sk_reuseport_md { 33662dbb9b9eSMartin KaFai Lau /* 33672dbb9b9eSMartin KaFai Lau * Start of directly accessible data. It begins from 33682dbb9b9eSMartin KaFai Lau * the tcp/udp header. 33692dbb9b9eSMartin KaFai Lau */ 3370b7df9adaSDaniel Borkmann __bpf_md_ptr(void *, data); 3371b7df9adaSDaniel Borkmann /* End of directly accessible data */ 3372b7df9adaSDaniel Borkmann __bpf_md_ptr(void *, data_end); 33732dbb9b9eSMartin KaFai Lau /* 33742dbb9b9eSMartin KaFai Lau * Total length of packet (starting from the tcp/udp header). 33752dbb9b9eSMartin KaFai Lau * Note that the directly accessible bytes (data_end - data) 33762dbb9b9eSMartin KaFai Lau * could be less than this "len". Those bytes could be 33772dbb9b9eSMartin KaFai Lau * indirectly read by a helper "bpf_skb_load_bytes()". 33782dbb9b9eSMartin KaFai Lau */ 33792dbb9b9eSMartin KaFai Lau __u32 len; 33802dbb9b9eSMartin KaFai Lau /* 33812dbb9b9eSMartin KaFai Lau * Eth protocol in the mac header (network byte order). e.g. 33822dbb9b9eSMartin KaFai Lau * ETH_P_IP(0x0800) and ETH_P_IPV6(0x86DD) 33832dbb9b9eSMartin KaFai Lau */ 33842dbb9b9eSMartin KaFai Lau __u32 eth_protocol; 33852dbb9b9eSMartin KaFai Lau __u32 ip_protocol; /* IP protocol. e.g. IPPROTO_TCP, IPPROTO_UDP */ 33862dbb9b9eSMartin KaFai Lau __u32 bind_inany; /* Is sock bound to an INANY address? */ 33872dbb9b9eSMartin KaFai Lau __u32 hash; /* A hash of the packet 4 tuples */ 33882dbb9b9eSMartin KaFai Lau }; 33892dbb9b9eSMartin KaFai Lau 33901e270976SMartin KaFai Lau #define BPF_TAG_SIZE 8 33911e270976SMartin KaFai Lau 33921e270976SMartin KaFai Lau struct bpf_prog_info { 33931e270976SMartin KaFai Lau __u32 type; 33941e270976SMartin KaFai Lau __u32 id; 33951e270976SMartin KaFai Lau __u8 tag[BPF_TAG_SIZE]; 33961e270976SMartin KaFai Lau __u32 jited_prog_len; 33971e270976SMartin KaFai Lau __u32 xlated_prog_len; 33981e270976SMartin KaFai Lau __aligned_u64 jited_prog_insns; 33991e270976SMartin KaFai Lau __aligned_u64 xlated_prog_insns; 3400cb4d2b3fSMartin KaFai Lau __u64 load_time; /* ns since boottime */ 3401cb4d2b3fSMartin KaFai Lau __u32 created_by_uid; 3402cb4d2b3fSMartin KaFai Lau __u32 nr_map_ids; 3403cb4d2b3fSMartin KaFai Lau __aligned_u64 map_ids; 3404067cae47SMartin KaFai Lau char name[BPF_OBJ_NAME_LEN]; 3405675fc275SJakub Kicinski __u32 ifindex; 3406b85fab0eSJiri Olsa __u32 gpl_compatible:1; 34070472301aSBaruch Siach __u32 :31; /* alignment pad */ 3408675fc275SJakub Kicinski __u64 netns_dev; 3409675fc275SJakub Kicinski __u64 netns_ino; 3410dbecd738SSandipan Das __u32 nr_jited_ksyms; 3411815581c1SSandipan Das __u32 nr_jited_func_lens; 3412dbecd738SSandipan Das __aligned_u64 jited_ksyms; 3413815581c1SSandipan Das __aligned_u64 jited_func_lens; 3414838e9690SYonghong Song __u32 btf_id; 3415838e9690SYonghong Song __u32 func_info_rec_size; 3416838e9690SYonghong Song __aligned_u64 func_info; 341711d8b82dSYonghong Song __u32 nr_func_info; 341811d8b82dSYonghong Song __u32 nr_line_info; 3419c454a46bSMartin KaFai Lau __aligned_u64 line_info; 3420c454a46bSMartin KaFai Lau __aligned_u64 jited_line_info; 342111d8b82dSYonghong Song __u32 nr_jited_line_info; 3422c454a46bSMartin KaFai Lau __u32 line_info_rec_size; 3423c454a46bSMartin KaFai Lau __u32 jited_line_info_rec_size; 3424c872bdb3SSong Liu __u32 nr_prog_tags; 3425c872bdb3SSong Liu __aligned_u64 prog_tags; 34265f8f8b93SAlexei Starovoitov __u64 run_time_ns; 34275f8f8b93SAlexei Starovoitov __u64 run_cnt; 34281e270976SMartin KaFai Lau } __attribute__((aligned(8))); 34291e270976SMartin KaFai Lau 34301e270976SMartin KaFai Lau struct bpf_map_info { 34311e270976SMartin KaFai Lau __u32 type; 34321e270976SMartin KaFai Lau __u32 id; 34331e270976SMartin KaFai Lau __u32 key_size; 34341e270976SMartin KaFai Lau __u32 value_size; 34351e270976SMartin KaFai Lau __u32 max_entries; 34361e270976SMartin KaFai Lau __u32 map_flags; 3437067cae47SMartin KaFai Lau char name[BPF_OBJ_NAME_LEN]; 343852775b33SJakub Kicinski __u32 ifindex; 343985d33df3SMartin KaFai Lau __u32 btf_vmlinux_value_type_id; 344052775b33SJakub Kicinski __u64 netns_dev; 344152775b33SJakub Kicinski __u64 netns_ino; 344278958fcaSMartin KaFai Lau __u32 btf_id; 34439b2cf328SMartin KaFai Lau __u32 btf_key_type_id; 34449b2cf328SMartin KaFai Lau __u32 btf_value_type_id; 34451e270976SMartin KaFai Lau } __attribute__((aligned(8))); 34461e270976SMartin KaFai Lau 344762dab84cSMartin KaFai Lau struct bpf_btf_info { 344862dab84cSMartin KaFai Lau __aligned_u64 btf; 344962dab84cSMartin KaFai Lau __u32 btf_size; 345062dab84cSMartin KaFai Lau __u32 id; 345162dab84cSMartin KaFai Lau } __attribute__((aligned(8))); 345262dab84cSMartin KaFai Lau 34534fbac77dSAndrey Ignatov /* User bpf_sock_addr struct to access socket fields and sockaddr struct passed 34544fbac77dSAndrey Ignatov * by user and intended to be used by socket (e.g. to bind to, depends on 34554fbac77dSAndrey Ignatov * attach attach type). 34564fbac77dSAndrey Ignatov */ 34574fbac77dSAndrey Ignatov struct bpf_sock_addr { 34584fbac77dSAndrey Ignatov __u32 user_family; /* Allows 4-byte read, but no write. */ 34594fbac77dSAndrey Ignatov __u32 user_ip4; /* Allows 1,2,4-byte read and 4-byte write. 34604fbac77dSAndrey Ignatov * Stored in network byte order. 34614fbac77dSAndrey Ignatov */ 3462d4ecfeb1SStanislav Fomichev __u32 user_ip6[4]; /* Allows 1,2,4,8-byte read and 4,8-byte write. 34634fbac77dSAndrey Ignatov * Stored in network byte order. 34644fbac77dSAndrey Ignatov */ 34654fbac77dSAndrey Ignatov __u32 user_port; /* Allows 4-byte read and write. 34664fbac77dSAndrey Ignatov * Stored in network byte order 34674fbac77dSAndrey Ignatov */ 34684fbac77dSAndrey Ignatov __u32 family; /* Allows 4-byte read, but no write */ 34694fbac77dSAndrey Ignatov __u32 type; /* Allows 4-byte read, but no write */ 34704fbac77dSAndrey Ignatov __u32 protocol; /* Allows 4-byte read, but no write */ 3471600c70baSStanislav Fomichev __u32 msg_src_ip4; /* Allows 1,2,4-byte read and 4-byte write. 34721cedee13SAndrey Ignatov * Stored in network byte order. 34731cedee13SAndrey Ignatov */ 3474d4ecfeb1SStanislav Fomichev __u32 msg_src_ip6[4]; /* Allows 1,2,4,8-byte read and 4,8-byte write. 34751cedee13SAndrey Ignatov * Stored in network byte order. 34761cedee13SAndrey Ignatov */ 3477fb85c4a7SStanislav Fomichev __bpf_md_ptr(struct bpf_sock *, sk); 34784fbac77dSAndrey Ignatov }; 34794fbac77dSAndrey Ignatov 348040304b2aSLawrence Brakmo /* User bpf_sock_ops struct to access socket values and specify request ops 348140304b2aSLawrence Brakmo * and their replies. 348240304b2aSLawrence Brakmo * Some of this fields are in network (bigendian) byte order and may need 348340304b2aSLawrence Brakmo * to be converted before use (bpf_ntohl() defined in samples/bpf/bpf_endian.h). 348440304b2aSLawrence Brakmo * New fields can only be added at the end of this structure 348540304b2aSLawrence Brakmo */ 348640304b2aSLawrence Brakmo struct bpf_sock_ops { 348740304b2aSLawrence Brakmo __u32 op; 348840304b2aSLawrence Brakmo union { 3489de525be2SLawrence Brakmo __u32 args[4]; /* Optionally passed to bpf program */ 3490de525be2SLawrence Brakmo __u32 reply; /* Returned by bpf program */ 3491de525be2SLawrence Brakmo __u32 replylong[4]; /* Optionally returned by bpf prog */ 349240304b2aSLawrence Brakmo }; 349340304b2aSLawrence Brakmo __u32 family; 349440304b2aSLawrence Brakmo __u32 remote_ip4; /* Stored in network byte order */ 349540304b2aSLawrence Brakmo __u32 local_ip4; /* Stored in network byte order */ 349640304b2aSLawrence Brakmo __u32 remote_ip6[4]; /* Stored in network byte order */ 349740304b2aSLawrence Brakmo __u32 local_ip6[4]; /* Stored in network byte order */ 349840304b2aSLawrence Brakmo __u32 remote_port; /* Stored in network byte order */ 349940304b2aSLawrence Brakmo __u32 local_port; /* stored in host byte order */ 3500f19397a5SLawrence Brakmo __u32 is_fullsock; /* Some TCP fields are only valid if 3501f19397a5SLawrence Brakmo * there is a full socket. If not, the 3502f19397a5SLawrence Brakmo * fields read as zero. 3503f19397a5SLawrence Brakmo */ 3504f19397a5SLawrence Brakmo __u32 snd_cwnd; 3505f19397a5SLawrence Brakmo __u32 srtt_us; /* Averaged RTT << 3 in usecs */ 3506b13d8807SLawrence Brakmo __u32 bpf_sock_ops_cb_flags; /* flags defined in uapi/linux/tcp.h */ 350744f0e430SLawrence Brakmo __u32 state; 350844f0e430SLawrence Brakmo __u32 rtt_min; 350944f0e430SLawrence Brakmo __u32 snd_ssthresh; 351044f0e430SLawrence Brakmo __u32 rcv_nxt; 351144f0e430SLawrence Brakmo __u32 snd_nxt; 351244f0e430SLawrence Brakmo __u32 snd_una; 351344f0e430SLawrence Brakmo __u32 mss_cache; 351444f0e430SLawrence Brakmo __u32 ecn_flags; 351544f0e430SLawrence Brakmo __u32 rate_delivered; 351644f0e430SLawrence Brakmo __u32 rate_interval_us; 351744f0e430SLawrence Brakmo __u32 packets_out; 351844f0e430SLawrence Brakmo __u32 retrans_out; 351944f0e430SLawrence Brakmo __u32 total_retrans; 352044f0e430SLawrence Brakmo __u32 segs_in; 352144f0e430SLawrence Brakmo __u32 data_segs_in; 352244f0e430SLawrence Brakmo __u32 segs_out; 352344f0e430SLawrence Brakmo __u32 data_segs_out; 352444f0e430SLawrence Brakmo __u32 lost_out; 352544f0e430SLawrence Brakmo __u32 sacked_out; 352644f0e430SLawrence Brakmo __u32 sk_txhash; 352744f0e430SLawrence Brakmo __u64 bytes_received; 352844f0e430SLawrence Brakmo __u64 bytes_acked; 35291314ef56SStanislav Fomichev __bpf_md_ptr(struct bpf_sock *, sk); 353040304b2aSLawrence Brakmo }; 353140304b2aSLawrence Brakmo 3532b13d8807SLawrence Brakmo /* Definitions for bpf_sock_ops_cb_flags */ 3533f89013f6SLawrence Brakmo #define BPF_SOCK_OPS_RTO_CB_FLAG (1<<0) 3534a31ad29eSLawrence Brakmo #define BPF_SOCK_OPS_RETRANS_CB_FLAG (1<<1) 3535d4487491SLawrence Brakmo #define BPF_SOCK_OPS_STATE_CB_FLAG (1<<2) 353623729ff2SStanislav Fomichev #define BPF_SOCK_OPS_RTT_CB_FLAG (1<<3) 353723729ff2SStanislav Fomichev #define BPF_SOCK_OPS_ALL_CB_FLAGS 0xF /* Mask of all currently 3538b13d8807SLawrence Brakmo * supported cb flags 3539b13d8807SLawrence Brakmo */ 3540b13d8807SLawrence Brakmo 354140304b2aSLawrence Brakmo /* List of known BPF sock_ops operators. 354240304b2aSLawrence Brakmo * New entries can only be added at the end 354340304b2aSLawrence Brakmo */ 354440304b2aSLawrence Brakmo enum { 354540304b2aSLawrence Brakmo BPF_SOCK_OPS_VOID, 35468550f328SLawrence Brakmo BPF_SOCK_OPS_TIMEOUT_INIT, /* Should return SYN-RTO value to use or 35478550f328SLawrence Brakmo * -1 if default value should be used 35488550f328SLawrence Brakmo */ 354913d3b1ebSLawrence Brakmo BPF_SOCK_OPS_RWND_INIT, /* Should return initial advertized 355013d3b1ebSLawrence Brakmo * window (in packets) or -1 if default 355113d3b1ebSLawrence Brakmo * value should be used 355213d3b1ebSLawrence Brakmo */ 35539872a4bdSLawrence Brakmo BPF_SOCK_OPS_TCP_CONNECT_CB, /* Calls BPF program right before an 35549872a4bdSLawrence Brakmo * active connection is initialized 35559872a4bdSLawrence Brakmo */ 35569872a4bdSLawrence Brakmo BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB, /* Calls BPF program when an 35579872a4bdSLawrence Brakmo * active connection is 35589872a4bdSLawrence Brakmo * established 35599872a4bdSLawrence Brakmo */ 35609872a4bdSLawrence Brakmo BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB, /* Calls BPF program when a 35619872a4bdSLawrence Brakmo * passive connection is 35629872a4bdSLawrence Brakmo * established 35639872a4bdSLawrence Brakmo */ 356491b5b21cSLawrence Brakmo BPF_SOCK_OPS_NEEDS_ECN, /* If connection's congestion control 356591b5b21cSLawrence Brakmo * needs ECN 356691b5b21cSLawrence Brakmo */ 3567e6546ef6SLawrence Brakmo BPF_SOCK_OPS_BASE_RTT, /* Get base RTT. The correct value is 3568e6546ef6SLawrence Brakmo * based on the path and may be 3569e6546ef6SLawrence Brakmo * dependent on the congestion control 3570e6546ef6SLawrence Brakmo * algorithm. In general it indicates 3571e6546ef6SLawrence Brakmo * a congestion threshold. RTTs above 3572e6546ef6SLawrence Brakmo * this indicate congestion 3573e6546ef6SLawrence Brakmo */ 3574f89013f6SLawrence Brakmo BPF_SOCK_OPS_RTO_CB, /* Called when an RTO has triggered. 3575f89013f6SLawrence Brakmo * Arg1: value of icsk_retransmits 3576f89013f6SLawrence Brakmo * Arg2: value of icsk_rto 3577f89013f6SLawrence Brakmo * Arg3: whether RTO has expired 3578f89013f6SLawrence Brakmo */ 3579a31ad29eSLawrence Brakmo BPF_SOCK_OPS_RETRANS_CB, /* Called when skb is retransmitted. 3580a31ad29eSLawrence Brakmo * Arg1: sequence number of 1st byte 3581a31ad29eSLawrence Brakmo * Arg2: # segments 3582a31ad29eSLawrence Brakmo * Arg3: return value of 3583a31ad29eSLawrence Brakmo * tcp_transmit_skb (0 => success) 3584a31ad29eSLawrence Brakmo */ 3585d4487491SLawrence Brakmo BPF_SOCK_OPS_STATE_CB, /* Called when TCP changes state. 3586d4487491SLawrence Brakmo * Arg1: old_state 3587d4487491SLawrence Brakmo * Arg2: new_state 3588d4487491SLawrence Brakmo */ 3589f333ee0cSAndrey Ignatov BPF_SOCK_OPS_TCP_LISTEN_CB, /* Called on listen(2), right after 3590f333ee0cSAndrey Ignatov * socket transition to LISTEN state. 3591f333ee0cSAndrey Ignatov */ 359223729ff2SStanislav Fomichev BPF_SOCK_OPS_RTT_CB, /* Called on every RTT. 359323729ff2SStanislav Fomichev */ 3594d4487491SLawrence Brakmo }; 3595d4487491SLawrence Brakmo 3596d4487491SLawrence Brakmo /* List of TCP states. There is a build check in net/ipv4/tcp.c to detect 3597d4487491SLawrence Brakmo * changes between the TCP and BPF versions. Ideally this should never happen. 3598d4487491SLawrence Brakmo * If it does, we need to add code to convert them before calling 3599d4487491SLawrence Brakmo * the BPF sock_ops function. 3600d4487491SLawrence Brakmo */ 3601d4487491SLawrence Brakmo enum { 3602d4487491SLawrence Brakmo BPF_TCP_ESTABLISHED = 1, 3603d4487491SLawrence Brakmo BPF_TCP_SYN_SENT, 3604d4487491SLawrence Brakmo BPF_TCP_SYN_RECV, 3605d4487491SLawrence Brakmo BPF_TCP_FIN_WAIT1, 3606d4487491SLawrence Brakmo BPF_TCP_FIN_WAIT2, 3607d4487491SLawrence Brakmo BPF_TCP_TIME_WAIT, 3608d4487491SLawrence Brakmo BPF_TCP_CLOSE, 3609d4487491SLawrence Brakmo BPF_TCP_CLOSE_WAIT, 3610d4487491SLawrence Brakmo BPF_TCP_LAST_ACK, 3611d4487491SLawrence Brakmo BPF_TCP_LISTEN, 3612d4487491SLawrence Brakmo BPF_TCP_CLOSING, /* Now a valid state */ 3613d4487491SLawrence Brakmo BPF_TCP_NEW_SYN_RECV, 3614d4487491SLawrence Brakmo 3615d4487491SLawrence Brakmo BPF_TCP_MAX_STATES /* Leave at the end! */ 361640304b2aSLawrence Brakmo }; 361740304b2aSLawrence Brakmo 3618fc747810SLawrence Brakmo #define TCP_BPF_IW 1001 /* Set TCP initial congestion window */ 361913bf9641SLawrence Brakmo #define TCP_BPF_SNDCWND_CLAMP 1002 /* Set sndcwnd_clamp */ 3620fc747810SLawrence Brakmo 3621908432caSYonghong Song struct bpf_perf_event_value { 3622908432caSYonghong Song __u64 counter; 3623908432caSYonghong Song __u64 enabled; 3624908432caSYonghong Song __u64 running; 3625908432caSYonghong Song }; 3626908432caSYonghong Song 3627ebc614f6SRoman Gushchin #define BPF_DEVCG_ACC_MKNOD (1ULL << 0) 3628ebc614f6SRoman Gushchin #define BPF_DEVCG_ACC_READ (1ULL << 1) 3629ebc614f6SRoman Gushchin #define BPF_DEVCG_ACC_WRITE (1ULL << 2) 3630ebc614f6SRoman Gushchin 3631ebc614f6SRoman Gushchin #define BPF_DEVCG_DEV_BLOCK (1ULL << 0) 3632ebc614f6SRoman Gushchin #define BPF_DEVCG_DEV_CHAR (1ULL << 1) 3633ebc614f6SRoman Gushchin 3634ebc614f6SRoman Gushchin struct bpf_cgroup_dev_ctx { 363506ef0ccbSYonghong Song /* access_type encoded as (BPF_DEVCG_ACC_* << 16) | BPF_DEVCG_DEV_* */ 363606ef0ccbSYonghong Song __u32 access_type; 3637ebc614f6SRoman Gushchin __u32 major; 3638ebc614f6SRoman Gushchin __u32 minor; 3639ebc614f6SRoman Gushchin }; 3640ebc614f6SRoman Gushchin 3641c4f6699dSAlexei Starovoitov struct bpf_raw_tracepoint_args { 3642c4f6699dSAlexei Starovoitov __u64 args[0]; 3643c4f6699dSAlexei Starovoitov }; 3644c4f6699dSAlexei Starovoitov 364587f5fc7eSDavid Ahern /* DIRECT: Skip the FIB rules and go to FIB table associated with device 364687f5fc7eSDavid Ahern * OUTPUT: Do lookup from egress perspective; default is ingress 364787f5fc7eSDavid Ahern */ 3648b1d6c15bSMartynas Pumputis #define BPF_FIB_LOOKUP_DIRECT (1U << 0) 3649b1d6c15bSMartynas Pumputis #define BPF_FIB_LOOKUP_OUTPUT (1U << 1) 365087f5fc7eSDavid Ahern 36514c79579bSDavid Ahern enum { 36524c79579bSDavid Ahern BPF_FIB_LKUP_RET_SUCCESS, /* lookup successful */ 36534c79579bSDavid Ahern BPF_FIB_LKUP_RET_BLACKHOLE, /* dest is blackholed; can be dropped */ 36544c79579bSDavid Ahern BPF_FIB_LKUP_RET_UNREACHABLE, /* dest is unreachable; can be dropped */ 36554c79579bSDavid Ahern BPF_FIB_LKUP_RET_PROHIBIT, /* dest not allowed; can be dropped */ 36564c79579bSDavid Ahern BPF_FIB_LKUP_RET_NOT_FWDED, /* packet is not forwarded */ 36574c79579bSDavid Ahern BPF_FIB_LKUP_RET_FWD_DISABLED, /* fwding is not enabled on ingress */ 36584c79579bSDavid Ahern BPF_FIB_LKUP_RET_UNSUPP_LWT, /* fwd requires encapsulation */ 36594c79579bSDavid Ahern BPF_FIB_LKUP_RET_NO_NEIGH, /* no neighbor entry for nh */ 36604c79579bSDavid Ahern BPF_FIB_LKUP_RET_FRAG_NEEDED, /* fragmentation required to fwd */ 36614c79579bSDavid Ahern }; 36624c79579bSDavid Ahern 366387f5fc7eSDavid Ahern struct bpf_fib_lookup { 3664fa898d76SDavid Ahern /* input: network family for lookup (AF_INET, AF_INET6) 3665fa898d76SDavid Ahern * output: network family of egress nexthop 3666fa898d76SDavid Ahern */ 3667fa898d76SDavid Ahern __u8 family; 366887f5fc7eSDavid Ahern 366987f5fc7eSDavid Ahern /* set if lookup is to consider L4 data - e.g., FIB rules */ 367087f5fc7eSDavid Ahern __u8 l4_protocol; 367187f5fc7eSDavid Ahern __be16 sport; 367287f5fc7eSDavid Ahern __be16 dport; 367387f5fc7eSDavid Ahern 367487f5fc7eSDavid Ahern /* total length of packet from network header - used for MTU check */ 367587f5fc7eSDavid Ahern __u16 tot_len; 36764c79579bSDavid Ahern 36774c79579bSDavid Ahern /* input: L3 device index for lookup 36784c79579bSDavid Ahern * output: device index from FIB lookup 36794c79579bSDavid Ahern */ 36804c79579bSDavid Ahern __u32 ifindex; 368187f5fc7eSDavid Ahern 368287f5fc7eSDavid Ahern union { 368387f5fc7eSDavid Ahern /* inputs to lookup */ 368487f5fc7eSDavid Ahern __u8 tos; /* AF_INET */ 3685bd3a08aaSDavid Ahern __be32 flowinfo; /* AF_INET6, flow_label + priority */ 368687f5fc7eSDavid Ahern 3687fa898d76SDavid Ahern /* output: metric of fib result (IPv4/IPv6 only) */ 368887f5fc7eSDavid Ahern __u32 rt_metric; 368987f5fc7eSDavid Ahern }; 369087f5fc7eSDavid Ahern 369187f5fc7eSDavid Ahern union { 369287f5fc7eSDavid Ahern __be32 ipv4_src; 369387f5fc7eSDavid Ahern __u32 ipv6_src[4]; /* in6_addr; network order */ 369487f5fc7eSDavid Ahern }; 369587f5fc7eSDavid Ahern 3696fa898d76SDavid Ahern /* input to bpf_fib_lookup, ipv{4,6}_dst is destination address in 3697fa898d76SDavid Ahern * network header. output: bpf_fib_lookup sets to gateway address 3698fa898d76SDavid Ahern * if FIB lookup returns gateway route 369987f5fc7eSDavid Ahern */ 370087f5fc7eSDavid Ahern union { 370187f5fc7eSDavid Ahern __be32 ipv4_dst; 370287f5fc7eSDavid Ahern __u32 ipv6_dst[4]; /* in6_addr; network order */ 370387f5fc7eSDavid Ahern }; 370487f5fc7eSDavid Ahern 370587f5fc7eSDavid Ahern /* output */ 370687f5fc7eSDavid Ahern __be16 h_vlan_proto; 370787f5fc7eSDavid Ahern __be16 h_vlan_TCI; 370887f5fc7eSDavid Ahern __u8 smac[6]; /* ETH_ALEN */ 370987f5fc7eSDavid Ahern __u8 dmac[6]; /* ETH_ALEN */ 371087f5fc7eSDavid Ahern }; 371187f5fc7eSDavid Ahern 371241bdc4b4SYonghong Song enum bpf_task_fd_type { 371341bdc4b4SYonghong Song BPF_FD_TYPE_RAW_TRACEPOINT, /* tp name */ 371441bdc4b4SYonghong Song BPF_FD_TYPE_TRACEPOINT, /* tp name */ 371541bdc4b4SYonghong Song BPF_FD_TYPE_KPROBE, /* (symbol + offset) or addr */ 371641bdc4b4SYonghong Song BPF_FD_TYPE_KRETPROBE, /* (symbol + offset) or addr */ 371741bdc4b4SYonghong Song BPF_FD_TYPE_UPROBE, /* filename + offset */ 371841bdc4b4SYonghong Song BPF_FD_TYPE_URETPROBE, /* filename + offset */ 371941bdc4b4SYonghong Song }; 372041bdc4b4SYonghong Song 3721086f9568SStanislav Fomichev #define BPF_FLOW_DISSECTOR_F_PARSE_1ST_FRAG (1U << 0) 3722086f9568SStanislav Fomichev #define BPF_FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL (1U << 1) 3723086f9568SStanislav Fomichev #define BPF_FLOW_DISSECTOR_F_STOP_AT_ENCAP (1U << 2) 3724086f9568SStanislav Fomichev 3725d58e468bSPetar Penkov struct bpf_flow_keys { 3726d58e468bSPetar Penkov __u16 nhoff; 3727d58e468bSPetar Penkov __u16 thoff; 3728d58e468bSPetar Penkov __u16 addr_proto; /* ETH_P_* of valid addrs */ 3729d58e468bSPetar Penkov __u8 is_frag; 3730d58e468bSPetar Penkov __u8 is_first_frag; 3731d58e468bSPetar Penkov __u8 is_encap; 3732d58e468bSPetar Penkov __u8 ip_proto; 3733d58e468bSPetar Penkov __be16 n_proto; 3734d58e468bSPetar Penkov __be16 sport; 3735d58e468bSPetar Penkov __be16 dport; 3736d58e468bSPetar Penkov union { 3737d58e468bSPetar Penkov struct { 3738d58e468bSPetar Penkov __be32 ipv4_src; 3739d58e468bSPetar Penkov __be32 ipv4_dst; 3740d58e468bSPetar Penkov }; 3741d58e468bSPetar Penkov struct { 3742d58e468bSPetar Penkov __u32 ipv6_src[4]; /* in6_addr; network order */ 3743d58e468bSPetar Penkov __u32 ipv6_dst[4]; /* in6_addr; network order */ 3744d58e468bSPetar Penkov }; 3745d58e468bSPetar Penkov }; 3746086f9568SStanislav Fomichev __u32 flags; 374771c99e32SStanislav Fomichev __be32 flow_label; 3748d58e468bSPetar Penkov }; 3749d58e468bSPetar Penkov 3750838e9690SYonghong Song struct bpf_func_info { 3751d30d42e0SMartin KaFai Lau __u32 insn_off; 3752838e9690SYonghong Song __u32 type_id; 3753838e9690SYonghong Song }; 3754838e9690SYonghong Song 3755c454a46bSMartin KaFai Lau #define BPF_LINE_INFO_LINE_NUM(line_col) ((line_col) >> 10) 3756c454a46bSMartin KaFai Lau #define BPF_LINE_INFO_LINE_COL(line_col) ((line_col) & 0x3ff) 3757c454a46bSMartin KaFai Lau 3758c454a46bSMartin KaFai Lau struct bpf_line_info { 3759c454a46bSMartin KaFai Lau __u32 insn_off; 3760c454a46bSMartin KaFai Lau __u32 file_name_off; 3761c454a46bSMartin KaFai Lau __u32 line_off; 3762c454a46bSMartin KaFai Lau __u32 line_col; 3763c454a46bSMartin KaFai Lau }; 3764c454a46bSMartin KaFai Lau 3765d83525caSAlexei Starovoitov struct bpf_spin_lock { 3766d83525caSAlexei Starovoitov __u32 val; 3767d83525caSAlexei Starovoitov }; 37687b146cebSAndrey Ignatov 37697b146cebSAndrey Ignatov struct bpf_sysctl { 37707b146cebSAndrey Ignatov __u32 write; /* Sysctl is being read (= 0) or written (= 1). 37717b146cebSAndrey Ignatov * Allows 1,2,4-byte read, but no write. 37727b146cebSAndrey Ignatov */ 3773e1550bfeSAndrey Ignatov __u32 file_pos; /* Sysctl file position to read from, write to. 3774e1550bfeSAndrey Ignatov * Allows 1,2,4-byte read an 4-byte write. 3775e1550bfeSAndrey Ignatov */ 37767b146cebSAndrey Ignatov }; 37777b146cebSAndrey Ignatov 37780d01da6aSStanislav Fomichev struct bpf_sockopt { 37790d01da6aSStanislav Fomichev __bpf_md_ptr(struct bpf_sock *, sk); 37800d01da6aSStanislav Fomichev __bpf_md_ptr(void *, optval); 37810d01da6aSStanislav Fomichev __bpf_md_ptr(void *, optval_end); 37820d01da6aSStanislav Fomichev 37830d01da6aSStanislav Fomichev __s32 level; 37840d01da6aSStanislav Fomichev __s32 optname; 37850d01da6aSStanislav Fomichev __s32 optlen; 37860d01da6aSStanislav Fomichev __s32 retval; 37870d01da6aSStanislav Fomichev }; 37880d01da6aSStanislav Fomichev 3789daedfb22SAlexei Starovoitov #endif /* _UAPI__LINUX_BPF_H__ */ 3790