# recvfrom ___________________________________________________
#
# long sys_recvfrom(int fd,
#		  void __user * ubuf,
#		  size_t size,
#		  unsigned flags,
#		  struct sockaddr __user *addr,
#		  int __user *addr_len)
#
# On some kernels, sys_recv() is just a wrapper for sys_recvfrom(). We
# check the syscall value (and the 'call' value in the case of
# sys_socketcall) to make sure we're really in a recvfrom() call.
#

@define _SYSCALL_RECVFROM_NAME
%(
	name = "recvfrom"
%)

@define _SYSCALL_RECVFROM_ARGSTR
%(
	argstr = sprintf("%d, %p, %u, %s, %p, %p", s, buf_uaddr, len,
	                 flags_str, addr_uaddr, addrlen_uaddr)
%)

@define _SYSCALL_RECVFROM_REGARGS
%(
	s = int_arg(1)
	buf_uaddr = pointer_arg(2)
	len = ulong_arg(3)
	flags = uint_arg(4)
	addr_uaddr = pointer_arg(5)
	addrlen_uaddr = pointer_arg(6)
%)

@define _SYSCALL_SOCKETCALL_RECVFROM_REGARGS
%(
	__args = &@ulong_cast(pointer_arg(2))
	s = __int32(user_ulong(&@ulong_cast(__args)[0]))
	buf_uaddr = user_ulong(&@ulong_cast(__args)[1])
	len = user_ulong(&@ulong_cast(__args)[2])
	flags = __uint32(user_ulong(&@ulong_cast(__args)[3]))
	addr_uaddr = user_ulong(&@ulong_cast(__args)[4])
	addrlen_uaddr = __uint32(user_ulong(&@ulong_cast(__args)[5]))
%)

@define _SYSCALL_COMPAT_SOCKETCALL_RECVFROM_REGARGS
%(
	__args = &@uint_cast(pointer_arg(2))
	s = user_int(&@uint_cast(__args)[0])
	buf_uaddr = user_uint32(&@uint_cast(__args)[1])
	len = user_uint32(&@uint_cast(__args)[2])
	flags = user_uint32(&@uint_cast(__args)[3])
	addr_uaddr = user_uint32(&@uint_cast(__args)[4])
	addrlen_uaddr = user_uint32(&@uint_cast(__args)[5])
%)

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

# dw_recvfrom _____________________________________________________

probe dw_syscall.recvfrom = __syscall.socketcall.recvfrom ?,
	__syscall.compat_socketcall.recvfrom ?, __syscall.recvfrom ?
{
	@_SYSCALL_RECVFROM_NAME
	flags_str = _msg_flags_str(flags)
	@_SYSCALL_RECVFROM_ARGSTR
}
probe __syscall.socketcall.recvfrom = kernel.function("sys_socketcall").call ?
{
	if (__int32($call) != @const("SYS_RECVFROM")) next
	s = __int32(user_ulong(&@ulong_cast($args)[0]))
	buf_uaddr = user_ulong(&@ulong_cast($args)[1])
	len = user_ulong(&@ulong_cast($args)[2])
	flags = __uint32(user_ulong(&@ulong_cast($args)[3]))
	addr_uaddr = user_ulong(&@ulong_cast($args)[4])
	addrlen_uaddr = __uint32(user_ulong(&@ulong_cast($args)[5]))
}
probe __syscall.compat_socketcall.recvfrom =
	kernel.function("compat_sys_socketcall").call ?
{
	if (__int32($call) != @const("SYS_RECVFROM")) next
	s = user_int(&@uint_cast($args)[0])
	buf_uaddr = user_uint32(&@uint_cast($args)[1])
	len = user_uint32(&@uint_cast($args)[2])
	flags = user_uint32(&@uint_cast($args)[3])
	addr_uaddr = user_uint32(&@uint_cast($args)[4])
	addrlen_uaddr = user_uint32(&@uint_cast($args)[5])
}
probe __syscall.recvfrom = kernel.function("sys_recvfrom").call ?
{
	@__syscall_gate(@const("__NR_recvfrom"))
	s = __int32($fd)
	buf_uaddr = $ubuf
	len = __ulong($size)
	flags = __uint32($flags)
	addr_uaddr = $addr
	addrlen_uaddr = $addr_len
}
probe dw_syscall.recvfrom.return = __syscall.socketcall.return ?,
	__syscall.recvfrom.return ?
{
	@_SYSCALL_RECVFROM_NAME
	@SYSC_RETVALSTR($return)
}
probe __syscall.socketcall.return = kernel.function("sys_socketcall").return ?,
	kernel.function("compat_sys_socketcall").return ?
{
	if (__int32(@entry($call)) != @const("SYS_RECVFROM")) next
}
probe __syscall.recvfrom.return = kernel.function("sys_recvfrom").return ?
{
	@__syscall_gate(@const("__NR_recvfrom"))
}

# nd_recvfrom _____________________________________________________

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

