bpf.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  1. /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
  2. *
  3. * This program is free software; you can redistribute it and/or
  4. * modify it under the terms of version 2 of the GNU General Public
  5. * License as published by the Free Software Foundation.
  6. */
  7. #ifndef _UAPI__LINUX_BPF_H__
  8. #define _UAPI__LINUX_BPF_H__
  9. #include <linux/types.h>
  10. #include <linux/bpf_common.h>
  11. /* Extended instruction set based on top of classic BPF */
  12. /* instruction classes */
  13. #define BPF_ALU64 0x07 /* alu mode in double word width */
  14. /* ld/ldx fields */
  15. #define BPF_DW 0x18 /* double word */
  16. #define BPF_XADD 0xc0 /* exclusive add */
  17. /* alu/jmp fields */
  18. #define BPF_MOV 0xb0 /* mov reg to reg */
  19. #define BPF_ARSH 0xc0 /* sign extending arithmetic shift right */
  20. /* change endianness of a register */
  21. #define BPF_END 0xd0 /* flags for endianness conversion: */
  22. #define BPF_TO_LE 0x00 /* convert to little-endian */
  23. #define BPF_TO_BE 0x08 /* convert to big-endian */
  24. #define BPF_FROM_LE BPF_TO_LE
  25. #define BPF_FROM_BE BPF_TO_BE
  26. /* jmp encodings */
  27. #define BPF_JNE 0x50 /* jump != */
  28. #define BPF_JLT 0xa0 /* LT is unsigned, '<' */
  29. #define BPF_JLE 0xb0 /* LE is unsigned, '<=' */
  30. #define BPF_JSGT 0x60 /* SGT is signed '>', GT in x86 */
  31. #define BPF_JSGE 0x70 /* SGE is signed '>=', GE in x86 */
  32. #define BPF_JSLT 0xc0 /* SLT is signed, '<' */
  33. #define BPF_JSLE 0xd0 /* SLE is signed, '<=' */
  34. #define BPF_CALL 0x80 /* function call */
  35. #define BPF_EXIT 0x90 /* function return */
  36. /* Register numbers */
  37. enum {
  38. BPF_REG_0 = 0,
  39. BPF_REG_1,
  40. BPF_REG_2,
  41. BPF_REG_3,
  42. BPF_REG_4,
  43. BPF_REG_5,
  44. BPF_REG_6,
  45. BPF_REG_7,
  46. BPF_REG_8,
  47. BPF_REG_9,
  48. BPF_REG_10,
  49. __MAX_BPF_REG,
  50. };
  51. /* BPF has 10 general purpose 64-bit registers and stack frame. */
  52. #define MAX_BPF_REG __MAX_BPF_REG
  53. struct bpf_insn {
  54. __u8 code; /* opcode */
  55. __u8 dst_reg:4; /* dest register */
  56. __u8 src_reg:4; /* source register */
  57. __s16 off; /* signed offset */
  58. __s32 imm; /* signed immediate constant */
  59. };
  60. /* Key of an a BPF_MAP_TYPE_LPM_TRIE entry */
  61. struct bpf_lpm_trie_key {
  62. __u32 prefixlen; /* up to 32 for AF_INET, 128 for AF_INET6 */
  63. __u8 data[0]; /* Arbitrary size */
  64. };
  65. /* BPF syscall commands, see bpf(2) man-page for details. */
  66. enum bpf_cmd {
  67. BPF_MAP_CREATE,
  68. BPF_MAP_LOOKUP_ELEM,
  69. BPF_MAP_UPDATE_ELEM,
  70. BPF_MAP_DELETE_ELEM,
  71. BPF_MAP_GET_NEXT_KEY,
  72. BPF_PROG_LOAD,
  73. BPF_OBJ_PIN,
  74. BPF_OBJ_GET,
  75. BPF_PROG_ATTACH,
  76. BPF_PROG_DETACH,
  77. BPF_PROG_TEST_RUN,
  78. BPF_PROG_GET_NEXT_ID,
  79. BPF_MAP_GET_NEXT_ID,
  80. BPF_PROG_GET_FD_BY_ID,
  81. BPF_MAP_GET_FD_BY_ID,
  82. BPF_OBJ_GET_INFO_BY_FD,
  83. };
  84. enum bpf_map_type {
  85. BPF_MAP_TYPE_UNSPEC,
  86. BPF_MAP_TYPE_HASH,
  87. BPF_MAP_TYPE_ARRAY,
  88. BPF_MAP_TYPE_PROG_ARRAY,
  89. BPF_MAP_TYPE_PERF_EVENT_ARRAY,
  90. BPF_MAP_TYPE_PERCPU_HASH,
  91. BPF_MAP_TYPE_PERCPU_ARRAY,
  92. BPF_MAP_TYPE_STACK_TRACE,
  93. BPF_MAP_TYPE_CGROUP_ARRAY,
  94. BPF_MAP_TYPE_LRU_HASH,
  95. BPF_MAP_TYPE_LRU_PERCPU_HASH,
  96. BPF_MAP_TYPE_LPM_TRIE,
  97. BPF_MAP_TYPE_ARRAY_OF_MAPS,
  98. BPF_MAP_TYPE_HASH_OF_MAPS,
  99. BPF_MAP_TYPE_DEVMAP,
  100. BPF_MAP_TYPE_SOCKMAP,
  101. };
  102. enum bpf_prog_type {
  103. BPF_PROG_TYPE_UNSPEC,
  104. BPF_PROG_TYPE_SOCKET_FILTER,
  105. BPF_PROG_TYPE_KPROBE,
  106. BPF_PROG_TYPE_SCHED_CLS,
  107. BPF_PROG_TYPE_SCHED_ACT,
  108. BPF_PROG_TYPE_TRACEPOINT,
  109. BPF_PROG_TYPE_XDP,
  110. BPF_PROG_TYPE_PERF_EVENT,
  111. BPF_PROG_TYPE_CGROUP_SKB,
  112. BPF_PROG_TYPE_CGROUP_SOCK,
  113. BPF_PROG_TYPE_LWT_IN,
  114. BPF_PROG_TYPE_LWT_OUT,
  115. BPF_PROG_TYPE_LWT_XMIT,
  116. BPF_PROG_TYPE_SOCK_OPS,
  117. BPF_PROG_TYPE_SK_SKB,
  118. };
  119. enum bpf_attach_type {
  120. BPF_CGROUP_INET_INGRESS,
  121. BPF_CGROUP_INET_EGRESS,
  122. BPF_CGROUP_INET_SOCK_CREATE,
  123. BPF_CGROUP_SOCK_OPS,
  124. BPF_SK_SKB_STREAM_PARSER,
  125. BPF_SK_SKB_STREAM_VERDICT,
  126. __MAX_BPF_ATTACH_TYPE
  127. };
  128. #define MAX_BPF_ATTACH_TYPE __MAX_BPF_ATTACH_TYPE
  129. /* If BPF_SOCKMAP_STRPARSER is used sockmap will use strparser on receive */
  130. #define BPF_SOCKMAP_STRPARSER (1U << 0)
  131. /* If BPF_F_ALLOW_OVERRIDE flag is used in BPF_PROG_ATTACH command
  132. * to the given target_fd cgroup the descendent cgroup will be able to
  133. * override effective bpf program that was inherited from this cgroup
  134. */
  135. #define BPF_F_ALLOW_OVERRIDE (1U << 0)
  136. /* If BPF_F_STRICT_ALIGNMENT is used in BPF_PROG_LOAD command, the
  137. * verifier will perform strict alignment checking as if the kernel
  138. * has been built with CONFIG_EFFICIENT_UNALIGNED_ACCESS not set,
  139. * and NET_IP_ALIGN defined to 2.
  140. */
  141. #define BPF_F_STRICT_ALIGNMENT (1U << 0)
  142. #define BPF_PSEUDO_MAP_FD 1
  143. /* flags for BPF_MAP_UPDATE_ELEM command */
  144. #define BPF_ANY 0 /* create new element or update existing */
  145. #define BPF_NOEXIST 1 /* create new element if it didn't exist */
  146. #define BPF_EXIST 2 /* update existing element */
  147. /* flags for BPF_MAP_CREATE command */
  148. #define BPF_F_NO_PREALLOC (1U << 0)
  149. /* Instead of having one common LRU list in the
  150. * BPF_MAP_TYPE_LRU_[PERCPU_]HASH map, use a percpu LRU list
  151. * which can scale and perform better.
  152. * Note, the LRU nodes (including free nodes) cannot be moved
  153. * across different LRU lists.
  154. */
  155. #define BPF_F_NO_COMMON_LRU (1U << 1)
  156. /* Specify numa node during map creation */
  157. #define BPF_F_NUMA_NODE (1U << 2)
  158. union bpf_attr {
  159. struct { /* anonymous struct used by BPF_MAP_CREATE command */
  160. __u32 map_type; /* one of enum bpf_map_type */
  161. __u32 key_size; /* size of key in bytes */
  162. __u32 value_size; /* size of value in bytes */
  163. __u32 max_entries; /* max number of entries in a map */
  164. __u32 map_flags; /* BPF_MAP_CREATE related
  165. * flags defined above.
  166. */
  167. __u32 inner_map_fd; /* fd pointing to the inner map */
  168. __u32 numa_node; /* numa node (effective only if
  169. * BPF_F_NUMA_NODE is set).
  170. */
  171. };
  172. struct { /* anonymous struct used by BPF_MAP_*_ELEM commands */
  173. __u32 map_fd;
  174. __aligned_u64 key;
  175. union {
  176. __aligned_u64 value;
  177. __aligned_u64 next_key;
  178. };
  179. __u64 flags;
  180. };
  181. struct { /* anonymous struct used by BPF_PROG_LOAD command */
  182. __u32 prog_type; /* one of enum bpf_prog_type */
  183. __u32 insn_cnt;
  184. __aligned_u64 insns;
  185. __aligned_u64 license;
  186. __u32 log_level; /* verbosity level of verifier */
  187. __u32 log_size; /* size of user buffer */
  188. __aligned_u64 log_buf; /* user supplied buffer */
  189. __u32 kern_version; /* checked when prog_type=kprobe */
  190. __u32 prog_flags;
  191. };
  192. struct { /* anonymous struct used by BPF_OBJ_* commands */
  193. __aligned_u64 pathname;
  194. __u32 bpf_fd;
  195. };
  196. struct { /* anonymous struct used by BPF_PROG_ATTACH/DETACH commands */
  197. __u32 target_fd; /* container object to attach to */
  198. __u32 attach_bpf_fd; /* eBPF program to attach */
  199. __u32 attach_type;
  200. __u32 attach_flags;
  201. };
  202. struct { /* anonymous struct used by BPF_PROG_TEST_RUN command */
  203. __u32 prog_fd;
  204. __u32 retval;
  205. __u32 data_size_in;
  206. __u32 data_size_out;
  207. __aligned_u64 data_in;
  208. __aligned_u64 data_out;
  209. __u32 repeat;
  210. __u32 duration;
  211. } test;
  212. struct { /* anonymous struct used by BPF_*_GET_*_ID */
  213. union {
  214. __u32 start_id;
  215. __u32 prog_id;
  216. __u32 map_id;
  217. };
  218. __u32 next_id;
  219. };
  220. struct { /* anonymous struct used by BPF_OBJ_GET_INFO_BY_FD */
  221. __u32 bpf_fd;
  222. __u32 info_len;
  223. __aligned_u64 info;
  224. } info;
  225. } __attribute__((aligned(8)));
  226. /* BPF helper function descriptions:
  227. *
  228. * void *bpf_map_lookup_elem(&map, &key)
  229. * Return: Map value or NULL
  230. *
  231. * int bpf_map_update_elem(&map, &key, &value, flags)
  232. * Return: 0 on success or negative error
  233. *
  234. * int bpf_map_delete_elem(&map, &key)
  235. * Return: 0 on success or negative error
  236. *
  237. * int bpf_probe_read(void *dst, int size, void *src)
  238. * Return: 0 on success or negative error
  239. *
  240. * u64 bpf_ktime_get_ns(void)
  241. * Return: current ktime
  242. *
  243. * int bpf_trace_printk(const char *fmt, int fmt_size, ...)
  244. * Return: length of buffer written or negative error
  245. *
  246. * u32 bpf_prandom_u32(void)
  247. * Return: random value
  248. *
  249. * u32 bpf_raw_smp_processor_id(void)
  250. * Return: SMP processor ID
  251. *
  252. * int bpf_skb_store_bytes(skb, offset, from, len, flags)
  253. * store bytes into packet
  254. * @skb: pointer to skb
  255. * @offset: offset within packet from skb->mac_header
  256. * @from: pointer where to copy bytes from
  257. * @len: number of bytes to store into packet
  258. * @flags: bit 0 - if true, recompute skb->csum
  259. * other bits - reserved
  260. * Return: 0 on success or negative error
  261. *
  262. * int bpf_l3_csum_replace(skb, offset, from, to, flags)
  263. * recompute IP checksum
  264. * @skb: pointer to skb
  265. * @offset: offset within packet where IP checksum is located
  266. * @from: old value of header field
  267. * @to: new value of header field
  268. * @flags: bits 0-3 - size of header field
  269. * other bits - reserved
  270. * Return: 0 on success or negative error
  271. *
  272. * int bpf_l4_csum_replace(skb, offset, from, to, flags)
  273. * recompute TCP/UDP checksum
  274. * @skb: pointer to skb
  275. * @offset: offset within packet where TCP/UDP checksum is located
  276. * @from: old value of header field
  277. * @to: new value of header field
  278. * @flags: bits 0-3 - size of header field
  279. * bit 4 - is pseudo header
  280. * other bits - reserved
  281. * Return: 0 on success or negative error
  282. *
  283. * int bpf_tail_call(ctx, prog_array_map, index)
  284. * jump into another BPF program
  285. * @ctx: context pointer passed to next program
  286. * @prog_array_map: pointer to map which type is BPF_MAP_TYPE_PROG_ARRAY
  287. * @index: index inside array that selects specific program to run
  288. * Return: 0 on success or negative error
  289. *
  290. * int bpf_clone_redirect(skb, ifindex, flags)
  291. * redirect to another netdev
  292. * @skb: pointer to skb
  293. * @ifindex: ifindex of the net device
  294. * @flags: bit 0 - if set, redirect to ingress instead of egress
  295. * other bits - reserved
  296. * Return: 0 on success or negative error
  297. *
  298. * u64 bpf_get_current_pid_tgid(void)
  299. * Return: current->tgid << 32 | current->pid
  300. *
  301. * u64 bpf_get_current_uid_gid(void)
  302. * Return: current_gid << 32 | current_uid
  303. *
  304. * int bpf_get_current_comm(char *buf, int size_of_buf)
  305. * stores current->comm into buf
  306. * Return: 0 on success or negative error
  307. *
  308. * u32 bpf_get_cgroup_classid(skb)
  309. * retrieve a proc's classid
  310. * @skb: pointer to skb
  311. * Return: classid if != 0
  312. *
  313. * int bpf_skb_vlan_push(skb, vlan_proto, vlan_tci)
  314. * Return: 0 on success or negative error
  315. *
  316. * int bpf_skb_vlan_pop(skb)
  317. * Return: 0 on success or negative error
  318. *
  319. * int bpf_skb_get_tunnel_key(skb, key, size, flags)
  320. * int bpf_skb_set_tunnel_key(skb, key, size, flags)
  321. * retrieve or populate tunnel metadata
  322. * @skb: pointer to skb
  323. * @key: pointer to 'struct bpf_tunnel_key'
  324. * @size: size of 'struct bpf_tunnel_key'
  325. * @flags: room for future extensions
  326. * Return: 0 on success or negative error
  327. *
  328. * u64 bpf_perf_event_read(map, flags)
  329. * read perf event counter value
  330. * @map: pointer to perf_event_array map
  331. * @flags: index of event in the map or bitmask flags
  332. * Return: value of perf event counter read or error code
  333. *
  334. * int bpf_redirect(ifindex, flags)
  335. * redirect to another netdev
  336. * @ifindex: ifindex of the net device
  337. * @flags:
  338. * cls_bpf:
  339. * bit 0 - if set, redirect to ingress instead of egress
  340. * other bits - reserved
  341. * xdp_bpf:
  342. * all bits - reserved
  343. * Return: cls_bpf: TC_ACT_REDIRECT on success or TC_ACT_SHOT on error
  344. * xdp_bfp: XDP_REDIRECT on success or XDP_ABORT on error
  345. * int bpf_redirect_map(map, key, flags)
  346. * redirect to endpoint in map
  347. * @map: pointer to dev map
  348. * @key: index in map to lookup
  349. * @flags: --
  350. * Return: XDP_REDIRECT on success or XDP_ABORT on error
  351. *
  352. * u32 bpf_get_route_realm(skb)
  353. * retrieve a dst's tclassid
  354. * @skb: pointer to skb
  355. * Return: realm if != 0
  356. *
  357. * int bpf_perf_event_output(ctx, map, flags, data, size)
  358. * output perf raw sample
  359. * @ctx: struct pt_regs*
  360. * @map: pointer to perf_event_array map
  361. * @flags: index of event in the map or bitmask flags
  362. * @data: data on stack to be output as raw data
  363. * @size: size of data
  364. * Return: 0 on success or negative error
  365. *
  366. * int bpf_get_stackid(ctx, map, flags)
  367. * walk user or kernel stack and return id
  368. * @ctx: struct pt_regs*
  369. * @map: pointer to stack_trace map
  370. * @flags: bits 0-7 - numer of stack frames to skip
  371. * bit 8 - collect user stack instead of kernel
  372. * bit 9 - compare stacks by hash only
  373. * bit 10 - if two different stacks hash into the same stackid
  374. * discard old
  375. * other bits - reserved
  376. * Return: >= 0 stackid on success or negative error
  377. *
  378. * s64 bpf_csum_diff(from, from_size, to, to_size, seed)
  379. * calculate csum diff
  380. * @from: raw from buffer
  381. * @from_size: length of from buffer
  382. * @to: raw to buffer
  383. * @to_size: length of to buffer
  384. * @seed: optional seed
  385. * Return: csum result or negative error code
  386. *
  387. * int bpf_skb_get_tunnel_opt(skb, opt, size)
  388. * retrieve tunnel options metadata
  389. * @skb: pointer to skb
  390. * @opt: pointer to raw tunnel option data
  391. * @size: size of @opt
  392. * Return: option size
  393. *
  394. * int bpf_skb_set_tunnel_opt(skb, opt, size)
  395. * populate tunnel options metadata
  396. * @skb: pointer to skb
  397. * @opt: pointer to raw tunnel option data
  398. * @size: size of @opt
  399. * Return: 0 on success or negative error
  400. *
  401. * int bpf_skb_change_proto(skb, proto, flags)
  402. * Change protocol of the skb. Currently supported is v4 -> v6,
  403. * v6 -> v4 transitions. The helper will also resize the skb. eBPF
  404. * program is expected to fill the new headers via skb_store_bytes
  405. * and lX_csum_replace.
  406. * @skb: pointer to skb
  407. * @proto: new skb->protocol type
  408. * @flags: reserved
  409. * Return: 0 on success or negative error
  410. *
  411. * int bpf_skb_change_type(skb, type)
  412. * Change packet type of skb.
  413. * @skb: pointer to skb
  414. * @type: new skb->pkt_type type
  415. * Return: 0 on success or negative error
  416. *
  417. * int bpf_skb_under_cgroup(skb, map, index)
  418. * Check cgroup2 membership of skb
  419. * @skb: pointer to skb
  420. * @map: pointer to bpf_map in BPF_MAP_TYPE_CGROUP_ARRAY type
  421. * @index: index of the cgroup in the bpf_map
  422. * Return:
  423. * == 0 skb failed the cgroup2 descendant test
  424. * == 1 skb succeeded the cgroup2 descendant test
  425. * < 0 error
  426. *
  427. * u32 bpf_get_hash_recalc(skb)
  428. * Retrieve and possibly recalculate skb->hash.
  429. * @skb: pointer to skb
  430. * Return: hash
  431. *
  432. * u64 bpf_get_current_task(void)
  433. * Returns current task_struct
  434. * Return: current
  435. *
  436. * int bpf_probe_write_user(void *dst, void *src, int len)
  437. * safely attempt to write to a location
  438. * @dst: destination address in userspace
  439. * @src: source address on stack
  440. * @len: number of bytes to copy
  441. * Return: 0 on success or negative error
  442. *
  443. * int bpf_current_task_under_cgroup(map, index)
  444. * Check cgroup2 membership of current task
  445. * @map: pointer to bpf_map in BPF_MAP_TYPE_CGROUP_ARRAY type
  446. * @index: index of the cgroup in the bpf_map
  447. * Return:
  448. * == 0 current failed the cgroup2 descendant test
  449. * == 1 current succeeded the cgroup2 descendant test
  450. * < 0 error
  451. *
  452. * int bpf_skb_change_tail(skb, len, flags)
  453. * The helper will resize the skb to the given new size, to be used f.e.
  454. * with control messages.
  455. * @skb: pointer to skb
  456. * @len: new skb length
  457. * @flags: reserved
  458. * Return: 0 on success or negative error
  459. *
  460. * int bpf_skb_pull_data(skb, len)
  461. * The helper will pull in non-linear data in case the skb is non-linear
  462. * and not all of len are part of the linear section. Only needed for
  463. * read/write with direct packet access.
  464. * @skb: pointer to skb
  465. * @len: len to make read/writeable
  466. * Return: 0 on success or negative error
  467. *
  468. * s64 bpf_csum_update(skb, csum)
  469. * Adds csum into skb->csum in case of CHECKSUM_COMPLETE.
  470. * @skb: pointer to skb
  471. * @csum: csum to add
  472. * Return: csum on success or negative error
  473. *
  474. * void bpf_set_hash_invalid(skb)
  475. * Invalidate current skb->hash.
  476. * @skb: pointer to skb
  477. *
  478. * int bpf_get_numa_node_id()
  479. * Return: Id of current NUMA node.
  480. *
  481. * int bpf_skb_change_head()
  482. * Grows headroom of skb and adjusts MAC header offset accordingly.
  483. * Will extends/reallocae as required automatically.
  484. * May change skb data pointer and will thus invalidate any check
  485. * performed for direct packet access.
  486. * @skb: pointer to skb
  487. * @len: length of header to be pushed in front
  488. * @flags: Flags (unused for now)
  489. * Return: 0 on success or negative error
  490. *
  491. * int bpf_xdp_adjust_head(xdp_md, delta)
  492. * Adjust the xdp_md.data by delta
  493. * @xdp_md: pointer to xdp_md
  494. * @delta: An positive/negative integer to be added to xdp_md.data
  495. * Return: 0 on success or negative on error
  496. *
  497. * int bpf_probe_read_str(void *dst, int size, const void *unsafe_ptr)
  498. * Copy a NUL terminated string from unsafe address. In case the string
  499. * length is smaller than size, the target is not padded with further NUL
  500. * bytes. In case the string length is larger than size, just count-1
  501. * bytes are copied and the last byte is set to NUL.
  502. * @dst: destination address
  503. * @size: maximum number of bytes to copy, including the trailing NUL
  504. * @unsafe_ptr: unsafe address
  505. * Return:
  506. * > 0 length of the string including the trailing NUL on success
  507. * < 0 error
  508. *
  509. * u64 bpf_get_socket_cookie(skb)
  510. * Get the cookie for the socket stored inside sk_buff.
  511. * @skb: pointer to skb
  512. * Return: 8 Bytes non-decreasing number on success or 0 if the socket
  513. * field is missing inside sk_buff
  514. *
  515. * u32 bpf_get_socket_uid(skb)
  516. * Get the owner uid of the socket stored inside sk_buff.
  517. * @skb: pointer to skb
  518. * Return: uid of the socket owner on success or overflowuid if failed.
  519. *
  520. * u32 bpf_set_hash(skb, hash)
  521. * Set full skb->hash.
  522. * @skb: pointer to skb
  523. * @hash: hash to set
  524. *
  525. * int bpf_setsockopt(bpf_socket, level, optname, optval, optlen)
  526. * Calls setsockopt. Not all opts are available, only those with
  527. * integer optvals plus TCP_CONGESTION.
  528. * Supported levels: SOL_SOCKET and IPROTO_TCP
  529. * @bpf_socket: pointer to bpf_socket
  530. * @level: SOL_SOCKET or IPROTO_TCP
  531. * @optname: option name
  532. * @optval: pointer to option value
  533. * @optlen: length of optval in byes
  534. * Return: 0 or negative error
  535. *
  536. * int bpf_skb_adjust_room(skb, len_diff, mode, flags)
  537. * Grow or shrink room in sk_buff.
  538. * @skb: pointer to skb
  539. * @len_diff: (signed) amount of room to grow/shrink
  540. * @mode: operation mode (enum bpf_adj_room_mode)
  541. * @flags: reserved for future use
  542. * Return: 0 on success or negative error code
  543. *
  544. * int bpf_sk_redirect_map(map, key, flags)
  545. * Redirect skb to a sock in map using key as a lookup key for the
  546. * sock in map.
  547. * @map: pointer to sockmap
  548. * @key: key to lookup sock in map
  549. * @flags: reserved for future use
  550. * Return: SK_REDIRECT
  551. *
  552. * int bpf_sock_map_update(skops, map, key, flags)
  553. * @skops: pointer to bpf_sock_ops
  554. * @map: pointer to sockmap to update
  555. * @key: key to insert/update sock in map
  556. * @flags: same flags as map update elem
  557. */
  558. #define __BPF_FUNC_MAPPER(FN) \
  559. FN(unspec), \
  560. FN(map_lookup_elem), \
  561. FN(map_update_elem), \
  562. FN(map_delete_elem), \
  563. FN(probe_read), \
  564. FN(ktime_get_ns), \
  565. FN(trace_printk), \
  566. FN(get_prandom_u32), \
  567. FN(get_smp_processor_id), \
  568. FN(skb_store_bytes), \
  569. FN(l3_csum_replace), \
  570. FN(l4_csum_replace), \
  571. FN(tail_call), \
  572. FN(clone_redirect), \
  573. FN(get_current_pid_tgid), \
  574. FN(get_current_uid_gid), \
  575. FN(get_current_comm), \
  576. FN(get_cgroup_classid), \
  577. FN(skb_vlan_push), \
  578. FN(skb_vlan_pop), \
  579. FN(skb_get_tunnel_key), \
  580. FN(skb_set_tunnel_key), \
  581. FN(perf_event_read), \
  582. FN(redirect), \
  583. FN(get_route_realm), \
  584. FN(perf_event_output), \
  585. FN(skb_load_bytes), \
  586. FN(get_stackid), \
  587. FN(csum_diff), \
  588. FN(skb_get_tunnel_opt), \
  589. FN(skb_set_tunnel_opt), \
  590. FN(skb_change_proto), \
  591. FN(skb_change_type), \
  592. FN(skb_under_cgroup), \
  593. FN(get_hash_recalc), \
  594. FN(get_current_task), \
  595. FN(probe_write_user), \
  596. FN(current_task_under_cgroup), \
  597. FN(skb_change_tail), \
  598. FN(skb_pull_data), \
  599. FN(csum_update), \
  600. FN(set_hash_invalid), \
  601. FN(get_numa_node_id), \
  602. FN(skb_change_head), \
  603. FN(xdp_adjust_head), \
  604. FN(probe_read_str), \
  605. FN(get_socket_cookie), \
  606. FN(get_socket_uid), \
  607. FN(set_hash), \
  608. FN(setsockopt), \
  609. FN(skb_adjust_room), \
  610. FN(redirect_map), \
  611. FN(sk_redirect_map), \
  612. FN(sock_map_update), \
  613. /* integer value in 'imm' field of BPF_CALL instruction selects which helper
  614. * function eBPF program intends to call
  615. */
  616. #define __BPF_ENUM_FN(x) BPF_FUNC_ ## x
  617. enum bpf_func_id {
  618. __BPF_FUNC_MAPPER(__BPF_ENUM_FN)
  619. __BPF_FUNC_MAX_ID,
  620. };
  621. #undef __BPF_ENUM_FN
  622. /* All flags used by eBPF helper functions, placed here. */
  623. /* BPF_FUNC_skb_store_bytes flags. */
  624. #define BPF_F_RECOMPUTE_CSUM (1ULL << 0)
  625. #define BPF_F_INVALIDATE_HASH (1ULL << 1)
  626. /* BPF_FUNC_l3_csum_replace and BPF_FUNC_l4_csum_replace flags.
  627. * First 4 bits are for passing the header field size.
  628. */
  629. #define BPF_F_HDR_FIELD_MASK 0xfULL
  630. /* BPF_FUNC_l4_csum_replace flags. */
  631. #define BPF_F_PSEUDO_HDR (1ULL << 4)
  632. #define BPF_F_MARK_MANGLED_0 (1ULL << 5)
  633. #define BPF_F_MARK_ENFORCE (1ULL << 6)
  634. /* BPF_FUNC_clone_redirect and BPF_FUNC_redirect flags. */
  635. #define BPF_F_INGRESS (1ULL << 0)
  636. /* BPF_FUNC_skb_set_tunnel_key and BPF_FUNC_skb_get_tunnel_key flags. */
  637. #define BPF_F_TUNINFO_IPV6 (1ULL << 0)
  638. /* BPF_FUNC_get_stackid flags. */
  639. #define BPF_F_SKIP_FIELD_MASK 0xffULL
  640. #define BPF_F_USER_STACK (1ULL << 8)
  641. #define BPF_F_FAST_STACK_CMP (1ULL << 9)
  642. #define BPF_F_REUSE_STACKID (1ULL << 10)
  643. /* BPF_FUNC_skb_set_tunnel_key flags. */
  644. #define BPF_F_ZERO_CSUM_TX (1ULL << 1)
  645. #define BPF_F_DONT_FRAGMENT (1ULL << 2)
  646. /* BPF_FUNC_perf_event_output and BPF_FUNC_perf_event_read flags. */
  647. #define BPF_F_INDEX_MASK 0xffffffffULL
  648. #define BPF_F_CURRENT_CPU BPF_F_INDEX_MASK
  649. /* BPF_FUNC_perf_event_output for sk_buff input context. */
  650. #define BPF_F_CTXLEN_MASK (0xfffffULL << 32)
  651. /* Mode for BPF_FUNC_skb_adjust_room helper. */
  652. enum bpf_adj_room_mode {
  653. BPF_ADJ_ROOM_NET,
  654. };
  655. /* user accessible mirror of in-kernel sk_buff.
  656. * new fields can only be added to the end of this structure
  657. */
  658. struct __sk_buff {
  659. __u32 len;
  660. __u32 pkt_type;
  661. __u32 mark;
  662. __u32 queue_mapping;
  663. __u32 protocol;
  664. __u32 vlan_present;
  665. __u32 vlan_tci;
  666. __u32 vlan_proto;
  667. __u32 priority;
  668. __u32 ingress_ifindex;
  669. __u32 ifindex;
  670. __u32 tc_index;
  671. __u32 cb[5];
  672. __u32 hash;
  673. __u32 tc_classid;
  674. __u32 data;
  675. __u32 data_end;
  676. __u32 napi_id;
  677. /* accessed by BPF_PROG_TYPE_sk_skb types */
  678. __u32 family;
  679. __u32 remote_ip4; /* Stored in network byte order */
  680. __u32 local_ip4; /* Stored in network byte order */
  681. __u32 remote_ip6[4]; /* Stored in network byte order */
  682. __u32 local_ip6[4]; /* Stored in network byte order */
  683. __u32 remote_port; /* Stored in network byte order */
  684. __u32 local_port; /* stored in host byte order */
  685. };
  686. struct bpf_tunnel_key {
  687. __u32 tunnel_id;
  688. union {
  689. __u32 remote_ipv4;
  690. __u32 remote_ipv6[4];
  691. };
  692. __u8 tunnel_tos;
  693. __u8 tunnel_ttl;
  694. __u16 tunnel_ext;
  695. __u32 tunnel_label;
  696. };
  697. /* Generic BPF return codes which all BPF program types may support.
  698. * The values are binary compatible with their TC_ACT_* counter-part to
  699. * provide backwards compatibility with existing SCHED_CLS and SCHED_ACT
  700. * programs.
  701. *
  702. * XDP is handled seprately, see XDP_*.
  703. */
  704. enum bpf_ret_code {
  705. BPF_OK = 0,
  706. /* 1 reserved */
  707. BPF_DROP = 2,
  708. /* 3-6 reserved */
  709. BPF_REDIRECT = 7,
  710. /* >127 are reserved for prog type specific return codes */
  711. };
  712. struct bpf_sock {
  713. __u32 bound_dev_if;
  714. __u32 family;
  715. __u32 type;
  716. __u32 protocol;
  717. };
  718. #define XDP_PACKET_HEADROOM 256
  719. /* User return codes for XDP prog type.
  720. * A valid XDP program must return one of these defined values. All other
  721. * return codes are reserved for future use. Unknown return codes will result
  722. * in packet drop.
  723. */
  724. enum xdp_action {
  725. XDP_ABORTED = 0,
  726. XDP_DROP,
  727. XDP_PASS,
  728. XDP_TX,
  729. XDP_REDIRECT,
  730. };
  731. /* user accessible metadata for XDP packet hook
  732. * new fields must be added to the end of this structure
  733. */
  734. struct xdp_md {
  735. __u32 data;
  736. __u32 data_end;
  737. };
  738. enum sk_action {
  739. SK_ABORTED = 0,
  740. SK_DROP,
  741. SK_REDIRECT,
  742. };
  743. #define BPF_TAG_SIZE 8
  744. struct bpf_prog_info {
  745. __u32 type;
  746. __u32 id;
  747. __u8 tag[BPF_TAG_SIZE];
  748. __u32 jited_prog_len;
  749. __u32 xlated_prog_len;
  750. __aligned_u64 jited_prog_insns;
  751. __aligned_u64 xlated_prog_insns;
  752. } __attribute__((aligned(8)));
  753. struct bpf_map_info {
  754. __u32 type;
  755. __u32 id;
  756. __u32 key_size;
  757. __u32 value_size;
  758. __u32 max_entries;
  759. __u32 map_flags;
  760. } __attribute__((aligned(8)));
  761. /* User bpf_sock_ops struct to access socket values and specify request ops
  762. * and their replies.
  763. * Some of this fields are in network (bigendian) byte order and may need
  764. * to be converted before use (bpf_ntohl() defined in samples/bpf/bpf_endian.h).
  765. * New fields can only be added at the end of this structure
  766. */
  767. struct bpf_sock_ops {
  768. __u32 op;
  769. union {
  770. __u32 reply;
  771. __u32 replylong[4];
  772. };
  773. __u32 family;
  774. __u32 remote_ip4; /* Stored in network byte order */
  775. __u32 local_ip4; /* Stored in network byte order */
  776. __u32 remote_ip6[4]; /* Stored in network byte order */
  777. __u32 local_ip6[4]; /* Stored in network byte order */
  778. __u32 remote_port; /* Stored in network byte order */
  779. __u32 local_port; /* stored in host byte order */
  780. };
  781. /* List of known BPF sock_ops operators.
  782. * New entries can only be added at the end
  783. */
  784. enum {
  785. BPF_SOCK_OPS_VOID,
  786. BPF_SOCK_OPS_TIMEOUT_INIT, /* Should return SYN-RTO value to use or
  787. * -1 if default value should be used
  788. */
  789. BPF_SOCK_OPS_RWND_INIT, /* Should return initial advertized
  790. * window (in packets) or -1 if default
  791. * value should be used
  792. */
  793. BPF_SOCK_OPS_TCP_CONNECT_CB, /* Calls BPF program right before an
  794. * active connection is initialized
  795. */
  796. BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB, /* Calls BPF program when an
  797. * active connection is
  798. * established
  799. */
  800. BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB, /* Calls BPF program when a
  801. * passive connection is
  802. * established
  803. */
  804. BPF_SOCK_OPS_NEEDS_ECN, /* If connection's congestion control
  805. * needs ECN
  806. */
  807. };
  808. #define TCP_BPF_IW 1001 /* Set TCP initial congestion window */
  809. #define TCP_BPF_SNDCWND_CLAMP 1002 /* Set sndcwnd_clamp */
  810. #endif /* _UAPI__LINUX_BPF_H__ */