# setsockopt _________________________________________________
#
# long sys_setsockopt(int fd,
#		    int level,
#		    int optname,
#		    char __user *optval,
#		    int optlen)
#

@define _SYSCALL_SETSOCKOPT_NAME
%(
	name = "setsockopt"
%)

@define _SYSCALL_SETSOCKOPT_ARGSTR
%(
	argstr = sprintf("%d, %s, %s, %p, %u", fd, level_str,
			 optname_str, optval_uaddr, optlen)
%)

@define _SYSCALL_SETSOCKOPT_REGARGS
%(
	fd = int_arg(1)
	level = int_arg(2)
	optname = int_arg(3)
	optval_uaddr = pointer_arg(4)
	optlen = uint_arg(5)
%)

@define _SYSCALL_SOCKETCALL_SETSOCKOPT_REGARGS
%(
	__args = &@ulong_cast(pointer_arg(2))
	fd = __int32(user_ulong(&@ulong_cast(__args)[0]))
	level = __int32(user_ulong(&@ulong_cast(__args)[1]))
	optname = __int32(user_ulong(&@ulong_cast(__args)[2]))
	optval_uaddr = user_ulong(&@ulong_cast(__args)[3])
	optlen = __uint32(user_ulong(&@ulong_cast(__args)[4]))
%)

@define _SYSCALL_COMPAT_SOCKETCALL_SETSOCKOPT_REGARGS
%(
	__args = &@uint_cast(pointer_arg(2))
	fd = user_int(&@uint_cast(__args)[0])
	level = user_int(&@uint_cast(__args)[1])
	optname = user_int(&@uint_cast(__args)[2])
	optval_uaddr = user_uint32(&@uint_cast(__args)[3])
	optlen = user_uint32(&@uint_cast(__args)[4])
%)

probe syscall.setsockopt = dw_syscall.setsockopt !, nd_syscall.setsockopt ? {}
probe syscall.setsockopt.return = dw_syscall.setsockopt.return !,
                                  nd_syscall.setsockopt.return ? {}

# dw_setsockopt _____________________________________________________

probe dw_syscall.setsockopt = __syscall.setsockopt ?,
	__syscall.socketcall.setsockopt ?,
	__syscall.compat_socketcall.setsockopt ?
{
	@_SYSCALL_SETSOCKOPT_NAME
	level_str = _sockopt_level_str(level)
	optname_str = _sockopt_optname_str(optname)
	@_SYSCALL_SETSOCKOPT_ARGSTR
}
probe __syscall.setsockopt = kernel.function("sys_setsockopt").call ?
{
	@__syscall_gate(@const("__NR_setsockopt"))
	fd = __int32($fd)
	level = __int32($level)
	optname = __int32($optname)
	optval_uaddr = $optval
	optlen = __uint32($optlen)
}
probe __syscall.socketcall.setsockopt = kernel.function("sys_socketcall").call ?
{
	if (__int32($call) != @const("SYS_SETSOCKOPT")) next;
	fd = __int32(user_ulong(&@ulong_cast($args)[0]))
	level = __int32(user_ulong(&@ulong_cast($args)[1]))
	optname = __int32(user_ulong(&@ulong_cast($args)[2]))
	optval_uaddr = user_ulong(&@ulong_cast($args)[3])
	optlen = __uint32(user_ulong(&@ulong_cast($args)[4]))
}
probe __syscall.compat_socketcall.setsockopt =
	kernel.function("compat_sys_socketcall").call ?
{
	if (__int32($call) != @const("SYS_SETSOCKOPT")) next;
	fd = user_int(&@uint_cast($args)[0])
	level = user_int(&@uint_cast($args)[1])
	optname = user_int(&@uint_cast($args)[2])
	optval_uaddr = user_uint32(&@uint_cast($args)[3])
	optlen = user_uint32(&@uint_cast($args)[4])
}
probe dw_syscall.setsockopt.return = __syscall.setsockopt.return ?,
      __syscall.socketcall.setsockopt.return ?
{
	@_SYSCALL_SETSOCKOPT_NAME
	@SYSC_RETVALSTR($return)
}
probe __syscall.setsockopt.return =
	kernel.function("sys_setsockopt").return ?
{
	@__syscall_gate(@const("__NR_setsockopt"))
}
probe __syscall.socketcall.setsockopt.return =
	kernel.function("sys_socketcall").return ?,
	kernel.function("compat_sys_socketcall").return ?
{
	if (__int32(@entry($call)) != @const("SYS_SETSOCKOPT")) next;
}

# nd_setsockopt _____________________________________________________

probe nd_syscall.setsockopt = nd1_syscall.setsockopt!, nd2_syscall.setsockopt!, tp_syscall.setsockopt
  { }

probe nd1_syscall.setsockopt = __nd1_syscall.setsockopt ?,
	__nd1_syscall.socketcall.setsockopt ?,
	__nd1_syscall.compat_socketcall.setsockopt ?
{
	@_SYSCALL_SETSOCKOPT_NAME
	level_str = _sockopt_level_str(level)
	optname_str = _sockopt_optname_str(optname)
	@_SYSCALL_SETSOCKOPT_ARGSTR
}
probe __nd1_syscall.setsockopt = kprobe.function("sys_setsockopt") ?
{
	asmlinkage()
	@__syscall_gate(@const("__NR_setsockopt"))
	@_SYSCALL_SETSOCKOPT_REGARGS
}
probe __nd1_syscall.socketcall.setsockopt =
	kprobe.function("sys_socketcall").call ?
{
	asmlinkage()
	if (int_arg(1) != @const("SYS_SETSOCKOPT")) next;
	@_SYSCALL_SOCKETCALL_SETSOCKOPT_REGARGS
}
probe __nd1_syscall.compat_socketcall.setsockopt =
	kprobe.function("compat_sys_socketcall").call ?
{
	asmlinkage()
	if (int_arg(1) != @const("SYS_SETSOCKOPT")) next;
	@_SYSCALL_COMPAT_SOCKETCALL_SETSOCKOPT_REGARGS
}

