Add kern_listen(), kern_shutdown(), and kern_socket(), and use them

instead of their sys_*() counterparts in various compats. The svr4
is left untouched, because there's no point.

Reviewed by:	ed@, kib@
MFC after:	2 weeks
Sponsored by:	DARPA, AFRL
Differential Revision:	https://reviews.freebsd.org/D9367
This commit is contained in:
trasz 2017-01-30 12:57:22 +00:00
parent 3612389ea7
commit a546306498
5 changed files with 57 additions and 67 deletions

View File

@ -100,9 +100,6 @@ cloudabi_sys_fd_create1(struct thread *td,
struct cloudabi_sys_fd_create1_args *uap)
{
struct filecaps fcaps = {};
struct socket_args socket_args = {
.domain = AF_UNIX,
};
switch (uap->type) {
case CLOUDABI_FILETYPE_POLL:
@ -113,14 +110,11 @@ cloudabi_sys_fd_create1(struct thread *td,
CAP_MMAP_RWX);
return (kern_shm_open(td, SHM_ANON, O_RDWR, 0, &fcaps));
case CLOUDABI_FILETYPE_SOCKET_DGRAM:
socket_args.type = SOCK_DGRAM;
return (sys_socket(td, &socket_args));
return (kern_socket(td, AF_UNIX, SOCK_DGRAM, 0));
case CLOUDABI_FILETYPE_SOCKET_SEQPACKET:
socket_args.type = SOCK_SEQPACKET;
return (sys_socket(td, &socket_args));
return (kern_socket(td, AF_UNIX, SOCK_SEQPACKET, 0));
case CLOUDABI_FILETYPE_SOCKET_STREAM:
socket_args.type = SOCK_STREAM;
return (sys_socket(td, &socket_args));
return (kern_socket(td, AF_UNIX, SOCK_STREAM, 0));
default:
return (EINVAL);
}

View File

@ -35,7 +35,6 @@ __FBSDID("$FreeBSD$");
#include <sys/socket.h>
#include <sys/socketvar.h>
#include <sys/syscallsubr.h>
#include <sys/sysproto.h>
#include <sys/systm.h>
#include <sys/un.h>
@ -165,37 +164,31 @@ int
cloudabi_sys_sock_listen(struct thread *td,
struct cloudabi_sys_sock_listen_args *uap)
{
struct listen_args listen_args = {
.s = uap->sock,
.backlog = uap->backlog,
};
return (sys_listen(td, &listen_args));
return (kern_listen(td, uap->sock, uap->backlog));
}
int
cloudabi_sys_sock_shutdown(struct thread *td,
struct cloudabi_sys_sock_shutdown_args *uap)
{
struct shutdown_args shutdown_args = {
.s = uap->sock,
};
int how;
switch (uap->how) {
case CLOUDABI_SHUT_RD:
shutdown_args.how = SHUT_RD;
how = SHUT_RD;
break;
case CLOUDABI_SHUT_WR:
shutdown_args.how = SHUT_WR;
how = SHUT_WR;
break;
case CLOUDABI_SHUT_RD | CLOUDABI_SHUT_WR:
shutdown_args.how = SHUT_RDWR;
how = SHUT_RDWR;
break;
default:
return (EINVAL);
}
return (sys_shutdown(td, &shutdown_args));
return (kern_shutdown(td, uap->sock, how));
}
int

View File

@ -697,32 +697,26 @@ linux_sendto_hdrincl(struct thread *td, struct linux_sendto_args *linux_args)
int
linux_socket(struct thread *td, struct linux_socket_args *args)
{
struct socket_args /* {
int domain;
int type;
int protocol;
} */ bsd_args;
int retval_socket;
int domain, retval_socket, type;
bsd_args.protocol = args->protocol;
bsd_args.type = args->type & LINUX_SOCK_TYPE_MASK;
if (bsd_args.type < 0 || bsd_args.type > LINUX_SOCK_MAX)
type = args->type & LINUX_SOCK_TYPE_MASK;
if (type < 0 || type > LINUX_SOCK_MAX)
return (EINVAL);
retval_socket = linux_set_socket_flags(args->type & ~LINUX_SOCK_TYPE_MASK,
&bsd_args.type);
&type);
if (retval_socket != 0)
return (retval_socket);
bsd_args.domain = linux_to_bsd_domain(args->domain);
if (bsd_args.domain == -1)
domain = linux_to_bsd_domain(args->domain);
if (domain == -1)
return (EAFNOSUPPORT);
retval_socket = sys_socket(td, &bsd_args);
retval_socket = kern_socket(td, domain, type, args->protocol);
if (retval_socket)
return (retval_socket);
if (bsd_args.type == SOCK_RAW
&& (bsd_args.protocol == IPPROTO_RAW || bsd_args.protocol == 0)
&& bsd_args.domain == PF_INET) {
if (type == SOCK_RAW
&& (args->protocol == IPPROTO_RAW || args->protocol == 0)
&& domain == PF_INET) {
/* It's a raw IP socket: set the IP_HDRINCL option. */
int hdrincl;
@ -738,7 +732,7 @@ linux_socket(struct thread *td, struct linux_socket_args *args)
* For simplicity we do this unconditionally of the net.inet6.ip6.v6only
* sysctl value.
*/
if (bsd_args.domain == PF_INET6) {
if (domain == PF_INET6) {
int v6only;
v6only = 0;
@ -816,14 +810,8 @@ linux_connect(struct thread *td, struct linux_connect_args *args)
int
linux_listen(struct thread *td, struct linux_listen_args *args)
{
struct listen_args /* {
int s;
int backlog;
} */ bsd_args;
bsd_args.s = args->s;
bsd_args.backlog = args->backlog;
return (sys_listen(td, &bsd_args));
return (kern_listen(td, args->s, args->backlog));
}
static int
@ -1524,14 +1512,8 @@ linux_recvmmsg(struct thread *td, struct linux_recvmmsg_args *args)
int
linux_shutdown(struct thread *td, struct linux_shutdown_args *args)
{
struct shutdown_args /* {
int s;
int how;
} */ bsd_args;
bsd_args.s = args->s;
bsd_args.how = args->how;
return (sys_shutdown(td, &bsd_args));
return (kern_shutdown(td, args->s, args->how));
}
int

View File

@ -123,14 +123,20 @@ getsock_cap(struct thread *td, int fd, cap_rights_t *rightsp,
int
sys_socket(struct thread *td, struct socket_args *uap)
{
return (kern_socket(td, uap->domain, uap->type, uap->protocol));
}
int
kern_socket(struct thread *td, int domain, int type, int protocol)
{
struct socket *so;
struct file *fp;
int fd, error, type, oflag, fflag;
int fd, error, oflag, fflag;
AUDIT_ARG_SOCKET(uap->domain, uap->type, uap->protocol);
AUDIT_ARG_SOCKET(domain, type, protocol);
type = uap->type;
oflag = 0;
fflag = 0;
if ((type & SOCK_CLOEXEC) != 0) {
@ -143,8 +149,7 @@ sys_socket(struct thread *td, struct socket_args *uap)
}
#ifdef MAC
error = mac_socket_check_create(td->td_ucred, uap->domain, type,
uap->protocol);
error = mac_socket_check_create(td->td_ucred, domain, type, protocol);
if (error != 0)
return (error);
#endif
@ -152,8 +157,7 @@ sys_socket(struct thread *td, struct socket_args *uap)
if (error != 0)
return (error);
/* An extra reference on `fp' has been held for us by falloc(). */
error = socreate(uap->domain, &so, type, uap->protocol,
td->td_ucred, td);
error = socreate(domain, &so, type, protocol, td->td_ucred, td);
if (error != 0) {
fdclose(td, fp, fd);
} else {
@ -230,14 +234,21 @@ sys_bindat(struct thread *td, struct bindat_args *uap)
int
sys_listen(struct thread *td, struct listen_args *uap)
{
return (kern_listen(td, uap->s, uap->backlog));
}
int
kern_listen(struct thread *td, int s, int backlog)
{
struct socket *so;
struct file *fp;
cap_rights_t rights;
int error;
AUDIT_ARG_FD(uap->s);
error = getsock_cap(td, uap->s, cap_rights_init(&rights, CAP_LISTEN),
AUDIT_ARG_FD(s);
error = getsock_cap(td, s, cap_rights_init(&rights, CAP_LISTEN),
&fp, NULL, NULL);
if (error == 0) {
so = fp->f_data;
@ -245,10 +256,10 @@ sys_listen(struct thread *td, struct listen_args *uap)
error = mac_socket_check_listen(td->td_ucred, so);
if (error == 0)
#endif
error = solisten(so, uap->backlog, td);
error = solisten(so, backlog, td);
fdrop(fp, td);
}
return(error);
return (error);
}
/*
@ -1204,18 +1215,25 @@ sys_recvmsg(struct thread *td, struct recvmsg_args *uap)
int
sys_shutdown(struct thread *td, struct shutdown_args *uap)
{
return (kern_shutdown(td, uap->s, uap->how));
}
int
kern_shutdown(struct thread *td, int s, int how)
{
struct socket *so;
struct file *fp;
cap_rights_t rights;
int error;
AUDIT_ARG_FD(uap->s);
error = getsock_cap(td, uap->s, cap_rights_init(&rights, CAP_SHUTDOWN),
AUDIT_ARG_FD(s);
error = getsock_cap(td, s, cap_rights_init(&rights, CAP_SHUTDOWN),
&fp, NULL, NULL);
if (error == 0) {
so = fp->f_data;
error = soshutdown(so, uap->how);
error = soshutdown(so, how);
/*
* Previous versions did not return ENOTCONN, but 0 in
* case the socket was not connected. Some important

View File

@ -135,6 +135,7 @@ int kern_kldstat(struct thread *td, int fileid, struct kld_file_stat *stat);
int kern_kldunload(struct thread *td, int fileid, int flags);
int kern_linkat(struct thread *td, int fd1, int fd2, char *path1,
char *path2, enum uio_seg segflg, int follow);
int kern_listen(struct thread *td, int s, int backlog);
int kern_lseek(struct thread *td, int fd, off_t offset, int whence);
int kern_lutimes(struct thread *td, char *path, enum uio_seg pathseg,
struct timeval *tptr, enum uio_seg tptrseg);
@ -213,6 +214,7 @@ int kern_shmat(struct thread *td, int shmid, const void *shmaddr,
int shmflg);
int kern_shmctl(struct thread *td, int shmid, int cmd, void *buf,
size_t *bufsz);
int kern_shutdown(struct thread *td, int s, int how);
int kern_sigaction(struct thread *td, int sig, const struct sigaction *act,
struct sigaction *oact, int flags);
int kern_sigaltstack(struct thread *td, stack_t *ss, stack_t *oss);
@ -221,6 +223,7 @@ int kern_sigprocmask(struct thread *td, int how,
int kern_sigsuspend(struct thread *td, sigset_t mask);
int kern_sigtimedwait(struct thread *td, sigset_t waitset,
struct ksiginfo *ksi, struct timespec *timeout);
int kern_socket(struct thread *td, int domain, int type, int protocol);
int kern_statat(struct thread *td, int flag, int fd, char *path,
enum uio_seg pathseg, struct stat *sbp,
void (*hook)(struct vnode *vp, struct stat *sbp));