bpf: Add d_path helper
[linux-2.6-microblaze.git] / include / uapi / linux / bpf.h
index 0480f89..0388bc0 100644 (file)
@@ -155,6 +155,7 @@ enum bpf_map_type {
        BPF_MAP_TYPE_DEVMAP_HASH,
        BPF_MAP_TYPE_STRUCT_OPS,
        BPF_MAP_TYPE_RINGBUF,
+       BPF_MAP_TYPE_INODE_STORAGE,
 };
 
 /* Note that tracing related programs such as
@@ -2807,7 +2808,7 @@ union bpf_attr {
  *
  *             **-ERANGE** if resulting value was out of range.
  *
- * void *bpf_sk_storage_get(struct bpf_map *map, struct bpf_sock *sk, void *value, u64 flags)
+ * void *bpf_sk_storage_get(struct bpf_map *map, void *sk, void *value, u64 flags)
  *     Description
  *             Get a bpf-local-storage from a *sk*.
  *
@@ -2823,6 +2824,9 @@ union bpf_attr {
  *             "type". The bpf-local-storage "type" (i.e. the *map*) is
  *             searched against all bpf-local-storages residing at *sk*.
  *
+ *             *sk* is a kernel **struct sock** pointer for LSM program.
+ *             *sk* is a **struct bpf_sock** pointer for other program types.
+ *
  *             An optional *flags* (**BPF_SK_STORAGE_GET_F_CREATE**) can be
  *             used such that a new bpf-local-storage will be
  *             created if one does not exist.  *value* can be used
@@ -2835,7 +2839,7 @@ union bpf_attr {
  *             **NULL** if not found or there was an error in adding
  *             a new bpf-local-storage.
  *
- * long bpf_sk_storage_delete(struct bpf_map *map, struct bpf_sock *sk)
+ * long bpf_sk_storage_delete(struct bpf_map *map, void *sk)
  *     Description
  *             Delete a bpf-local-storage from a *sk*.
  *     Return
@@ -3395,6 +3399,168 @@ union bpf_attr {
  *             A non-negative value equal to or less than *size* on success,
  *             or a negative error in case of failure.
  *
+ * long bpf_load_hdr_opt(struct bpf_sock_ops *skops, void *searchby_res, u32 len, u64 flags)
+ *     Description
+ *             Load header option.  Support reading a particular TCP header
+ *             option for bpf program (BPF_PROG_TYPE_SOCK_OPS).
+ *
+ *             If *flags* is 0, it will search the option from the
+ *             sock_ops->skb_data.  The comment in "struct bpf_sock_ops"
+ *             has details on what skb_data contains under different
+ *             sock_ops->op.
+ *
+ *             The first byte of the *searchby_res* specifies the
+ *             kind that it wants to search.
+ *
+ *             If the searching kind is an experimental kind
+ *             (i.e. 253 or 254 according to RFC6994).  It also
+ *             needs to specify the "magic" which is either
+ *             2 bytes or 4 bytes.  It then also needs to
+ *             specify the size of the magic by using
+ *             the 2nd byte which is "kind-length" of a TCP
+ *             header option and the "kind-length" also
+ *             includes the first 2 bytes "kind" and "kind-length"
+ *             itself as a normal TCP header option also does.
+ *
+ *             For example, to search experimental kind 254 with
+ *             2 byte magic 0xeB9F, the searchby_res should be
+ *             [ 254, 4, 0xeB, 0x9F, 0, 0, .... 0 ].
+ *
+ *             To search for the standard window scale option (3),
+ *             the searchby_res should be [ 3, 0, 0, .... 0 ].
+ *             Note, kind-length must be 0 for regular option.
+ *
+ *             Searching for No-Op (0) and End-of-Option-List (1) are
+ *             not supported.
+ *
+ *             *len* must be at least 2 bytes which is the minimal size
+ *             of a header option.
+ *
+ *             Supported flags:
+ *             * **BPF_LOAD_HDR_OPT_TCP_SYN** to search from the
+ *               saved_syn packet or the just-received syn packet.
+ *
+ *     Return
+ *             >0 when found, the header option is copied to *searchby_res*.
+ *             The return value is the total length copied.
+ *
+ *             **-EINVAL** If param is invalid
+ *
+ *             **-ENOMSG** The option is not found
+ *
+ *             **-ENOENT** No syn packet available when
+ *                         **BPF_LOAD_HDR_OPT_TCP_SYN** is used
+ *
+ *             **-ENOSPC** Not enough space.  Only *len* number of
+ *                         bytes are copied.
+ *
+ *             **-EFAULT** Cannot parse the header options in the packet
+ *
+ *             **-EPERM** This helper cannot be used under the
+ *                        current sock_ops->op.
+ *
+ * long bpf_store_hdr_opt(struct bpf_sock_ops *skops, const void *from, u32 len, u64 flags)
+ *     Description
+ *             Store header option.  The data will be copied
+ *             from buffer *from* with length *len* to the TCP header.
+ *
+ *             The buffer *from* should have the whole option that
+ *             includes the kind, kind-length, and the actual
+ *             option data.  The *len* must be at least kind-length
+ *             long.  The kind-length does not have to be 4 byte
+ *             aligned.  The kernel will take care of the padding
+ *             and setting the 4 bytes aligned value to th->doff.
+ *
+ *             This helper will check for duplicated option
+ *             by searching the same option in the outgoing skb.
+ *
+ *             This helper can only be called during
+ *             BPF_SOCK_OPS_WRITE_HDR_OPT_CB.
+ *
+ *     Return
+ *             0 on success, or negative error in case of failure:
+ *
+ *             **-EINVAL** If param is invalid
+ *
+ *             **-ENOSPC** Not enough space in the header.
+ *                         Nothing has been written
+ *
+ *             **-EEXIST** The option has already existed
+ *
+ *             **-EFAULT** Cannot parse the existing header options
+ *
+ *             **-EPERM** This helper cannot be used under the
+ *                        current sock_ops->op.
+ *
+ * long bpf_reserve_hdr_opt(struct bpf_sock_ops *skops, u32 len, u64 flags)
+ *     Description
+ *             Reserve *len* bytes for the bpf header option.  The
+ *             space will be used by bpf_store_hdr_opt() later in
+ *             BPF_SOCK_OPS_WRITE_HDR_OPT_CB.
+ *
+ *             If bpf_reserve_hdr_opt() is called multiple times,
+ *             the total number of bytes will be reserved.
+ *
+ *             This helper can only be called during
+ *             BPF_SOCK_OPS_HDR_OPT_LEN_CB.
+ *
+ *     Return
+ *             0 on success, or negative error in case of failure:
+ *
+ *             **-EINVAL** if param is invalid
+ *
+ *             **-ENOSPC** Not enough space in the header.
+ *
+ *             **-EPERM** This helper cannot be used under the
+ *                        current sock_ops->op.
+ *
+ * void *bpf_inode_storage_get(struct bpf_map *map, void *inode, void *value, u64 flags)
+ *     Description
+ *             Get a bpf_local_storage from an *inode*.
+ *
+ *             Logically, it could be thought of as getting the value from
+ *             a *map* with *inode* as the **key**.  From this
+ *             perspective,  the usage is not much different from
+ *             **bpf_map_lookup_elem**\ (*map*, **&**\ *inode*) except this
+ *             helper enforces the key must be an inode and the map must also
+ *             be a **BPF_MAP_TYPE_INODE_STORAGE**.
+ *
+ *             Underneath, the value is stored locally at *inode* instead of
+ *             the *map*.  The *map* is used as the bpf-local-storage
+ *             "type". The bpf-local-storage "type" (i.e. the *map*) is
+ *             searched against all bpf_local_storage residing at *inode*.
+ *
+ *             An optional *flags* (**BPF_LOCAL_STORAGE_GET_F_CREATE**) can be
+ *             used such that a new bpf_local_storage will be
+ *             created if one does not exist.  *value* can be used
+ *             together with **BPF_LOCAL_STORAGE_GET_F_CREATE** to specify
+ *             the initial value of a bpf_local_storage.  If *value* is
+ *             **NULL**, the new bpf_local_storage will be zero initialized.
+ *     Return
+ *             A bpf_local_storage pointer is returned on success.
+ *
+ *             **NULL** if not found or there was an error in adding
+ *             a new bpf_local_storage.
+ *
+ * int bpf_inode_storage_delete(struct bpf_map *map, void *inode)
+ *     Description
+ *             Delete a bpf_local_storage from an *inode*.
+ *     Return
+ *             0 on success.
+ *
+ *             **-ENOENT** if the bpf_local_storage cannot be found.
+ *
+ * long bpf_d_path(struct path *path, char *buf, u32 sz)
+ *     Description
+ *             Return full path for given 'struct path' object, which
+ *             needs to be the kernel BTF 'path' object. The path is
+ *             returned in the provided buffer 'buf' of size 'sz' and
+ *             is zero terminated.
+ *
+ *     Return
+ *             On success, the strictly positive length of the string,
+ *             including the trailing NUL character. On error, a negative
+ *             value.
  */
 #define __BPF_FUNC_MAPPER(FN)          \
        FN(unspec),                     \
