Skip to content

Commit

Permalink
Reduce code duplication by introducing linux_copyout_sockaddr()
Browse files Browse the repository at this point in the history
helper function.  No functional changes.

Reviewed by:	emaste
MFC after:	2 weeks
Sponsored by:	The FreeBSD Foundation
Differential Revision:	https://reviews.freebsd.org/D25804
  • Loading branch information
trasz committed Sep 17, 2020
1 parent 79e3da0 commit 106a784
Showing 1 changed file with 26 additions and 40 deletions.
66 changes: 26 additions & 40 deletions sys/compat/linux/linux_socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,22 @@ linux_set_socket_flags(int lflags, int *flags)
return (0);
}

static int
linux_copyout_sockaddr(const struct sockaddr *sa, void *uaddr, size_t len)
{
struct l_sockaddr *lsa;
int error;

error = bsd_to_linux_sockaddr(sa, &lsa, len);
if (error != 0)
return (error);

error = copyout(lsa, uaddr, len);
free(lsa, M_SONAME);

return (error);
}

static int
linux_sendit(struct thread *td, int s, struct msghdr *mp, int flags,
struct mbuf *control, enum uio_seg segflg)
Expand Down Expand Up @@ -609,7 +625,6 @@ static int
linux_accept_common(struct thread *td, int s, l_uintptr_t addr,
l_uintptr_t namelen, int flags)
{
struct l_sockaddr *lsa;
struct sockaddr *sa;
struct file *fp, *fp1;
int bflags, len;
Expand Down Expand Up @@ -665,10 +680,7 @@ linux_accept_common(struct thread *td, int s, l_uintptr_t addr,
}

if (len != 0) {
error = bsd_to_linux_sockaddr(sa, &lsa, len);
if (error == 0)
error = copyout(lsa, PTRIN(addr), len);
free(lsa, M_SONAME);
error = linux_copyout_sockaddr(sa, PTRIN(addr), len);

/*
* XXX: We should also copyout the len, shouldn't we?
Expand Down Expand Up @@ -704,7 +716,6 @@ linux_accept4(struct thread *td, struct linux_accept4_args *args)
int
linux_getsockname(struct thread *td, struct linux_getsockname_args *args)
{
struct l_sockaddr *lsa;
struct sockaddr *sa;
int len, error;

Expand All @@ -716,13 +727,8 @@ linux_getsockname(struct thread *td, struct linux_getsockname_args *args)
if (error != 0)
return (error);

if (len != 0) {
error = bsd_to_linux_sockaddr(sa, &lsa, len);
if (error == 0)
error = copyout(lsa, PTRIN(args->addr),
len);
free(lsa, M_SONAME);
}
if (len != 0)
error = linux_copyout_sockaddr(sa, PTRIN(args->addr), len);

free(sa, M_SONAME);
if (error == 0)
Expand All @@ -733,7 +739,6 @@ linux_getsockname(struct thread *td, struct linux_getsockname_args *args)
int
linux_getpeername(struct thread *td, struct linux_getpeername_args *args)
{
struct l_sockaddr *lsa;
struct sockaddr *sa;
int len, error;

Expand All @@ -747,13 +752,8 @@ linux_getpeername(struct thread *td, struct linux_getpeername_args *args)
if (error != 0)
return (error);

if (len != 0) {
error = bsd_to_linux_sockaddr(sa, &lsa, len);
if (error == 0)
error = copyout(lsa, PTRIN(args->addr),
len);
free(lsa, M_SONAME);
}
if (len != 0)
error = linux_copyout_sockaddr(sa, PTRIN(args->addr), len);

free(sa, M_SONAME);
if (error == 0)
Expand Down Expand Up @@ -895,7 +895,6 @@ linux_sendto(struct thread *td, struct linux_sendto_args *args)
int
linux_recvfrom(struct thread *td, struct linux_recvfrom_args *args)
{
struct l_sockaddr *lsa;
struct sockaddr *sa;
struct msghdr msg;
struct iovec aiov;
Expand Down Expand Up @@ -927,13 +926,8 @@ linux_recvfrom(struct thread *td, struct linux_recvfrom_args *args)
if (error != 0)
goto out;

if (PTRIN(args->from) != NULL) {
error = bsd_to_linux_sockaddr(sa, &lsa, msg.msg_namelen);
if (error == 0)
error = copyout(lsa, PTRIN(args->from),
msg.msg_namelen);
free(lsa, M_SONAME);
}
if (PTRIN(args->from) != NULL)
error = linux_copyout_sockaddr(sa, PTRIN(args->from), msg.msg_namelen);

if (error == 0 && PTRIN(args->fromlen) != NULL)
error = copyout(&msg.msg_namelen, PTRIN(args->fromlen),
Expand Down Expand Up @@ -1170,7 +1164,6 @@ linux_recvmsg_common(struct thread *td, l_int s, struct l_msghdr *msghdr,
struct mbuf *control = NULL;
struct mbuf **controlp;
struct timeval *ftmvl;
struct l_sockaddr *lsa;
struct sockaddr *sa;
l_timeval ltmvl;
caddr_t outbuf;
Expand Down Expand Up @@ -1216,11 +1209,8 @@ linux_recvmsg_common(struct thread *td, l_int s, struct l_msghdr *msghdr,
*/
if (msg->msg_name != NULL && msg->msg_namelen > 0) {
msg->msg_name = PTRIN(linux_msghdr.msg_name);
error = bsd_to_linux_sockaddr(sa, &lsa, msg->msg_namelen);
if (error == 0)
error = copyout(lsa, PTRIN(msg->msg_name),
msg->msg_namelen);
free(lsa, M_SONAME);
error = linux_copyout_sockaddr(sa,
PTRIN(msg->msg_name), msg->msg_namelen);
if (error != 0)
goto bad;
}
Expand Down Expand Up @@ -1505,7 +1495,6 @@ linux_getsockopt(struct thread *td, struct linux_getsockopt_args *args)
l_timeval linux_tv;
struct timeval tv;
socklen_t tv_len, xulen, len;
struct l_sockaddr *lsa;
struct sockaddr *sa;
struct xucred xu;
struct l_ucred lxu;
Expand Down Expand Up @@ -1591,10 +1580,7 @@ linux_getsockopt(struct thread *td, struct linux_getsockopt_args *args)
if (error != 0)
goto out;

error = bsd_to_linux_sockaddr(sa, &lsa, len);
if (error == 0)
error = copyout(lsa, PTRIN(args->optval), len);
free(lsa, M_SONAME);
error = linux_copyout_sockaddr(sa, PTRIN(args->optval), len);
if (error == 0)
error = copyout(&len, PTRIN(args->optlen),
sizeof(len));
Expand Down

0 comments on commit 106a784

Please sign in to comment.