probe nd1_syscall.recvfrom = __nd1_syscall.recvfrom ?,
      	__nd1_syscall.socketcall.recvfrom ?,
	__nd1_syscall.compat_socketcall.recvfrom ? 
{
	@_SYSCALL_RECVFROM_NAME
	flags_str = _msg_flags_str(flags)
	@_SYSCALL_RECVFROM_ARGSTR
}
probe __nd1_syscall.recvfrom = kprobe.function("sys_recvfrom").call ?
{
	@__syscall_gate(@const("__NR_recvfrom"))
	asmlinkage()
	@_SYSCALL_RECVFROM_REGARGS
}
probe __nd1_syscall.socketcall.recvfrom =
	kprobe.function("sys_socketcall").call ?
{
	asmlinkage()
	if (int_arg(1) != @const("SYS_RECVFROM")) next
	@_SYSCALL_SOCKETCALL_RECVFROM_REGARGS
}
probe __nd1_syscall.compat_socketcall.recvfrom =
	kprobe.function("compat_sys_socketcall").call ?
{
	asmlinkage()
	if (int_arg(1) != @const("SYS_RECVFROM")) next
	@_SYSCALL_COMPAT_SOCKETCALL_RECVFROM_REGARGS
}

/* kernel 4.17+ */
probe nd2_syscall.recvfrom = __nd2_syscall.recvfrom ?,
	__nd2_syscall.socketcall.recvfrom ?,
	__nd2_syscall.compat_socketcall.recvfrom ?
{
	@_SYSCALL_RECVFROM_NAME
	flags_str = _msg_flags_str(flags)
	@_SYSCALL_RECVFROM_ARGSTR
}
probe __nd2_syscall.recvfrom = kprobe.function(@arch_syscall_prefix "sys_recvfrom") ?
{
	__set_syscall_pt_regs(pointer_arg(1))
	@_SYSCALL_RECVFROM_REGARGS
}
probe __nd2_syscall.socketcall.recvfrom = kprobe.function(@arch_syscall_prefix "sys_socketcall").call ?
{
	__set_syscall_pt_regs(pointer_arg(1))
	if (int_arg(1) != @const("SYS_RECVFROM")) next;
	@_SYSCALL_SOCKETCALL_RECVFROM_REGARGS
}
probe __nd2_syscall.compat_socketcall.recvfrom =
	kprobe.function(@arch_syscall_prefix "compat_sys_socketcall").call ?
{
	__set_syscall_pt_regs(pointer_arg(1))
	if (int_arg(1) != @const("SYS_RECVFROM")) next;
	@_SYSCALL_COMPAT_SOCKETCALL_RECVFROM_REGARGS
}

/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.recvfrom = __tp_syscall.recvfrom ?,
	__tp_syscall.socketcall.recvfrom ?
{
	@_SYSCALL_RECVFROM_NAME
	flags_str = _msg_flags_str(flags)
	@_SYSCALL_RECVFROM_ARGSTR
}
probe __tp_syscall.recvfrom = kernel.trace("sys_enter")
{
	__set_syscall_pt_regs($regs)
	@__syscall_gate(@const("__NR_recvfrom"))
	@_SYSCALL_RECVFROM_REGARGS
}
probe __tp_syscall.socketcall.recvfrom = 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_RECVFROM")) next;
	if (@__compat_task) {
		@_SYSCALL_COMPAT_SOCKETCALL_RECVFROM_REGARGS
	}
	else {
		@_SYSCALL_SOCKETCALL_RECVFROM_REGARGS
	}
}

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

probe nd1_syscall.recvfrom.return = __nd1_syscall.recvfrom.return ?,
      __nd1_syscall.socketcall.return ?
{
	@_SYSCALL_RECVFROM_NAME
	@SYSC_RETVALSTR(returnval())
}
probe __nd1_syscall.recvfrom.return = kprobe.function("sys_recvfrom").return ?
{
	@__syscall_gate(@const("__NR_recvfrom"))
}
probe __nd1_syscall.socketcall.return =
	kprobe.function("sys_socketcall").return ?,
	kprobe.function("compat_sys_socketcall").return ?
{
	if (@entry(__asmlinkage_int_arg(1)) != @const("SYS_RECVFROM")) next;
}

/* kernel 4.17+ */
probe nd2_syscall.recvfrom.return =
	kprobe.function(@arch_syscall_prefix "sys_recvfrom").return ?,
	__nd2_syscall.socketcall.recvfrom.return ?
{
	@_SYSCALL_RECVFROM_NAME
	@SYSC_RETVALSTR(returnval())
}
probe __nd2_syscall.socketcall.recvfrom.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_RECVFROM")) next;
}

/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.recvfrom.return = __tp_syscall.recvfrom.return ?,
	__tp_syscall.socketcall.recvfrom.return ?
{
	@_SYSCALL_RECVFROM_NAME
	@SYSC_RETVALSTR($ret)
}
probe __tp_syscall.recvfrom.return = kernel.trace("sys_exit")
{
	@__syscall_gate(@const("__NR_recvfrom"))
}
probe __tp_syscall.socketcall.recvfrom.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_RECVFROM")) next;
}