/* kernel 4.17+ */
probe nd2_syscall.setsockopt = __nd2_syscall.setsockopt ?,
	__nd2_syscall.socketcall.setsockopt ?,
	__nd2_syscall.compat_socketcall.setsockopt ?
{
	@_SYSCALL_SETSOCKOPT_NAME
	level_str = _sockopt_level_str(level)
	optname_str = _sockopt_optname_str(optname)
	@_SYSCALL_SETSOCKOPT_ARGSTR
}
probe __nd2_syscall.setsockopt = kprobe.function(@arch_syscall_prefix "sys_setsockopt") ?
{
	__set_syscall_pt_regs(pointer_arg(1))
	@_SYSCALL_SETSOCKOPT_REGARGS
}
probe __nd2_syscall.socketcall.setsockopt = kprobe.function(@arch_syscall_prefix "sys_socketcall").call ?
{
	__set_syscall_pt_regs(pointer_arg(1))
	if (int_arg(1) != @const("SYS_SETSOCKOPT")) next;
	@_SYSCALL_SOCKETCALL_SETSOCKOPT_REGARGS
}
probe __nd2_syscall.compat_socketcall.setsockopt =
	kprobe.function(@arch_syscall_prefix "compat_sys_socketcall").call ?
{
	__set_syscall_pt_regs(pointer_arg(1))
	if (int_arg(1) != @const("SYS_SETSOCKOPT")) next;
	@_SYSCALL_COMPAT_SOCKETCALL_SETSOCKOPT_REGARGS
}

/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.setsockopt = __tp_syscall.setsockopt ?,
	__tp_syscall.socketcall.setsockopt ?
{
	@_SYSCALL_SETSOCKOPT_NAME
	level_str = _sockopt_level_str(level)
	optname_str = _sockopt_optname_str(optname)
	@_SYSCALL_SETSOCKOPT_ARGSTR
}
probe __tp_syscall.setsockopt = kernel.trace("sys_enter")
{
	__set_syscall_pt_regs($regs)
	@__syscall_gate(@const("__NR_setsockopt"))
	@_SYSCALL_SETSOCKOPT_REGARGS
}
probe __tp_syscall.socketcall.setsockopt = kernel.trace("sys_enter")
{
	__set_syscall_pt_regs($regs)
	@__syscall_compat_gate(@const("__NR_socketcall"), @const("__NR_compat_socketcall"))
	if (int_arg(1) != @const("SYS_SETSOCKOPT")) next;
	if (@__compat_task) {
		@_SYSCALL_COMPAT_SOCKETCALL_SETSOCKOPT_REGARGS
	}
	else {
		@_SYSCALL_SOCKETCALL_SETSOCKOPT_REGARGS
	}
}

probe nd_syscall.setsockopt.return = nd1_syscall.setsockopt.return!, nd2_syscall.setsockopt.return!, tp_syscall.setsockopt.return
  { }

probe nd1_syscall.setsockopt.return = __nd1_syscall.setsockopt.return ?,
	__nd1_syscall.socketcall.setsockopt.return ?
{
	@_SYSCALL_SETSOCKOPT_NAME
	@SYSC_RETVALSTR(returnval())
}
probe __nd1_syscall.setsockopt.return =
	kprobe.function("sys_setsockopt").return ?
{
	@__syscall_gate(@const("__NR_setsockopt"))
}
probe __nd1_syscall.socketcall.setsockopt.return =
	kprobe.function("sys_socketcall").return ?,
	kprobe.function("compat_sys_socketcall").return ?
{
	if (@entry(__asmlinkage_int_arg(1)) != @const("SYS_SETSOCKOPT")) next;
}

/* kernel 4.17+ */
probe nd2_syscall.setsockopt.return =
	kprobe.function(@arch_syscall_prefix "sys_setsockopt").return ?,
	__nd2_syscall.socketcall.setsockopt.return ?
{
	@_SYSCALL_SETSOCKOPT_NAME
	@SYSC_RETVALSTR(returnval())
}
probe __nd2_syscall.socketcall.setsockopt.return =
	kprobe.function(@arch_syscall_prefix "sys_socketcall").return ?,
	kprobe.function(@arch_syscall_prefix "compat_sys_socketcall").return ?
{
	__set_syscall_pt_regs(@entry(pointer_arg(1)))
	if (int_arg(1) != @const("SYS_SETSOCKOPT")) next;
}

/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.setsockopt.return = __tp_syscall.setsockopt.return ?,
	__tp_syscall.socketcall.setsockopt.return ?
{
	@_SYSCALL_SETSOCKOPT_NAME
	@SYSC_RETVALSTR($ret)
}
probe __tp_syscall.setsockopt.return = kernel.trace("sys_exit")
{
	@__syscall_gate(@const("__NR_setsockopt"))
}
probe __tp_syscall.socketcall.setsockopt.return = kernel.trace("sys_exit")
{
	__set_syscall_pt_regs($regs)
	@__syscall_compat_gate(@const("__NR_socketcall"), @const("__NR_compat_socketcall"))
	if (int_arg(1) != @const("SYS_SETSOCKOPT")) next;
}
