github.com/inspektor-gadget/inspektor-gadget@v0.28.1/pkg/gadgets/trace/bind/tracer/bpf/bindsnoop.h (about)

     1  /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
     2  #ifndef __BINDSNOOP_H
     3  #define __BINDSNOOP_H
     4  
     5  #define TASK_COMM_LEN 16
     6  
     7  struct bind_event {
     8  	__u8 addr[16];
     9  	__u64 mount_ns_id;
    10  	__u64 timestamp;
    11  	__u64 ts_us;
    12  	__u32 pid;
    13  	__u32 bound_dev_if;
    14  	__u32 uid;
    15  	__u32 gid;
    16  	int ret;
    17  	__u16 port;
    18  	__u16 proto;
    19  	__u8 opts;
    20  	__u8 ver;
    21  	__u8 task[TASK_COMM_LEN];
    22  };
    23  
    24  union bind_options {
    25  	__u8 data;
    26  	struct {
    27  		__u8 freebind : 1;
    28  		__u8 transparent : 1;
    29  		__u8 bind_address_no_port : 1;
    30  		__u8 reuseaddress : 1;
    31  		__u8 reuseport : 1;
    32  	} fields;
    33  };
    34  
    35  
    36  /*
    37   * Highly inspired from:
    38   * https://github.com/iovisor/bcc/pull/4846/commits/a55b5a0c920f
    39   */
    40  struct inet_sock___o {
    41  	__u8 freebind: 1;
    42  	__u8 transparent: 1;
    43  	__u8 bind_address_no_port: 1;
    44  };
    45  
    46  enum {
    47  	INET_FLAGS_FREEBIND___x = 11,
    48  	INET_FLAGS_TRANSPARENT___x = 15,
    49  	INET_FLAGS_BIND_ADDRESS_NO_PORT___x = 18,
    50  };
    51  
    52  struct inet_sock___x {
    53  	unsigned long inet_flags;
    54  };
    55  
    56  #define get_inet_sock_flag(inet_sock, flag_name, flag_value) ({                \
    57  	__u8 __ret;                                                            \
    58  	if (bpf_core_field_exists(struct inet_sock___o, flag_name)) {          \
    59  		__ret = BPF_CORE_READ_BITFIELD_PROBED(                         \
    60  			(struct inet_sock___o *)inet_sock, flag_name);         \
    61  	} else {                                                               \
    62  		unsigned long __flags;                                         \
    63  		__flags = BPF_CORE_READ((struct inet_sock___x *)inet_sock,     \
    64  				      inet_flags);                             \
    65  		__ret = !!((1 << flag_value) & __flags);                       \
    66  	}                                                                      \
    67  	__ret; })
    68  
    69  static __always_inline __u8 get_inet_sock_freebind(void *inet_sock)
    70  {
    71  	return get_inet_sock_flag(inet_sock, freebind, INET_FLAGS_FREEBIND___x);
    72  }
    73  
    74  static __always_inline __u8 get_inet_sock_transparent(void *inet_sock)
    75  {
    76  	return get_inet_sock_flag(inet_sock, transparent, INET_FLAGS_TRANSPARENT___x);
    77  }
    78  
    79  static __always_inline __u8 get_inet_sock_bind_address_no_port(void *inet_sock)
    80  {
    81  	return get_inet_sock_flag(inet_sock, bind_address_no_port, INET_FLAGS_BIND_ADDRESS_NO_PORT___x);
    82  }
    83  
    84  #endif /* __BINDSNOOP_H */