2 * Copyright (c) 1995 Søren Schmidt
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer
10 * in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * $FreeBSD: src/sys/compat/linux/linux_socket.c,v 1.19.2.8 2001/11/07 20:33:55 marcel Exp $
31 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/sysproto.h>
35 #include <sys/fcntl.h>
37 #include <sys/kern_syscall.h>
38 #include <sys/socket.h>
39 #include <sys/socketvar.h>
41 #include <sys/malloc.h>
45 #include <sys/mplock2.h>
47 #include <netinet/in.h>
48 #include <netinet/in_systm.h>
49 #include <netinet/ip.h>
51 #include <arch_linux/linux.h>
52 #include <arch_linux/linux_proto.h>
53 #include "linux_socket.h"
54 #include "linux_util.h"
57 * Copyin a sockaddr structure provided by a Linux binary. Linux uses
58 * the 4.3BSD sockaddr structure which has no sa_len field. We must
59 * pass 4.4BSD sockaddr structures when we call native functions in the
60 * BSD kernel. This function does the conversion for us.
62 * Also, our socket calls require the sockaddr structure length to agree
63 * with the address family. Linux does not, so we must force it.
65 * This function should only need to be called from linux_connect()
69 linux_getsockaddr(struct sockaddr **namp, struct sockaddr *uaddr, size_t len)
72 uint16_t family; /* XXX: must match Linux sockaddr */
78 if (len > SOCK_MAXADDRLEN)
80 error = copyin(uaddr, &family, sizeof(family));
85 * Force the sa_len field to match the address family.
89 sa_len = sizeof(struct sockaddr_in);
92 sa_len = sizeof(struct sockaddr_in6);
96 * This is the default behavior of the old
97 * linux_to_bsd_namelen() function. NOTE! The
98 * minimum length we allocate must cover sa->sa_len and
101 sa_len = offsetof(struct sockaddr, sa_data[0]);
107 sa = kmalloc(sa_len, M_SONAME, M_WAITOK);
108 error = copyin(uaddr, sa, sa_len);
113 * Convert to the 4.4BSD sockaddr structure.
115 sa->sa_family = *(sa_family_t *)sa;
124 * Transform a 4.4BSD sockaddr structure into a Linux sockaddr structure
125 * and copy it out to a user address.
128 linux_copyout_sockaddr(struct sockaddr *sa, struct sockaddr *uaddr, int sa_len)
132 if (sa_len < (int)sizeof(u_short))
135 *(u_short *)sa = sa->sa_family;
136 error = copyout(sa, uaddr, sa_len);
142 linux_to_bsd_domain(int domain)
146 case LINUX_AF_UNSPEC:
161 linux_to_bsd_sockopt_level(int level)
165 case LINUX_SOL_SOCKET:
172 linux_to_bsd_ip_sockopt(int opt)
180 case LINUX_IP_OPTIONS:
182 case LINUX_IP_MULTICAST_IF:
183 return (IP_MULTICAST_IF);
184 case LINUX_IP_MULTICAST_TTL:
185 return (IP_MULTICAST_TTL);
186 case LINUX_IP_MULTICAST_LOOP:
187 return (IP_MULTICAST_LOOP);
188 case LINUX_IP_ADD_MEMBERSHIP:
189 return (IP_ADD_MEMBERSHIP);
190 case LINUX_IP_DROP_MEMBERSHIP:
191 return (IP_DROP_MEMBERSHIP);
192 case LINUX_IP_HDRINCL:
199 linux_to_bsd_so_sockopt(int opt)
205 case LINUX_SO_REUSEADDR:
206 return (SO_REUSEADDR);
211 case LINUX_SO_DONTROUTE:
212 return (SO_DONTROUTE);
213 case LINUX_SO_BROADCAST:
214 return (SO_BROADCAST);
215 case LINUX_SO_SNDBUF:
217 case LINUX_SO_RCVBUF:
219 case LINUX_SO_KEEPALIVE:
220 return (SO_KEEPALIVE);
221 case LINUX_SO_OOBINLINE:
222 return (SO_OOBINLINE);
223 case LINUX_SO_LINGER:
225 case LINUX_SO_PEERCRED:
226 return (LOCAL_PEERCRED);
232 linux_to_bsd_msg_flags(int flags)
236 if (flags & LINUX_MSG_OOB)
237 ret_flags |= MSG_OOB;
238 if (flags & LINUX_MSG_PEEK)
239 ret_flags |= MSG_PEEK;
240 if (flags & LINUX_MSG_DONTROUTE)
241 ret_flags |= MSG_DONTROUTE;
242 if (flags & LINUX_MSG_CTRUNC)
243 ret_flags |= MSG_CTRUNC;
244 if (flags & LINUX_MSG_TRUNC)
245 ret_flags |= MSG_TRUNC;
246 if (flags & LINUX_MSG_DONTWAIT)
247 ret_flags |= MSG_DONTWAIT;
248 if (flags & LINUX_MSG_EOR)
249 ret_flags |= MSG_EOR;
250 if (flags & LINUX_MSG_WAITALL)
251 ret_flags |= MSG_WAITALL;
252 #if 0 /* not handled */
253 if (flags & LINUX_MSG_PROXY)
255 if (flags & LINUX_MSG_FIN)
257 if (flags & LINUX_MSG_SYN)
259 if (flags & LINUX_MSG_CONFIRM)
261 if (flags & LINUX_MSG_RST)
263 if (flags & LINUX_MSG_ERRQUEUE)
265 if (flags & LINUX_MSG_NOSIGNAL)
271 struct linux_socket_args {
278 linux_socket(struct linux_socket_args *args, int *res)
280 struct linux_socket_args linux_args;
282 int error, domain, optval;
284 error = copyin(args, &linux_args, sizeof(linux_args));
288 domain = linux_to_bsd_domain(linux_args.domain);
292 error = kern_socket(domain, linux_args.type, linux_args.protocol, res);
294 /* Copy back the return value from socket() */
295 if (error == 0 && linux_args.type == SOCK_RAW &&
296 (linux_args.protocol == IPPROTO_RAW || linux_args.protocol == 0) &&
297 linux_args.domain == AF_INET) {
298 /* It's a raw IP socket: set the IP_HDRINCL option. */
300 sopt.sopt_dir = SOPT_SET;
301 sopt.sopt_level = IPPROTO_IP;
302 sopt.sopt_name = IP_HDRINCL;
303 sopt.sopt_val = &optval;
304 sopt.sopt_valsize = sizeof(optval);
307 /* We ignore any error returned by setsockopt() */
308 kern_setsockopt(*res, &sopt);
314 struct linux_bind_args {
316 struct sockaddr *name;
321 linux_bind(struct linux_bind_args *args, int *res)
323 struct linux_bind_args linux_args;
327 error = copyin(args, &linux_args, sizeof(linux_args));
330 error = linux_getsockaddr(&sa, linux_args.name, linux_args.namelen);
334 error = kern_bind(linux_args.s, sa);
340 struct linux_connect_args {
342 struct sockaddr * name;
347 linux_connect(struct linux_connect_args *args, int *res)
349 struct thread *td = curthread; /* XXX */
350 struct proc *p = td->td_proc;
351 struct linux_connect_args linux_args;
359 error = copyin(args, &linux_args, sizeof(linux_args));
362 error = linux_getsockaddr(&sa, linux_args.name, linux_args.namelen);
366 error = kern_connect(linux_args.s, 0, sa);
369 if (error != EISCONN)
373 * Linux doesn't return EISCONN the first time it occurs,
374 * when on a non-blocking socket. Instead it returns the
375 * error getsockopt(SOL_SOCKET, SO_ERROR) would return on BSD.
377 error = holdsock(p->p_fd, linux_args.s, &fp);
381 if (fp->f_flag & FNONBLOCK) {
382 so = (struct socket *)fp->f_data;
383 if (so->so_emuldata == 0)
384 error = so->so_error;
385 so->so_emuldata = (void *)1;
391 struct linux_listen_args {
397 linux_listen(struct linux_listen_args *args, int *res)
399 struct linux_listen_args linux_args;
402 error = copyin(args, &linux_args, sizeof(linux_args));
406 error = kern_listen(linux_args.s, linux_args.backlog);
411 struct linux_accept_args {
413 struct sockaddr *addr;
418 linux_accept(struct linux_accept_args *args, int *res)
420 struct thread *td = curthread;
421 struct linux_accept_args linux_args;
422 struct sockaddr *sa = NULL;
423 union fcntl_dat dat = { 0 };
426 error = copyin(args, &linux_args, sizeof(linux_args));
430 if (linux_args.addr) {
431 error = copyin(linux_args.namelen, &sa_len, sizeof(sa_len));
435 error = kern_accept(linux_args.s, 0, &sa, &sa_len, res);
439 * Return a namelen of zero for older code which
440 * might ignore the return value from accept().
443 copyout(&sa_len, linux_args.namelen,
444 sizeof(*linux_args.namelen));
446 error = linux_copyout_sockaddr(sa, linux_args.addr,
449 error = copyout(&sa_len, linux_args.namelen,
450 sizeof(*linux_args.namelen));
456 error = kern_accept(linux_args.s, 0, NULL, 0, res);
463 * linux appears not to copy flags from the parent socket to the
464 * accepted one, so we must clear the flags in the new descriptor.
465 * Ignore any errors, because we already have an open fd.
467 kern_fcntl(*res, F_SETFL, &dat, td->td_ucred);
471 struct linux_getsockname_args {
473 struct sockaddr *addr;
478 linux_getsockname(struct linux_getsockname_args *args, int *res)
480 struct linux_getsockname_args linux_args;
481 struct sockaddr *sa = NULL;
485 error = copyin(args, &linux_args, sizeof(linux_args));
488 error = copyin(linux_args.namelen, &sa_len, sizeof(sa_len));
492 error = kern_getsockname(linux_args.s, &sa, &sa_len);
495 error = linux_copyout_sockaddr(sa, linux_args.addr, sa_len);
497 error = copyout(&sa_len, linux_args.namelen,
498 sizeof(*linux_args.namelen));
504 struct linux_getpeername_args {
506 struct sockaddr *addr;
511 linux_getpeername(struct linux_getpeername_args *args, int *res)
513 struct linux_getpeername_args linux_args;
514 struct sockaddr *sa = NULL;
517 error = copyin(args, &linux_args, sizeof(linux_args));
520 error = copyin(linux_args.namelen, &sa_len, sizeof(sa_len));
524 error = kern_getpeername(linux_args.s, &sa, &sa_len);
527 error = linux_copyout_sockaddr(sa, linux_args.addr, sa_len);
529 error = copyout(&sa_len, linux_args.namelen,
530 sizeof(*linux_args.namelen));
536 struct linux_socketpair_args {
544 linux_socketpair(struct linux_socketpair_args *args, int *res)
546 struct linux_socketpair_args linux_args;
547 int error, domain, sockv[2];
549 error = copyin(args, &linux_args, sizeof(linux_args));
553 domain = linux_to_bsd_domain(linux_args.domain);
556 error = kern_socketpair(domain, linux_args.type, linux_args.protocol,
560 error = copyout(sockv, linux_args.rsv, sizeof(sockv));
564 struct linux_send_args {
572 linux_send(struct linux_send_args *args, size_t *res)
574 struct linux_send_args linux_args;
575 struct thread *td = curthread;
580 error = copyin(args, &linux_args, sizeof(linux_args));
584 aiov.iov_base = linux_args.msg;
585 aiov.iov_len = linux_args.len;
586 auio.uio_iov = &aiov;
589 auio.uio_resid = linux_args.len;
590 auio.uio_segflg = UIO_USERSPACE;
591 auio.uio_rw = UIO_WRITE;
594 error = kern_sendmsg(linux_args.s, NULL, &auio, NULL,
595 linux_args.flags, res);
600 struct linux_recv_args {
608 linux_recv(struct linux_recv_args *args, size_t *res)
610 struct linux_recv_args linux_args;
611 struct thread *td = curthread;
616 error = copyin(args, &linux_args, sizeof(linux_args));
620 aiov.iov_base = linux_args.msg;
621 aiov.iov_len = linux_args.len;
622 auio.uio_iov = &aiov;
625 auio.uio_resid = linux_args.len;
626 auio.uio_segflg = UIO_USERSPACE;
627 auio.uio_rw = UIO_READ;
630 error = kern_recvmsg(linux_args.s, NULL, &auio, NULL,
631 &linux_args.flags, res);
636 struct linux_sendto_args {
646 linux_sendto(struct linux_sendto_args *args, size_t *res)
648 struct linux_sendto_args linux_args;
649 struct thread *td = curthread;
653 struct sockaddr *sa = NULL;
657 error = copyin(args, &linux_args, sizeof(linux_args));
662 error = linux_getsockaddr(&sa, linux_args.to,
669 * Check to see if the IP_HDRINCL option is set.
671 sopt.sopt_dir = SOPT_GET;
672 sopt.sopt_level = IPPROTO_IP;
673 sopt.sopt_name = IP_HDRINCL;
674 sopt.sopt_val = &optval;
675 sopt.sopt_valsize = sizeof(optval);
678 if (kern_getsockopt(linux_args.s, &sopt) != 0)
683 * IP_HDRINCL is not set. Package the message as usual.
685 aiov.iov_base = linux_args.msg;
686 aiov.iov_len = linux_args.len;
687 auio.uio_iov = &aiov;
690 auio.uio_resid = linux_args.len;
691 auio.uio_segflg = UIO_USERSPACE;
692 auio.uio_rw = UIO_WRITE;
696 * IP_HDRINCL is set. We must convert the beginning of
697 * the packet header so we can feed it to the BSD kernel.
701 * Check that the packet header is long enough to contain
702 * the fields of interest. This relies on the fact that
703 * the fragment offset field comes after the length field.
705 if (linux_args.len < offsetof(struct ip, ip_off))
708 msg = kmalloc(linux_args.len, M_LINUX, M_WAITOK);
709 error = copyin(linux_args.msg, msg, linux_args.len);
713 /* Fix the ip_len and ip_off fields. */
714 ((struct ip *)msg)->ip_len = linux_args.len;
715 ((struct ip *)msg)->ip_off = ntohs(((struct ip *)msg)->ip_off);
718 aiov.iov_len = linux_args.len;
719 auio.uio_iov = &aiov;
722 auio.uio_resid = linux_args.len;
723 auio.uio_segflg = UIO_SYSSPACE;
724 auio.uio_rw = UIO_WRITE;
728 error = kern_sendmsg(linux_args.s, sa, &auio, NULL,
729 linux_args.flags, res);
739 struct linux_recvfrom_args {
744 struct sockaddr *from;
749 linux_recvfrom(struct linux_recvfrom_args *args, size_t *res)
751 struct linux_recvfrom_args linux_args;
752 struct thread *td = curthread;
755 struct sockaddr *sa = NULL;
756 int error, fromlen, flags;
758 error = copyin(args, &linux_args, sizeof(linux_args));
762 if (linux_args.from && linux_args.fromlen) {
763 error = copyin(linux_args.fromlen, &fromlen, sizeof(fromlen));
771 aiov.iov_base = linux_args.buf;
772 aiov.iov_len = linux_args.len;
773 auio.uio_iov = &aiov;
776 auio.uio_resid = linux_args.len;
777 auio.uio_segflg = UIO_USERSPACE;
778 auio.uio_rw = UIO_READ;
781 flags = linux_to_bsd_msg_flags(linux_args.flags);
783 error = kern_recvmsg(linux_args.s, linux_args.from ? &sa : NULL, &auio,
786 if (error == 0 && linux_args.from) {
788 fromlen = MIN(fromlen, sa->sa_len);
789 error = linux_copyout_sockaddr(sa, linux_args.from,
794 copyout(&fromlen, linux_args.fromlen,
803 struct linux_sendmsg_args {
810 linux_sendmsg(struct linux_sendmsg_args *args, size_t *res)
812 struct linux_sendmsg_args linux_args;
813 struct thread *td = curthread;
816 struct iovec aiov[UIO_SMALLIOV], *iov = NULL;
817 struct sockaddr *sa = NULL;
818 struct mbuf *control = NULL;
821 error = copyin(args, &linux_args, sizeof(linux_args));
825 error = copyin(linux_args.msg, &msg, sizeof(msg));
830 * XXX: I'm not sure atm how this relates to dragonfly, but
831 * just in case, I put it in.
832 * Ping on linux does pass 0 in controllen which is forbidden
833 * by FreeBSD but seems to be ok on Linux. This needs some
834 * checking but now it lets ping work.
836 if (msg.msg_control && msg.msg_controllen == 0)
837 msg.msg_control = NULL;
840 * Conditionally copyin msg.msg_name.
843 error = linux_getsockaddr(&sa, msg.msg_name, msg.msg_namelen);
851 error = iovec_copyin(msg.msg_iov, &iov, aiov, msg.msg_iovlen,
856 auio.uio_iovcnt = msg.msg_iovlen;
858 auio.uio_segflg = UIO_USERSPACE;
859 auio.uio_rw = UIO_WRITE;
863 * Conditionally copyin msg.msg_control.
865 if (msg.msg_control) {
866 if (msg.msg_controllen < sizeof(struct cmsghdr) ||
867 msg.msg_controllen > MLEN) {
871 control = m_get(MB_WAIT, MT_CONTROL);
872 if (control == NULL) {
876 control->m_len = msg.msg_controllen;
877 error = copyin(msg.msg_control, mtod(control, caddr_t),
884 * Linux and BSD both support SCM_RIGHTS. If a linux binary
885 * wants anything else with an option level of SOL_SOCKET,
886 * we don't support it.
888 if (mtod(control, struct cmsghdr *)->cmsg_level ==
890 mtod(control, struct cmsghdr *)->cmsg_type !=
898 error = kern_sendmsg(linux_args.s, sa, &auio, control,
899 linux_args.flags, res);
902 iovec_free(&iov, aiov);
909 struct linux_recvmsg_args {
916 linux_recvmsg(struct linux_recvmsg_args *args, size_t *res)
918 struct linux_recvmsg_args linux_args;
919 struct thread *td = curthread;
922 struct iovec aiov[UIO_SMALLIOV], *iov = NULL;
923 struct mbuf *m, *control = NULL;
924 struct sockaddr *sa = NULL;
926 socklen_t *ufromlenp, *ucontrollenp;
927 int error, fromlen, controllen, len, flags, *uflagsp;
929 error = copyin(args, &linux_args, sizeof(linux_args));
933 error = copyin(linux_args.msg, &msg, sizeof(struct msghdr));
937 if (msg.msg_name && msg.msg_namelen < 0)
939 if (msg.msg_control && msg.msg_controllen < 0)
942 ufromlenp = (socklen_t *)((caddr_t)linux_args.msg +
943 offsetof(struct msghdr, msg_namelen));
944 ucontrollenp = (socklen_t *)((caddr_t)linux_args.msg +
945 offsetof(struct msghdr, msg_controllen));
946 uflagsp = (int *)((caddr_t)linux_args.msg +
947 offsetof(struct msghdr, msg_flags));
952 error = iovec_copyin(msg.msg_iov, &iov, aiov, msg.msg_iovlen,
957 auio.uio_iovcnt = msg.msg_iovlen;
959 auio.uio_segflg = UIO_USERSPACE;
960 auio.uio_rw = UIO_READ;
963 flags = linux_to_bsd_msg_flags(linux_args.flags);
965 error = kern_recvmsg(linux_args.s, msg.msg_name ? &sa : NULL, &auio,
966 msg.msg_control ? &control : NULL, &flags, res);
969 * Copyout msg.msg_name and msg.msg_namelen.
971 if (error == 0 && msg.msg_name) {
973 fromlen = MIN(msg.msg_namelen, sa->sa_len);
974 error = linux_copyout_sockaddr(sa, msg.msg_name,
979 error = copyout(&fromlen, ufromlenp,
984 * Copyout msg.msg_control and msg.msg_controllen.
986 if (error == 0 && msg.msg_control) {
988 * Linux and BSD both support SCM_RIGHTS. If a linux binary
989 * wants anything else with an option level of SOL_SOCKET,
990 * we don't support it.
992 if (mtod((struct mbuf *)msg.msg_control,
993 struct cmsghdr *)->cmsg_level == SOL_SOCKET &&
994 mtod((struct mbuf *)msg.msg_control,
995 struct cmsghdr *)->cmsg_type != SCM_RIGHTS) {
1000 len = msg.msg_controllen;
1002 ctlbuf = (caddr_t)msg.msg_control;
1004 while (m && len > 0) {
1005 unsigned int tocopy;
1007 if (len >= m->m_len) {
1010 msg.msg_flags |= MSG_CTRUNC;
1014 error = copyout(mtod(m, caddr_t), ctlbuf,
1023 controllen = ctlbuf - (caddr_t)msg.msg_control;
1024 error = copyout(&controllen, ucontrollenp,
1025 sizeof(*ucontrollenp));
1029 error = copyout(&flags, uflagsp, sizeof(*uflagsp));
1033 kfree(sa, M_SONAME);
1034 iovec_free(&iov, aiov);
1040 struct linux_shutdown_args {
1046 linux_shutdown(struct linux_shutdown_args *args, int *res)
1048 struct linux_shutdown_args linux_args;
1051 error = copyin(args, &linux_args, sizeof(linux_args));
1055 error = kern_shutdown(linux_args.s, linux_args.how);
1060 struct linux_setsockopt_args {
1069 linux_setsockopt(struct linux_setsockopt_args *args, int *res)
1071 struct linux_setsockopt_args linux_args;
1072 struct thread *td = curthread;
1073 struct sockopt sopt;
1076 int error, name, level;
1078 error = copyin(args, &linux_args, sizeof(linux_args));
1082 level = linux_to_bsd_sockopt_level(linux_args.level);
1085 name = linux_to_bsd_so_sockopt(linux_args.optname);
1090 error = copyin(linux_args.optval, &linux_tv,
1094 tv.tv_sec = linux_tv.tv_sec;
1095 tv.tv_usec = linux_tv.tv_usec;
1096 sopt.sopt_dir = SOPT_SET;
1097 sopt.sopt_level = level;
1098 sopt.sopt_name = name;
1099 sopt.sopt_valsize = sizeof(tv);
1100 sopt.sopt_val = &tv;
1102 return (kern_setsockopt(linux_args.s, &sopt));
1110 name = linux_to_bsd_ip_sockopt(linux_args.optname);
1113 /* Linux TCP option values match BSD's */
1114 name = linux_args.optname;
1121 return (ENOPROTOOPT);
1123 if (linux_args.optlen < 0 || linux_args.optlen > SOMAXOPT_SIZE)
1125 if (linux_args.optval != NULL && linux_args.optlen == 0)
1127 if (linux_args.optval == NULL && linux_args.optlen != 0)
1130 sopt.sopt_dir = SOPT_SET;
1131 sopt.sopt_level = level;
1132 sopt.sopt_name = name;
1133 sopt.sopt_valsize = linux_args.optlen;
1136 if (linux_args.optval) {
1137 sopt.sopt_val = kmalloc(sopt.sopt_valsize, M_TEMP, M_WAITOK);
1138 error = copyin(linux_args.optval, sopt.sopt_val, sopt.sopt_valsize);
1142 sopt.sopt_val = NULL;
1144 error = kern_setsockopt(linux_args.s, &sopt);
1147 if (linux_args.optval)
1148 error = copyout(sopt.sopt_val, linux_args.optval,
1151 if (linux_args.optval)
1152 kfree(sopt.sopt_val, M_TEMP);
1156 struct linux_getsockopt_args {
1165 linux_getsockopt(struct linux_getsockopt_args *args, int *res)
1167 struct linux_getsockopt_args linux_args;
1168 struct thread *td = curthread;
1169 struct sockopt sopt;
1174 int error, name, valsize, level;
1176 error = copyin(args, &linux_args, sizeof(linux_args));
1180 if (linux_args.optlen) {
1181 error = copyin(linux_args.optlen, &valsize, sizeof(valsize));
1188 if (valsize < 0 || valsize > SOMAXOPT_SIZE)
1190 if (linux_args.optval != NULL && valsize == 0)
1192 if (linux_args.optval == NULL && valsize != 0)
1195 level = linux_to_bsd_sockopt_level(linux_args.level);
1198 name = linux_to_bsd_so_sockopt(linux_args.optname);
1203 sopt.sopt_dir = SOPT_GET;
1204 sopt.sopt_level = level;
1205 sopt.sopt_name = name;
1206 sopt.sopt_valsize = sizeof(tv);
1208 sopt.sopt_val = &tv;
1209 error = kern_getsockopt(linux_args.s, &sopt);
1212 linux_tv.tv_sec = tv.tv_sec;
1213 linux_tv.tv_usec = tv.tv_usec;
1214 return (copyout(&linux_tv, linux_args.optval,
1218 case LOCAL_PEERCRED:
1219 if (valsize != sizeof(lxu))
1221 sopt.sopt_dir = SOPT_GET;
1222 sopt.sopt_level = level;
1223 sopt.sopt_name = name;
1224 sopt.sopt_valsize = sizeof(xu);
1226 sopt.sopt_val = &xu;
1227 error = kern_getsockopt(linux_args.s, &sopt);
1231 * XXX Use 0 for pid as the FreeBSD does not cache peer pid.
1234 lxu.uid = xu.cr_uid;
1235 lxu.gid = xu.cr_gid;
1236 return (copyout(&lxu, linux_args.optval, sizeof(lxu)));
1244 name = linux_to_bsd_ip_sockopt(linux_args.optname);
1247 /* Linux TCP option values match BSD's */
1248 name = linux_args.optname;
1255 return (EOPNOTSUPP);
1259 sopt.sopt_dir = SOPT_GET;
1260 sopt.sopt_level = level;
1261 sopt.sopt_name = name;
1262 sopt.sopt_valsize = valsize;
1265 if (linux_args.optval) {
1266 sopt.sopt_val = kmalloc(sopt.sopt_valsize, M_TEMP, M_WAITOK);
1267 error = copyin(linux_args.optval, sopt.sopt_val, sopt.sopt_valsize);
1271 sopt.sopt_val = NULL;
1273 error = kern_getsockopt(linux_args.s, &sopt);
1275 if (error == EINVAL)
1276 error = ENOPROTOOPT;
1279 valsize = sopt.sopt_valsize;
1280 error = copyout(&valsize, linux_args.optlen, sizeof(valsize));
1283 if (linux_args.optval)
1284 error = copyout(sopt.sopt_val, linux_args.optval, sopt.sopt_valsize);
1286 if (linux_args.optval)
1287 kfree(sopt.sopt_val, M_TEMP);
1295 sys_linux_socketcall(struct linux_socketcall_args *args)
1297 void *arg = (void *)args->args;
1302 switch (args->what) {
1304 error = linux_socket(arg, &args->sysmsg_result);
1307 error = linux_bind(arg, &args->sysmsg_result);
1310 error = linux_connect(arg, &args->sysmsg_result);
1313 error = linux_listen(arg, &args->sysmsg_result);
1316 error = linux_accept(arg, &args->sysmsg_result);
1318 case LINUX_GETSOCKNAME:
1319 error = linux_getsockname(arg, &args->sysmsg_result);
1321 case LINUX_GETPEERNAME:
1322 error = linux_getpeername(arg, &args->sysmsg_result);
1324 case LINUX_SOCKETPAIR:
1325 error = linux_socketpair(arg, &args->sysmsg_result);
1328 error = linux_send(arg, &args->sysmsg_szresult);
1331 error = linux_recv(arg, &args->sysmsg_szresult);
1334 error = linux_sendto(arg, &args->sysmsg_szresult);
1336 case LINUX_RECVFROM:
1337 error = linux_recvfrom(arg, &args->sysmsg_szresult);
1339 case LINUX_SHUTDOWN:
1340 error = linux_shutdown(arg, &args->sysmsg_result);
1342 case LINUX_SETSOCKOPT:
1343 error = linux_setsockopt(arg, &args->sysmsg_result);
1345 case LINUX_GETSOCKOPT:
1346 error = linux_getsockopt(arg, &args->sysmsg_result);
1349 error = linux_sendmsg(arg, &args->sysmsg_szresult);
1352 error = linux_recvmsg(arg, &args->sysmsg_szresult);
1355 uprintf("LINUX: 'socket' typ=%d not implemented\n",