@@ -3539,6 +3705,12 @@ union bpf_attr {
        FN(skc_to_tcp_request_sock),    \
        FN(skc_to_udp6_sock),           \
        FN(get_task_stack),             \
+       FN(load_hdr_opt),               \
+       FN(store_hdr_opt),              \
+       FN(reserve_hdr_opt),            \
+       FN(inode_storage_get),          \
+       FN(inode_storage_delete),       \
+       FN(d_path),                     \
        /* */
 
 /* integer value in 'imm' field of BPF_CALL instruction selects which helper
@@ -3648,9 +3820,13 @@ enum {
        BPF_F_SYSCTL_BASE_NAME          = (1ULL << 0),
 };
 
-/* BPF_FUNC_sk_storage_get flags */
+/* BPF_FUNC_<kernel_obj>_storage_get flags */
 enum {
-       BPF_SK_STORAGE_GET_F_CREATE     = (1ULL << 0),
+       BPF_LOCAL_STORAGE_GET_F_CREATE  = (1ULL << 0),
+       /* BPF_SK_STORAGE_GET_F_CREATE is only kept for backward compatibility
+        * and BPF_LOCAL_STORAGE_GET_F_CREATE must be used instead.
+        */
+       BPF_SK_STORAGE_GET_F_CREATE  = BPF_LOCAL_STORAGE_GET_F_CREATE,
 };
 
 /* BPF_FUNC_read_branch_records flags. */
@@ -4071,6 +4247,13 @@ struct bpf_link_info {
                        __u64 cgroup_id;
                        __u32 attach_type;
                } cgroup;
+               struct {
+                       __aligned_u64 target_name; /* in/out: target_name buffer ptr */
+                       __u32 target_name_len;     /* in/out: target_name buffer len */
+                       union {
+                               __u32 map_id;
+                       } map;
+               } iter;
                struct  {
                        __u32 netns_ino;
                        __u32 attach_type;
@@ -4158,6 +4341,36 @@ struct bpf_sock_ops {
        __u64 bytes_received;
        __u64 bytes_acked;
        __bpf_md_ptr(struct bpf_sock *, sk);
+       /* [skb_data, skb_data_end) covers the whole TCP header.
+        *
+        * BPF_SOCK_OPS_PARSE_HDR_OPT_CB: The packet received
+        * BPF_SOCK_OPS_HDR_OPT_LEN_CB:   Not useful because the
+        *                                header has not been written.
+        * BPF_SOCK_OPS_WRITE_HDR_OPT_CB: The header and options have
+        *                                been written so far.
+        * BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB:  The SYNACK that concludes
+        *                                      the 3WHS.
+        * BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB: The ACK that concludes
+        *                                      the 3WHS.
+        *
+        * bpf_load_hdr_opt() can also be used to read a particular option.
+        */
+       __bpf_md_ptr(void *, skb_data);
+       __bpf_md_ptr(void *, skb_data_end);
+       __u32 skb_len;          /* The total length of a packet.
+                                * It includes the header, options,
+                                * and payload.
+                                */
+       __u32 skb_tcp_flags;    /* tcp_flags of the header.  It provides
+                                * an easy way to check for tcp_flags
+                                * without parsing skb_data.
+                                *
+                                * In particular, the skb_tcp_flags
+                                * will still be available in
+                                * BPF_SOCK_OPS_HDR_OPT_LEN even though
+                                * the outgoing header has not
+                                * been written yet.
+                                */
 };
 
 /* Definitions for bpf_sock_ops_cb_flags */
@@ -4166,8 +4379,51 @@ enum {
        BPF_SOCK_OPS_RETRANS_CB_FLAG    = (1<<1),
        BPF_SOCK_OPS_STATE_CB_FLAG      = (1<<2),
        BPF_SOCK_OPS_RTT_CB_FLAG        = (1<<3),
+       /* Call bpf for all received TCP headers.  The bpf prog will be
+        * called under sock_ops->op == BPF_SOCK_OPS_PARSE_HDR_OPT_CB
+        *
+        * Please refer to the comment in BPF_SOCK_OPS_PARSE_HDR_OPT_CB
+        * for the header option related helpers that will be useful
+        * to the bpf programs.
+        *
+        * It could be used at the client/active side (i.e. connect() side)
+        * when the server told it that the server was in syncookie
+        * mode and required the active side to resend the bpf-written
+        * options.  The active side can keep writing the bpf-options until
+        * it received a valid packet from the server side to confirm
+        * the earlier packet (and options) has been received.  The later
+        * example patch is using it like this at the active side when the
+        * server is in syncookie mode.
+        *
+        * The bpf prog will usually turn this off in the common cases.
+        */
+       BPF_SOCK_OPS_PARSE_ALL_HDR_OPT_CB_FLAG  = (1<<4),
+       /* Call bpf when kernel has received a header option that
+        * the kernel cannot handle.  The bpf prog will be called under
+        * sock_ops->op == BPF_SOCK_OPS_PARSE_HDR_OPT_CB.
+        *
+        * Please refer to the comment in BPF_SOCK_OPS_PARSE_HDR_OPT_CB
+        * for the header option related helpers that will be useful
+        * to the bpf programs.
+        */
+       BPF_SOCK_OPS_PARSE_UNKNOWN_HDR_OPT_CB_FLAG = (1<<5),
+       /* Call bpf when the kernel is writing header options for the
+        * outgoing packet.  The bpf prog will first be called
+        * to reserve space in a skb under
+        * sock_ops->op == BPF_SOCK_OPS_HDR_OPT_LEN_CB.  Then
+        * the bpf prog will be called to write the header option(s)
+        * under sock_ops->op == BPF_SOCK_OPS_WRITE_HDR_OPT_CB.
+        *
+        * Please refer to the comment in BPF_SOCK_OPS_HDR_OPT_LEN_CB
+        * and BPF_SOCK_OPS_WRITE_HDR_OPT_CB for the header option
+        * related helpers that will be useful to the bpf programs.
+        *
+        * The kernel gets its chance to reserve space and write
+        * options first before the BPF program does.
+        */
+       BPF_SOCK_OPS_WRITE_HDR_OPT_CB_FLAG = (1<<6),
 /* Mask of all currently supported cb flags */
-       BPF_SOCK_OPS_ALL_CB_FLAGS       = 0xF,
+       BPF_SOCK_OPS_ALL_CB_FLAGS       = 0x7F,
 };
 
 /* List of known BPF sock_ops operators.
@@ -4223,6 +4479,63 @@ enum {
                                         */
        BPF_SOCK_OPS_RTT_CB,            /* Called on every RTT.
                                         */
+       BPF_SOCK_OPS_PARSE_HDR_OPT_CB,  /* Parse the header option.
+                                        * It will be called to handle
+                                        * the packets received at
+                                        * an already established
+                                        * connection.
+                                        *
+                                        * sock_ops->skb_data:
+                                        * Referring to the received skb.
+                                        * It covers the TCP header only.
+                                        *
+                                        * bpf_load_hdr_opt() can also
+                                        * be used to search for a
+                                        * particular option.
+                                        */
+       BPF_SOCK_OPS_HDR_OPT_LEN_CB,    /* Reserve space for writing the
+                                        * header option later in
+                                        * BPF_SOCK_OPS_WRITE_HDR_OPT_CB.
+                                        * Arg1: bool want_cookie. (in
+                                        *       writing SYNACK only)
+                                        *
+                                        * sock_ops->skb_data:
+                                        * Not available because no header has
+                                        * been written yet.
+                                        *
+                                        * sock_ops->skb_tcp_flags:
+                                        * The tcp_flags of the
+                                        * outgoing skb. (e.g. SYN, ACK, FIN).
+                                        *
+                                        * bpf_reserve_hdr_opt() should
+                                        * be used to reserve space.
+                                        */
+       BPF_SOCK_OPS_WRITE_HDR_OPT_CB,  /* Write the header options
+                                        * Arg1: bool want_cookie. (in
+                                        *       writing SYNACK only)
+                                        *
+                                        * sock_ops->skb_data:
+                                        * Referring to the outgoing skb.
+                                        * It covers the TCP header
+                                        * that has already been written
+                                        * by the kernel and the
+                                        * earlier bpf-progs.
+                                        *
+                                        * sock_ops->skb_tcp_flags:
+                                        * The tcp_flags of the outgoing
+                                        * skb. (e.g. SYN, ACK, FIN).
+                                        *
+                                        * bpf_store_hdr_opt() should
+                                        * be used to write the
+                                        * option.
+                                        *
+                                        * bpf_load_hdr_opt() can also
+                                        * be used to search for a
+                                        * particular option that
+                                        * has already been written
+                                        * by the kernel or the
+                                        * earlier bpf-progs.
+                                        */
 };
 
 /* List of TCP states. There is a build check in net/ipv4/tcp.c to detect
@@ -4250,6 +4563,63 @@ enum {
 enum {
        TCP_BPF_IW              = 1001, /* Set TCP initial congestion window */
        TCP_BPF_SNDCWND_CLAMP   = 1002, /* Set sndcwnd_clamp */
+       TCP_BPF_DELACK_MAX      = 1003, /* Max delay ack in usecs */
+       TCP_BPF_RTO_MIN         = 1004, /* Min delay ack in usecs */
+       /* Copy the SYN pkt to optval
+        *
+        * BPF_PROG_TYPE_SOCK_OPS only.  It is similar to the
+        * bpf_getsockopt(TCP_SAVED_SYN) but it does not limit
+        * to only getting from the saved_syn.  It can either get the
+        * syn packet from:
+        *
+        * 1. the just-received SYN packet (only available when writing the
+        *    SYNACK).  It will be useful when it is not necessary to
+        *    save the SYN packet for latter use.  It is also the only way
+        *    to get the SYN during syncookie mode because the syn
+        *    packet cannot be saved during syncookie.
+        *
+        * OR
+        *
+        * 2. the earlier saved syn which was done by
+        *    bpf_setsockopt(TCP_SAVE_SYN).
+        *
+        * The bpf_getsockopt(TCP_BPF_SYN*) option will hide where the
+        * SYN packet is obtained.
+        *
+        * If the bpf-prog does not need the IP[46] header,  the
+        * bpf-prog can avoid parsing the IP header by using
+        * TCP_BPF_SYN.  Otherwise, the bpf-prog can get both
+        * IP[46] and TCP header by using TCP_BPF_SYN_IP.
+        *
+        *      >0: Total number of bytes copied
+        * -ENOSPC: Not enough space in optval. Only optlen number of
+        *          bytes is copied.
+        * -ENOENT: The SYN skb is not available now and the earlier SYN pkt
+        *          is not saved by setsockopt(TCP_SAVE_SYN).
+        */
+       TCP_BPF_SYN             = 1005, /* Copy the TCP header */
+       TCP_BPF_SYN_IP          = 1006, /* Copy the IP[46] and TCP header */
+       TCP_BPF_SYN_MAC         = 1007, /* Copy the MAC, IP[46], and TCP header */
+};
+
+enum {
+       BPF_LOAD_HDR_OPT_TCP_SYN = (1ULL << 0),
+};
+
+/* args[0] value during BPF_SOCK_OPS_HDR_OPT_LEN_CB and
+ * BPF_SOCK_OPS_WRITE_HDR_OPT_CB.
+ */
+enum {
+       BPF_WRITE_HDR_TCP_CURRENT_MSS = 1,      /* Kernel is finding the
+                                                * total option spaces
+                                                * required for an established
+                                                * sk in order to calculate the
+                                                * MSS.  No skb is actually
+                                                * sent.
+                                                */
+       BPF_WRITE_HDR_TCP_SYNACK_COOKIE = 2,    /* Kernel is in syncookie mode
+                                                * when sending a SYN.
+                                                */
 };
 
 struct bpf_perf_event_value {