]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/compat/linux/linux_socket.c
linux(4): Improve netlink diagnostics
[FreeBSD/FreeBSD.git] / sys / compat / linux / linux_socket.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 1995 Søren Schmidt
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 /* XXX we use functions that might not exist. */
33 #include "opt_compat.h"
34 #include "opt_inet6.h"
35
36 #include <sys/param.h>
37 #include <sys/proc.h>
38 #include <sys/systm.h>
39 #include <sys/sysproto.h>
40 #include <sys/capsicum.h>
41 #include <sys/fcntl.h>
42 #include <sys/file.h>
43 #include <sys/filedesc.h>
44 #include <sys/limits.h>
45 #include <sys/lock.h>
46 #include <sys/malloc.h>
47 #include <sys/mutex.h>
48 #include <sys/mbuf.h>
49 #include <sys/socket.h>
50 #include <sys/socketvar.h>
51 #include <sys/syscallsubr.h>
52 #include <sys/uio.h>
53 #include <sys/stat.h>
54 #include <sys/syslog.h>
55 #include <sys/un.h>
56 #include <sys/unistd.h>
57
58 #include <security/audit/audit.h>
59
60 #include <net/if.h>
61 #include <net/vnet.h>
62 #include <netinet/in.h>
63 #include <netinet/in_systm.h>
64 #include <netinet/ip.h>
65 #include <netinet/tcp.h>
66 #ifdef INET6
67 #include <netinet/ip6.h>
68 #include <netinet6/ip6_var.h>
69 #endif
70
71 #ifdef COMPAT_LINUX32
72 #include <machine/../linux32/linux.h>
73 #include <machine/../linux32/linux32_proto.h>
74 #else
75 #include <machine/../linux/linux.h>
76 #include <machine/../linux/linux_proto.h>
77 #endif
78 #include <compat/linux/linux_common.h>
79 #include <compat/linux/linux_file.h>
80 #include <compat/linux/linux_mib.h>
81 #include <compat/linux/linux_socket.h>
82 #include <compat/linux/linux_timer.h>
83 #include <compat/linux/linux_util.h>
84
85 static int linux_sendmsg_common(struct thread *, l_int, struct l_msghdr *,
86                                         l_uint);
87 static int linux_recvmsg_common(struct thread *, l_int, struct l_msghdr *,
88                                         l_uint, struct msghdr *);
89 static int linux_set_socket_flags(int, int *);
90
91 static int
92 linux_to_bsd_sockopt_level(int level)
93 {
94
95         if (level == LINUX_SOL_SOCKET)
96                 return (SOL_SOCKET);
97         /* Remaining values are RFC-defined protocol numbers. */
98         return (level);
99 }
100
101 static int
102 bsd_to_linux_sockopt_level(int level)
103 {
104
105         if (level == SOL_SOCKET)
106                 return (LINUX_SOL_SOCKET);
107         return (level);
108 }
109
110 static int
111 linux_to_bsd_ip_sockopt(int opt)
112 {
113
114         switch (opt) {
115         case LINUX_IP_TOS:
116                 return (IP_TOS);
117         case LINUX_IP_TTL:
118                 return (IP_TTL);
119         case LINUX_IP_OPTIONS:
120                 return (IP_OPTIONS);
121         case LINUX_IP_MULTICAST_IF:
122                 return (IP_MULTICAST_IF);
123         case LINUX_IP_MULTICAST_TTL:
124                 return (IP_MULTICAST_TTL);
125         case LINUX_IP_MULTICAST_LOOP:
126                 return (IP_MULTICAST_LOOP);
127         case LINUX_IP_ADD_MEMBERSHIP:
128                 return (IP_ADD_MEMBERSHIP);
129         case LINUX_IP_DROP_MEMBERSHIP:
130                 return (IP_DROP_MEMBERSHIP);
131         case LINUX_IP_HDRINCL:
132                 return (IP_HDRINCL);
133         }
134         return (-1);
135 }
136
137 static int
138 linux_to_bsd_ip6_sockopt(int opt)
139 {
140
141         switch (opt) {
142         case LINUX_IPV6_NEXTHOP:
143                 return (IPV6_NEXTHOP);
144         case LINUX_IPV6_UNICAST_HOPS:
145                 return (IPV6_UNICAST_HOPS);
146         case LINUX_IPV6_MULTICAST_IF:
147                 return (IPV6_MULTICAST_IF);
148         case LINUX_IPV6_MULTICAST_HOPS:
149                 return (IPV6_MULTICAST_HOPS);
150         case LINUX_IPV6_MULTICAST_LOOP:
151                 return (IPV6_MULTICAST_LOOP);
152         case LINUX_IPV6_ADD_MEMBERSHIP:
153                 return (IPV6_JOIN_GROUP);
154         case LINUX_IPV6_DROP_MEMBERSHIP:
155                 return (IPV6_LEAVE_GROUP);
156         case LINUX_IPV6_V6ONLY:
157                 return (IPV6_V6ONLY);
158         case LINUX_IPV6_DONTFRAG:
159                 return (IPV6_DONTFRAG);
160 #if 0
161         case LINUX_IPV6_CHECKSUM:
162                 return (IPV6_CHECKSUM);
163         case LINUX_IPV6_RECVPKTINFO:
164                 return (IPV6_RECVPKTINFO);
165         case LINUX_IPV6_PKTINFO:
166                 return (IPV6_PKTINFO);
167         case LINUX_IPV6_RECVHOPLIMIT:
168                 return (IPV6_RECVHOPLIMIT);
169         case LINUX_IPV6_HOPLIMIT:
170                 return (IPV6_HOPLIMIT);
171         case LINUX_IPV6_RECVHOPOPTS:
172                 return (IPV6_RECVHOPOPTS);
173         case LINUX_IPV6_HOPOPTS:
174                 return (IPV6_HOPOPTS);
175         case LINUX_IPV6_RTHDRDSTOPTS:
176                 return (IPV6_RTHDRDSTOPTS);
177         case LINUX_IPV6_RECVRTHDR:
178                 return (IPV6_RECVRTHDR);
179         case LINUX_IPV6_RTHDR:
180                 return (IPV6_RTHDR);
181         case LINUX_IPV6_RECVDSTOPTS:
182                 return (IPV6_RECVDSTOPTS);
183         case LINUX_IPV6_DSTOPTS:
184                 return (IPV6_DSTOPTS);
185         case LINUX_IPV6_RECVPATHMTU:
186                 return (IPV6_RECVPATHMTU);
187         case LINUX_IPV6_PATHMTU:
188                 return (IPV6_PATHMTU);
189 #endif
190         }
191         return (-1);
192 }
193
194 static int
195 linux_to_bsd_so_sockopt(int opt)
196 {
197
198         switch (opt) {
199         case LINUX_SO_DEBUG:
200                 return (SO_DEBUG);
201         case LINUX_SO_REUSEADDR:
202                 return (SO_REUSEADDR);
203         case LINUX_SO_TYPE:
204                 return (SO_TYPE);
205         case LINUX_SO_ERROR:
206                 return (SO_ERROR);
207         case LINUX_SO_DONTROUTE:
208                 return (SO_DONTROUTE);
209         case LINUX_SO_BROADCAST:
210                 return (SO_BROADCAST);
211         case LINUX_SO_SNDBUF:
212         case LINUX_SO_SNDBUFFORCE:
213                 return (SO_SNDBUF);
214         case LINUX_SO_RCVBUF:
215         case LINUX_SO_RCVBUFFORCE:
216                 return (SO_RCVBUF);
217         case LINUX_SO_KEEPALIVE:
218                 return (SO_KEEPALIVE);
219         case LINUX_SO_OOBINLINE:
220                 return (SO_OOBINLINE);
221         case LINUX_SO_LINGER:
222                 return (SO_LINGER);
223         case LINUX_SO_REUSEPORT:
224                 return (SO_REUSEPORT_LB);
225         case LINUX_SO_PASSCRED:
226                 return (LOCAL_CREDS_PERSISTENT);
227         case LINUX_SO_PEERCRED:
228                 return (LOCAL_PEERCRED);
229         case LINUX_SO_RCVLOWAT:
230                 return (SO_RCVLOWAT);
231         case LINUX_SO_SNDLOWAT:
232                 return (SO_SNDLOWAT);
233         case LINUX_SO_RCVTIMEO:
234                 return (SO_RCVTIMEO);
235         case LINUX_SO_SNDTIMEO:
236                 return (SO_SNDTIMEO);
237         case LINUX_SO_TIMESTAMP:
238                 return (SO_TIMESTAMP);
239         case LINUX_SO_ACCEPTCONN:
240                 return (SO_ACCEPTCONN);
241         case LINUX_SO_PROTOCOL:
242                 return (SO_PROTOCOL);
243         }
244         return (-1);
245 }
246
247 static int
248 linux_to_bsd_tcp_sockopt(int opt)
249 {
250
251         switch (opt) {
252         case LINUX_TCP_NODELAY:
253                 return (TCP_NODELAY);
254         case LINUX_TCP_MAXSEG:
255                 return (TCP_MAXSEG);
256         case LINUX_TCP_CORK:
257                 return (TCP_NOPUSH);
258         case LINUX_TCP_KEEPIDLE:
259                 return (TCP_KEEPIDLE);
260         case LINUX_TCP_KEEPINTVL:
261                 return (TCP_KEEPINTVL);
262         case LINUX_TCP_KEEPCNT:
263                 return (TCP_KEEPCNT);
264         case LINUX_TCP_MD5SIG:
265                 return (TCP_MD5SIG);
266         }
267         return (-1);
268 }
269
270 static int
271 linux_to_bsd_msg_flags(int flags)
272 {
273         int ret_flags = 0;
274
275         if (flags & LINUX_MSG_OOB)
276                 ret_flags |= MSG_OOB;
277         if (flags & LINUX_MSG_PEEK)
278                 ret_flags |= MSG_PEEK;
279         if (flags & LINUX_MSG_DONTROUTE)
280                 ret_flags |= MSG_DONTROUTE;
281         if (flags & LINUX_MSG_CTRUNC)
282                 ret_flags |= MSG_CTRUNC;
283         if (flags & LINUX_MSG_TRUNC)
284                 ret_flags |= MSG_TRUNC;
285         if (flags & LINUX_MSG_DONTWAIT)
286                 ret_flags |= MSG_DONTWAIT;
287         if (flags & LINUX_MSG_EOR)
288                 ret_flags |= MSG_EOR;
289         if (flags & LINUX_MSG_WAITALL)
290                 ret_flags |= MSG_WAITALL;
291         if (flags & LINUX_MSG_NOSIGNAL)
292                 ret_flags |= MSG_NOSIGNAL;
293 #if 0 /* not handled */
294         if (flags & LINUX_MSG_PROXY)
295                 ;
296         if (flags & LINUX_MSG_FIN)
297                 ;
298         if (flags & LINUX_MSG_SYN)
299                 ;
300         if (flags & LINUX_MSG_CONFIRM)
301                 ;
302         if (flags & LINUX_MSG_RST)
303                 ;
304         if (flags & LINUX_MSG_ERRQUEUE)
305                 ;
306 #endif
307         return (ret_flags);
308 }
309
310 static int
311 linux_to_bsd_cmsg_type(int cmsg_type)
312 {
313
314         switch (cmsg_type) {
315         case LINUX_SCM_RIGHTS:
316                 return (SCM_RIGHTS);
317         case LINUX_SCM_CREDENTIALS:
318                 return (SCM_CREDS);
319         }
320         return (-1);
321 }
322
323 static int
324 bsd_to_linux_cmsg_type(int cmsg_type)
325 {
326
327         switch (cmsg_type) {
328         case SCM_RIGHTS:
329                 return (LINUX_SCM_RIGHTS);
330         case SCM_CREDS:
331                 return (LINUX_SCM_CREDENTIALS);
332         case SCM_TIMESTAMP:
333                 return (LINUX_SCM_TIMESTAMP);
334         }
335         return (-1);
336 }
337
338 static int
339 linux_to_bsd_msghdr(struct msghdr *bhdr, const struct l_msghdr *lhdr)
340 {
341         if (lhdr->msg_controllen > INT_MAX)
342                 return (ENOBUFS);
343
344         bhdr->msg_name          = PTRIN(lhdr->msg_name);
345         bhdr->msg_namelen       = lhdr->msg_namelen;
346         bhdr->msg_iov           = PTRIN(lhdr->msg_iov);
347         bhdr->msg_iovlen        = lhdr->msg_iovlen;
348         bhdr->msg_control       = PTRIN(lhdr->msg_control);
349
350         /*
351          * msg_controllen is skipped since BSD and LINUX control messages
352          * are potentially different sizes (e.g. the cred structure used
353          * by SCM_CREDS is different between the two operating system).
354          *
355          * The caller can set it (if necessary) after converting all the
356          * control messages.
357          */
358
359         bhdr->msg_flags         = linux_to_bsd_msg_flags(lhdr->msg_flags);
360         return (0);
361 }
362
363 static int
364 bsd_to_linux_msghdr(const struct msghdr *bhdr, struct l_msghdr *lhdr)
365 {
366         lhdr->msg_name          = PTROUT(bhdr->msg_name);
367         lhdr->msg_namelen       = bhdr->msg_namelen;
368         lhdr->msg_iov           = PTROUT(bhdr->msg_iov);
369         lhdr->msg_iovlen        = bhdr->msg_iovlen;
370         lhdr->msg_control       = PTROUT(bhdr->msg_control);
371
372         /*
373          * msg_controllen is skipped since BSD and LINUX control messages
374          * are potentially different sizes (e.g. the cred structure used
375          * by SCM_CREDS is different between the two operating system).
376          *
377          * The caller can set it (if necessary) after converting all the
378          * control messages.
379          */
380
381         /* msg_flags skipped */
382         return (0);
383 }
384
385 static int
386 linux_set_socket_flags(int lflags, int *flags)
387 {
388
389         if (lflags & ~(LINUX_SOCK_CLOEXEC | LINUX_SOCK_NONBLOCK))
390                 return (EINVAL);
391         if (lflags & LINUX_SOCK_NONBLOCK)
392                 *flags |= SOCK_NONBLOCK;
393         if (lflags & LINUX_SOCK_CLOEXEC)
394                 *flags |= SOCK_CLOEXEC;
395         return (0);
396 }
397
398 static int
399 linux_copyout_sockaddr(const struct sockaddr *sa, void *uaddr, size_t len)
400 {
401         struct l_sockaddr *lsa;
402         int error;
403
404         error = bsd_to_linux_sockaddr(sa, &lsa, len);
405         if (error != 0)
406                 return (error);
407         
408         error = copyout(lsa, uaddr, len);
409         free(lsa, M_SONAME);
410
411         return (error);
412 }
413
414 static int
415 linux_sendit(struct thread *td, int s, struct msghdr *mp, int flags,
416     struct mbuf *control, enum uio_seg segflg)
417 {
418         struct sockaddr *to;
419         int error, len;
420
421         if (mp->msg_name != NULL) {
422                 len = mp->msg_namelen;
423                 error = linux_to_bsd_sockaddr(mp->msg_name, &to, &len);
424                 if (error != 0)
425                         return (error);
426                 mp->msg_name = to;
427         } else
428                 to = NULL;
429
430         error = kern_sendit(td, s, mp, linux_to_bsd_msg_flags(flags), control,
431             segflg);
432
433         if (to)
434                 free(to, M_SONAME);
435         return (error);
436 }
437
438 /* Return 0 if IP_HDRINCL is set for the given socket. */
439 static int
440 linux_check_hdrincl(struct thread *td, int s)
441 {
442         int error, optval;
443         socklen_t size_val;
444
445         size_val = sizeof(optval);
446         error = kern_getsockopt(td, s, IPPROTO_IP, IP_HDRINCL,
447             &optval, UIO_SYSSPACE, &size_val);
448         if (error != 0)
449                 return (error);
450
451         return (optval == 0);
452 }
453
454 /*
455  * Updated sendto() when IP_HDRINCL is set:
456  * tweak endian-dependent fields in the IP packet.
457  */
458 static int
459 linux_sendto_hdrincl(struct thread *td, struct linux_sendto_args *linux_args)
460 {
461 /*
462  * linux_ip_copysize defines how many bytes we should copy
463  * from the beginning of the IP packet before we customize it for BSD.
464  * It should include all the fields we modify (ip_len and ip_off).
465  */
466 #define linux_ip_copysize       8
467
468         struct ip *packet;
469         struct msghdr msg;
470         struct iovec aiov[1];
471         int error;
472
473         /* Check that the packet isn't too big or too small. */
474         if (linux_args->len < linux_ip_copysize ||
475             linux_args->len > IP_MAXPACKET)
476                 return (EINVAL);
477
478         packet = (struct ip *)malloc(linux_args->len, M_LINUX, M_WAITOK);
479
480         /* Make kernel copy of the packet to be sent */
481         if ((error = copyin(PTRIN(linux_args->msg), packet,
482             linux_args->len)))
483                 goto goout;
484
485         /* Convert fields from Linux to BSD raw IP socket format */
486         packet->ip_len = linux_args->len;
487         packet->ip_off = ntohs(packet->ip_off);
488
489         /* Prepare the msghdr and iovec structures describing the new packet */
490         msg.msg_name = PTRIN(linux_args->to);
491         msg.msg_namelen = linux_args->tolen;
492         msg.msg_iov = aiov;
493         msg.msg_iovlen = 1;
494         msg.msg_control = NULL;
495         msg.msg_flags = 0;
496         aiov[0].iov_base = (char *)packet;
497         aiov[0].iov_len = linux_args->len;
498         error = linux_sendit(td, linux_args->s, &msg, linux_args->flags,
499             NULL, UIO_SYSSPACE);
500 goout:
501         free(packet, M_LINUX);
502         return (error);
503 }
504
505 static const char *linux_netlink_names[] = {
506         [LINUX_NETLINK_ROUTE] = "ROUTE",
507         [LINUX_NETLINK_SOCK_DIAG] = "SOCK_DIAG",
508         [LINUX_NETLINK_NFLOG] = "NFLOG",
509         [LINUX_NETLINK_SELINUX] = "SELINUX",
510         [LINUX_NETLINK_AUDIT] = "AUDIT",
511         [LINUX_NETLINK_FIB_LOOKUP] = "FIB_LOOKUP",
512         [LINUX_NETLINK_NETFILTER] = "NETFILTER",
513         [LINUX_NETLINK_KOBJECT_UEVENT] = "KOBJECT_UEVENT",
514 };
515
516 int
517 linux_socket(struct thread *td, struct linux_socket_args *args)
518 {
519         int domain, retval_socket, type;
520
521         type = args->type & LINUX_SOCK_TYPE_MASK;
522         if (type < 0 || type > LINUX_SOCK_MAX)
523                 return (EINVAL);
524         retval_socket = linux_set_socket_flags(args->type & ~LINUX_SOCK_TYPE_MASK,
525                 &type);
526         if (retval_socket != 0)
527                 return (retval_socket);
528         domain = linux_to_bsd_domain(args->domain);
529         if (domain == -1) {
530                 /* Mask off SOCK_NONBLOCK / CLOEXEC for error messages. */
531                 type = args->type & LINUX_SOCK_TYPE_MASK;
532                 if (args->domain == LINUX_AF_NETLINK) {
533                         const char *nl_name;
534
535                         if (args->protocol >= 0 &&
536                             args->protocol < nitems(linux_netlink_names))
537                                 nl_name = linux_netlink_names[args->protocol];
538                         else
539                                 nl_name = NULL;
540                         if (nl_name != NULL)
541                                 linux_msg(curthread,
542                                     "unsupported socket(AF_NETLINK, %d, "
543                                     "NETLINK_%s)", type, nl_name);
544                         else
545                                 linux_msg(curthread,
546                                     "unsupported socket(AF_NETLINK, %d, %d)",
547                                     type, args->protocol);
548                 } else {
549                         linux_msg(curthread, "unsupported socket domain %d, "
550                             "type %d, protocol %d", args->domain, type,
551                             args->protocol);
552                 }
553                 return (EAFNOSUPPORT);
554         }
555
556         retval_socket = kern_socket(td, domain, type, args->protocol);
557         if (retval_socket)
558                 return (retval_socket);
559
560         if (type == SOCK_RAW
561             && (args->protocol == IPPROTO_RAW || args->protocol == 0)
562             && domain == PF_INET) {
563                 /* It's a raw IP socket: set the IP_HDRINCL option. */
564                 int hdrincl;
565
566                 hdrincl = 1;
567                 /* We ignore any error returned by kern_setsockopt() */
568                 kern_setsockopt(td, td->td_retval[0], IPPROTO_IP, IP_HDRINCL,
569                     &hdrincl, UIO_SYSSPACE, sizeof(hdrincl));
570         }
571 #ifdef INET6
572         /*
573          * Linux AF_INET6 socket has IPV6_V6ONLY setsockopt set to 0 by default
574          * and some apps depend on this. So, set V6ONLY to 0 for Linux apps.
575          * For simplicity we do this unconditionally of the net.inet6.ip6.v6only
576          * sysctl value.
577          */
578         if (domain == PF_INET6) {
579                 int v6only;
580
581                 v6only = 0;
582                 /* We ignore any error returned by setsockopt() */
583                 kern_setsockopt(td, td->td_retval[0], IPPROTO_IPV6, IPV6_V6ONLY,
584                     &v6only, UIO_SYSSPACE, sizeof(v6only));
585         }
586 #endif
587
588         return (retval_socket);
589 }
590
591 int
592 linux_bind(struct thread *td, struct linux_bind_args *args)
593 {
594         struct sockaddr *sa;
595         int error;
596
597         error = linux_to_bsd_sockaddr(PTRIN(args->name), &sa,
598             &args->namelen);
599         if (error != 0)
600                 return (error);
601
602         error = kern_bindat(td, AT_FDCWD, args->s, sa);
603         free(sa, M_SONAME);
604
605         /* XXX */
606         if (error == EADDRNOTAVAIL && args->namelen != sizeof(struct sockaddr_in))
607                 return (EINVAL);
608         return (error);
609 }
610
611 int
612 linux_connect(struct thread *td, struct linux_connect_args *args)
613 {
614         struct socket *so;
615         struct sockaddr *sa;
616         struct file *fp;
617         u_int fflag;
618         int error;
619
620         error = linux_to_bsd_sockaddr(PTRIN(args->name), &sa,
621             &args->namelen);
622         if (error != 0)
623                 return (error);
624
625         error = kern_connectat(td, AT_FDCWD, args->s, sa);
626         free(sa, M_SONAME);
627         if (error != EISCONN)
628                 return (error);
629
630         /*
631          * Linux doesn't return EISCONN the first time it occurs,
632          * when on a non-blocking socket. Instead it returns the
633          * error getsockopt(SOL_SOCKET, SO_ERROR) would return on BSD.
634          */
635         error = getsock_cap(td, args->s, &cap_connect_rights,
636             &fp, &fflag, NULL);
637         if (error != 0)
638                 return (error);
639
640         error = EISCONN;
641         so = fp->f_data;
642         if (fflag & FNONBLOCK) {
643                 SOCK_LOCK(so);
644                 if (so->so_emuldata == 0)
645                         error = so->so_error;
646                 so->so_emuldata = (void *)1;
647                 SOCK_UNLOCK(so);
648         }
649         fdrop(fp, td);
650
651         return (error);
652 }
653
654 int
655 linux_listen(struct thread *td, struct linux_listen_args *args)
656 {
657
658         return (kern_listen(td, args->s, args->backlog));
659 }
660
661 static int
662 linux_accept_common(struct thread *td, int s, l_uintptr_t addr,
663     l_uintptr_t namelen, int flags)
664 {
665         struct sockaddr *sa;
666         struct file *fp, *fp1;
667         int bflags, len;
668         struct socket *so;
669         int error, error1;
670
671         bflags = 0;
672         fp = NULL;
673         sa = NULL;
674
675         error = linux_set_socket_flags(flags, &bflags);
676         if (error != 0)
677                 return (error);
678
679         if (PTRIN(addr) == NULL) {
680                 len = 0;
681                 error = kern_accept4(td, s, NULL, NULL, bflags, NULL);
682         } else {
683                 error = copyin(PTRIN(namelen), &len, sizeof(len));
684                 if (error != 0)
685                         return (error);
686                 if (len < 0)
687                         return (EINVAL);
688                 error = kern_accept4(td, s, &sa, &len, bflags, &fp);
689         }
690
691         /*
692          * Translate errno values into ones used by Linux.
693          */
694         if (error != 0) {
695                 /*
696                  * XXX. This is wrong, different sockaddr structures
697                  * have different sizes.
698                  */
699                 switch (error) {
700                 case EFAULT:
701                         if (namelen != sizeof(struct sockaddr_in))
702                                 error = EINVAL;
703                         break;
704                 case EINVAL:
705                         error1 = getsock_cap(td, s, &cap_accept_rights, &fp1, NULL, NULL);
706                         if (error1 != 0) {
707                                 error = error1;
708                                 break;
709                         }
710                         so = fp1->f_data;
711                         if (so->so_type == SOCK_DGRAM)
712                                 error = EOPNOTSUPP;
713                         fdrop(fp1, td);
714                         break;
715                 }
716                 return (error);
717         }
718
719         if (len != 0) {
720                 error = linux_copyout_sockaddr(sa, PTRIN(addr), len);
721
722                 /*
723                  * XXX: We should also copyout the len, shouldn't we?
724                  */
725
726                 if (error != 0) {
727                         fdclose(td, fp, td->td_retval[0]);
728                         td->td_retval[0] = 0;
729                 }
730         }
731         if (fp != NULL)
732                 fdrop(fp, td);
733         free(sa, M_SONAME);
734         return (error);
735 }
736
737 int
738 linux_accept(struct thread *td, struct linux_accept_args *args)
739 {
740
741         return (linux_accept_common(td, args->s, args->addr,
742             args->namelen, 0));
743 }
744
745 int
746 linux_accept4(struct thread *td, struct linux_accept4_args *args)
747 {
748
749         return (linux_accept_common(td, args->s, args->addr,
750             args->namelen, args->flags));
751 }
752
753 int
754 linux_getsockname(struct thread *td, struct linux_getsockname_args *args)
755 {
756         struct sockaddr *sa;
757         int len, error;
758
759         error = copyin(PTRIN(args->namelen), &len, sizeof(len));
760         if (error != 0)
761                 return (error);
762
763         error = kern_getsockname(td, args->s, &sa, &len);
764         if (error != 0)
765                 return (error);
766
767         if (len != 0)
768                 error = linux_copyout_sockaddr(sa, PTRIN(args->addr), len);
769
770         free(sa, M_SONAME);
771         if (error == 0)
772                 error = copyout(&len, PTRIN(args->namelen), sizeof(len));
773         return (error);
774 }
775
776 int
777 linux_getpeername(struct thread *td, struct linux_getpeername_args *args)
778 {
779         struct sockaddr *sa;
780         int len, error;
781
782         error = copyin(PTRIN(args->namelen), &len, sizeof(len));
783         if (error != 0)
784                 return (error);
785         if (len < 0)
786                 return (EINVAL);
787
788         error = kern_getpeername(td, args->s, &sa, &len);
789         if (error != 0)
790                 return (error);
791
792         if (len != 0)
793                 error = linux_copyout_sockaddr(sa, PTRIN(args->addr), len);
794
795         free(sa, M_SONAME);
796         if (error == 0)
797                 error = copyout(&len, PTRIN(args->namelen), sizeof(len));
798         return (error);
799 }
800
801 int
802 linux_socketpair(struct thread *td, struct linux_socketpair_args *args)
803 {
804         int domain, error, sv[2], type;
805
806         domain = linux_to_bsd_domain(args->domain);
807         if (domain != PF_LOCAL)
808                 return (EAFNOSUPPORT);
809         type = args->type & LINUX_SOCK_TYPE_MASK;
810         if (type < 0 || type > LINUX_SOCK_MAX)
811                 return (EINVAL);
812         error = linux_set_socket_flags(args->type & ~LINUX_SOCK_TYPE_MASK,
813             &type);
814         if (error != 0)
815                 return (error);
816         if (args->protocol != 0 && args->protocol != PF_UNIX) {
817                 /*
818                  * Use of PF_UNIX as protocol argument is not right,
819                  * but Linux does it.
820                  * Do not map PF_UNIX as its Linux value is identical
821                  * to FreeBSD one.
822                  */
823                 return (EPROTONOSUPPORT);
824         }
825         error = kern_socketpair(td, domain, type, 0, sv);
826         if (error != 0)
827                 return (error);
828         error = copyout(sv, PTRIN(args->rsv), 2 * sizeof(int));
829         if (error != 0) {
830                 (void)kern_close(td, sv[0]);
831                 (void)kern_close(td, sv[1]);
832         }
833         return (error);
834 }
835
836 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
837 struct linux_send_args {
838         register_t s;
839         register_t msg;
840         register_t len;
841         register_t flags;
842 };
843
844 static int
845 linux_send(struct thread *td, struct linux_send_args *args)
846 {
847         struct sendto_args /* {
848                 int s;
849                 caddr_t buf;
850                 int len;
851                 int flags;
852                 caddr_t to;
853                 int tolen;
854         } */ bsd_args;
855         struct file *fp;
856         int error, fflag;
857
858         bsd_args.s = args->s;
859         bsd_args.buf = (caddr_t)PTRIN(args->msg);
860         bsd_args.len = args->len;
861         bsd_args.flags = args->flags;
862         bsd_args.to = NULL;
863         bsd_args.tolen = 0;
864         error = sys_sendto(td, &bsd_args);
865         if (error == ENOTCONN) {
866                 /*
867                  * Linux doesn't return ENOTCONN for non-blocking sockets.
868                  * Instead it returns the EAGAIN.
869                  */
870                 error = getsock_cap(td, args->s, &cap_send_rights, &fp,
871                     &fflag, NULL);
872                 if (error == 0) {
873                         if (fflag & FNONBLOCK)
874                                 error = EAGAIN;
875                         fdrop(fp, td);
876                 }
877         }
878         return (error);
879 }
880
881 struct linux_recv_args {
882         register_t s;
883         register_t msg;
884         register_t len;
885         register_t flags;
886 };
887
888 static int
889 linux_recv(struct thread *td, struct linux_recv_args *args)
890 {
891         struct recvfrom_args /* {
892                 int s;
893                 caddr_t buf;
894                 int len;
895                 int flags;
896                 struct sockaddr *from;
897                 socklen_t fromlenaddr;
898         } */ bsd_args;
899
900         bsd_args.s = args->s;
901         bsd_args.buf = (caddr_t)PTRIN(args->msg);
902         bsd_args.len = args->len;
903         bsd_args.flags = linux_to_bsd_msg_flags(args->flags);
904         bsd_args.from = NULL;
905         bsd_args.fromlenaddr = 0;
906         return (sys_recvfrom(td, &bsd_args));
907 }
908 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
909
910 int
911 linux_sendto(struct thread *td, struct linux_sendto_args *args)
912 {
913         struct msghdr msg;
914         struct iovec aiov;
915
916         if (linux_check_hdrincl(td, args->s) == 0)
917                 /* IP_HDRINCL set, tweak the packet before sending */
918                 return (linux_sendto_hdrincl(td, args));
919
920         msg.msg_name = PTRIN(args->to);
921         msg.msg_namelen = args->tolen;
922         msg.msg_iov = &aiov;
923         msg.msg_iovlen = 1;
924         msg.msg_control = NULL;
925         msg.msg_flags = 0;
926         aiov.iov_base = PTRIN(args->msg);
927         aiov.iov_len = args->len;
928         return (linux_sendit(td, args->s, &msg, args->flags, NULL,
929             UIO_USERSPACE));
930 }
931
932 int
933 linux_recvfrom(struct thread *td, struct linux_recvfrom_args *args)
934 {
935         struct sockaddr *sa;
936         struct msghdr msg;
937         struct iovec aiov;
938         int error, fromlen;
939
940         if (PTRIN(args->fromlen) != NULL) {
941                 error = copyin(PTRIN(args->fromlen), &fromlen,
942                     sizeof(fromlen));
943                 if (error != 0)
944                         return (error);
945                 if (fromlen < 0)
946                         return (EINVAL);
947                 sa = malloc(fromlen, M_SONAME, M_WAITOK);
948         } else {
949                 fromlen = 0;
950                 sa = NULL;
951         }
952
953         msg.msg_name = sa;
954         msg.msg_namelen = fromlen;
955         msg.msg_iov = &aiov;
956         msg.msg_iovlen = 1;
957         aiov.iov_base = PTRIN(args->buf);
958         aiov.iov_len = args->len;
959         msg.msg_control = 0;
960         msg.msg_flags = linux_to_bsd_msg_flags(args->flags);
961
962         error = kern_recvit(td, args->s, &msg, UIO_SYSSPACE, NULL);
963         if (error != 0)
964                 goto out;
965
966         if (PTRIN(args->from) != NULL)
967                 error = linux_copyout_sockaddr(sa, PTRIN(args->from), msg.msg_namelen);
968
969         if (error == 0 && PTRIN(args->fromlen) != NULL)
970                 error = copyout(&msg.msg_namelen, PTRIN(args->fromlen),
971                     sizeof(msg.msg_namelen));
972 out:
973         free(sa, M_SONAME);
974         return (error);
975 }
976
977 static int
978 linux_sendmsg_common(struct thread *td, l_int s, struct l_msghdr *msghdr,
979     l_uint flags)
980 {
981         struct cmsghdr *cmsg;
982         struct mbuf *control;
983         struct msghdr msg;
984         struct l_cmsghdr linux_cmsg;
985         struct l_cmsghdr *ptr_cmsg;
986         struct l_msghdr linux_msghdr;
987         struct iovec *iov;
988         socklen_t datalen;
989         struct sockaddr *sa;
990         struct socket *so;
991         sa_family_t sa_family;
992         struct file *fp;
993         void *data;
994         l_size_t len;
995         l_size_t clen;
996         int error, fflag;
997
998         error = copyin(msghdr, &linux_msghdr, sizeof(linux_msghdr));
999         if (error != 0)
1000                 return (error);
1001
1002         /*
1003          * Some Linux applications (ping) define a non-NULL control data
1004          * pointer, but a msg_controllen of 0, which is not allowed in the
1005          * FreeBSD system call interface.  NULL the msg_control pointer in
1006          * order to handle this case.  This should be checked, but allows the
1007          * Linux ping to work.
1008          */
1009         if (PTRIN(linux_msghdr.msg_control) != NULL &&
1010             linux_msghdr.msg_controllen == 0)
1011                 linux_msghdr.msg_control = PTROUT(NULL);
1012
1013         error = linux_to_bsd_msghdr(&msg, &linux_msghdr);
1014         if (error != 0)
1015                 return (error);
1016
1017 #ifdef COMPAT_LINUX32
1018         error = linux32_copyiniov(PTRIN(msg.msg_iov), msg.msg_iovlen,
1019             &iov, EMSGSIZE);
1020 #else
1021         error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
1022 #endif
1023         if (error != 0)
1024                 return (error);
1025
1026         control = NULL;
1027
1028         error = kern_getsockname(td, s, &sa, &datalen);
1029         if (error != 0)
1030                 goto bad;
1031         sa_family = sa->sa_family;
1032         free(sa, M_SONAME);
1033
1034         if (flags & LINUX_MSG_OOB) {
1035                 error = EOPNOTSUPP;
1036                 if (sa_family == AF_UNIX)
1037                         goto bad;
1038
1039                 error = getsock_cap(td, s, &cap_send_rights, &fp,
1040                     &fflag, NULL);
1041                 if (error != 0)
1042                         goto bad;
1043                 so = fp->f_data;
1044                 if (so->so_type != SOCK_STREAM)
1045                         error = EOPNOTSUPP;
1046                 fdrop(fp, td);
1047                 if (error != 0)
1048                         goto bad;
1049         }
1050
1051         if (linux_msghdr.msg_controllen >= sizeof(struct l_cmsghdr)) {
1052                 error = ENOBUFS;
1053                 control = m_get(M_WAITOK, MT_CONTROL);
1054                 MCLGET(control, M_WAITOK);
1055                 data = mtod(control, void *);
1056                 datalen = 0;
1057
1058                 ptr_cmsg = PTRIN(linux_msghdr.msg_control);
1059                 clen = linux_msghdr.msg_controllen;
1060                 do {
1061                         error = copyin(ptr_cmsg, &linux_cmsg,
1062                             sizeof(struct l_cmsghdr));
1063                         if (error != 0)
1064                                 goto bad;
1065
1066                         error = EINVAL;
1067                         if (linux_cmsg.cmsg_len < sizeof(struct l_cmsghdr) ||
1068                             linux_cmsg.cmsg_len > clen)
1069                                 goto bad;
1070
1071                         if (datalen + CMSG_HDRSZ > MCLBYTES)
1072                                 goto bad;
1073
1074                         /*
1075                          * Now we support only SCM_RIGHTS and SCM_CRED,
1076                          * so return EINVAL in any other cmsg_type
1077                          */
1078                         cmsg = data;
1079                         cmsg->cmsg_type =
1080                             linux_to_bsd_cmsg_type(linux_cmsg.cmsg_type);
1081                         cmsg->cmsg_level =
1082                             linux_to_bsd_sockopt_level(linux_cmsg.cmsg_level);
1083                         if (cmsg->cmsg_type == -1
1084                             || cmsg->cmsg_level != SOL_SOCKET) {
1085                                 linux_msg(curthread,
1086                                     "unsupported sendmsg cmsg level %d type %d",
1087                                     linux_cmsg.cmsg_level, linux_cmsg.cmsg_type);
1088                                 goto bad;
1089                         }
1090
1091                         /*
1092                          * Some applications (e.g. pulseaudio) attempt to
1093                          * send ancillary data even if the underlying protocol
1094                          * doesn't support it which is not allowed in the
1095                          * FreeBSD system call interface.
1096                          */
1097                         if (sa_family != AF_UNIX)
1098                                 goto next;
1099
1100                         if (cmsg->cmsg_type == SCM_CREDS) {
1101                                 len = sizeof(struct cmsgcred);
1102                                 if (datalen + CMSG_SPACE(len) > MCLBYTES)
1103                                         goto bad;
1104
1105                                 /*
1106                                  * The lower levels will fill in the structure
1107                                  */
1108                                 memset(CMSG_DATA(data), 0, len);
1109                         } else {
1110                                 len = linux_cmsg.cmsg_len - L_CMSG_HDRSZ;
1111                                 if (datalen + CMSG_SPACE(len) < datalen ||
1112                                     datalen + CMSG_SPACE(len) > MCLBYTES)
1113                                         goto bad;
1114
1115                                 error = copyin(LINUX_CMSG_DATA(ptr_cmsg),
1116                                     CMSG_DATA(data), len);
1117                                 if (error != 0)
1118                                         goto bad;
1119                         }
1120
1121                         cmsg->cmsg_len = CMSG_LEN(len);
1122                         data = (char *)data + CMSG_SPACE(len);
1123                         datalen += CMSG_SPACE(len);
1124
1125 next:
1126                         if (clen <= LINUX_CMSG_ALIGN(linux_cmsg.cmsg_len))
1127                                 break;
1128
1129                         clen -= LINUX_CMSG_ALIGN(linux_cmsg.cmsg_len);
1130                         ptr_cmsg = (struct l_cmsghdr *)((char *)ptr_cmsg +
1131                             LINUX_CMSG_ALIGN(linux_cmsg.cmsg_len));
1132                 } while(clen >= sizeof(struct l_cmsghdr));
1133
1134                 control->m_len = datalen;
1135                 if (datalen == 0) {
1136                         m_freem(control);
1137                         control = NULL;
1138                 }
1139         }
1140
1141         msg.msg_iov = iov;
1142         msg.msg_flags = 0;
1143         error = linux_sendit(td, s, &msg, flags, control, UIO_USERSPACE);
1144         control = NULL;
1145
1146 bad:
1147         m_freem(control);
1148         free(iov, M_IOV);
1149         return (error);
1150 }
1151
1152 int
1153 linux_sendmsg(struct thread *td, struct linux_sendmsg_args *args)
1154 {
1155
1156         return (linux_sendmsg_common(td, args->s, PTRIN(args->msg),
1157             args->flags));
1158 }
1159
1160 int
1161 linux_sendmmsg(struct thread *td, struct linux_sendmmsg_args *args)
1162 {
1163         struct l_mmsghdr *msg;
1164         l_uint retval;
1165         int error, datagrams;
1166
1167         if (args->vlen > UIO_MAXIOV)
1168                 args->vlen = UIO_MAXIOV;
1169
1170         msg = PTRIN(args->msg);
1171         datagrams = 0;
1172         while (datagrams < args->vlen) {
1173                 error = linux_sendmsg_common(td, args->s, &msg->msg_hdr,
1174                     args->flags);
1175                 if (error != 0)
1176                         break;
1177
1178                 retval = td->td_retval[0];
1179                 error = copyout(&retval, &msg->msg_len, sizeof(msg->msg_len));
1180                 if (error != 0)
1181                         break;
1182                 ++msg;
1183                 ++datagrams;
1184         }
1185         if (error == 0)
1186                 td->td_retval[0] = datagrams;
1187         return (error);
1188 }
1189
1190 static int
1191 linux_recvmsg_common(struct thread *td, l_int s, struct l_msghdr *msghdr,
1192     l_uint flags, struct msghdr *msg)
1193 {
1194         struct cmsghdr *cm;
1195         struct cmsgcred *cmcred;
1196         struct l_cmsghdr *linux_cmsg = NULL;
1197         struct l_ucred linux_ucred;
1198         socklen_t datalen, maxlen, outlen;
1199         struct l_msghdr linux_msghdr;
1200         struct iovec *iov, *uiov;
1201         struct mbuf *control = NULL;
1202         struct mbuf **controlp;
1203         struct timeval *ftmvl;
1204         struct sockaddr *sa;
1205         l_timeval ltmvl;
1206         caddr_t outbuf;
1207         void *data;
1208         int error, i, fd, fds, *fdp;
1209
1210         error = copyin(msghdr, &linux_msghdr, sizeof(linux_msghdr));
1211         if (error != 0)
1212                 return (error);
1213
1214         error = linux_to_bsd_msghdr(msg, &linux_msghdr);
1215         if (error != 0)
1216                 return (error);
1217
1218 #ifdef COMPAT_LINUX32
1219         error = linux32_copyiniov(PTRIN(msg->msg_iov), msg->msg_iovlen,
1220             &iov, EMSGSIZE);
1221 #else
1222         error = copyiniov(msg->msg_iov, msg->msg_iovlen, &iov, EMSGSIZE);
1223 #endif
1224         if (error != 0)
1225                 return (error);
1226
1227         if (msg->msg_name != NULL && msg->msg_namelen > 0) {
1228                 msg->msg_namelen = min(msg->msg_namelen, SOCK_MAXADDRLEN);
1229                 sa = malloc(msg->msg_namelen, M_SONAME, M_WAITOK);
1230                 msg->msg_name = sa;
1231         } else {
1232                 sa = NULL;
1233                 msg->msg_name = NULL;
1234         }
1235
1236         uiov = msg->msg_iov;
1237         msg->msg_iov = iov;
1238         controlp = (msg->msg_control != NULL) ? &control : NULL;
1239         error = kern_recvit(td, s, msg, UIO_SYSSPACE, controlp);
1240         msg->msg_iov = uiov;
1241         if (error != 0)
1242                 goto bad;
1243
1244         /*
1245          * Note that kern_recvit() updates msg->msg_namelen.
1246          */
1247         if (msg->msg_name != NULL && msg->msg_namelen > 0) {
1248                 msg->msg_name = PTRIN(linux_msghdr.msg_name);
1249                 error = linux_copyout_sockaddr(sa,
1250                     PTRIN(msg->msg_name), msg->msg_namelen);
1251                 if (error != 0)
1252                         goto bad;
1253         }
1254
1255         error = bsd_to_linux_msghdr(msg, &linux_msghdr);
1256         if (error != 0)
1257                 goto bad;
1258
1259         maxlen = linux_msghdr.msg_controllen;
1260         linux_msghdr.msg_controllen = 0;
1261         if (control) {
1262                 linux_cmsg = malloc(L_CMSG_HDRSZ, M_LINUX, M_WAITOK | M_ZERO);
1263
1264                 msg->msg_control = mtod(control, struct cmsghdr *);
1265                 msg->msg_controllen = control->m_len;
1266
1267                 cm = CMSG_FIRSTHDR(msg);
1268                 outbuf = PTRIN(linux_msghdr.msg_control);
1269                 outlen = 0;
1270                 while (cm != NULL) {
1271                         linux_cmsg->cmsg_type =
1272                             bsd_to_linux_cmsg_type(cm->cmsg_type);
1273                         linux_cmsg->cmsg_level =
1274                             bsd_to_linux_sockopt_level(cm->cmsg_level);
1275                         if (linux_cmsg->cmsg_type == -1 ||
1276                             cm->cmsg_level != SOL_SOCKET) {
1277                                 linux_msg(curthread,
1278                                     "unsupported recvmsg cmsg level %d type %d",
1279                                     cm->cmsg_level, cm->cmsg_type);
1280                                 error = EINVAL;
1281                                 goto bad;
1282                         }
1283
1284                         data = CMSG_DATA(cm);
1285                         datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
1286
1287                         switch (cm->cmsg_type) {
1288                         case SCM_RIGHTS:
1289                                 if (flags & LINUX_MSG_CMSG_CLOEXEC) {
1290                                         fds = datalen / sizeof(int);
1291                                         fdp = data;
1292                                         for (i = 0; i < fds; i++) {
1293                                                 fd = *fdp++;
1294                                                 (void)kern_fcntl(td, fd,
1295                                                     F_SETFD, FD_CLOEXEC);
1296                                         }
1297                                 }
1298                                 break;
1299
1300                         case SCM_CREDS:
1301                                 /*
1302                                  * Currently LOCAL_CREDS is never in
1303                                  * effect for Linux so no need to worry
1304                                  * about sockcred
1305                                  */
1306                                 if (datalen != sizeof(*cmcred)) {
1307                                         error = EMSGSIZE;
1308                                         goto bad;
1309                                 }
1310                                 cmcred = (struct cmsgcred *)data;
1311                                 bzero(&linux_ucred, sizeof(linux_ucred));
1312                                 linux_ucred.pid = cmcred->cmcred_pid;
1313                                 linux_ucred.uid = cmcred->cmcred_uid;
1314                                 linux_ucred.gid = cmcred->cmcred_gid;
1315                                 data = &linux_ucred;
1316                                 datalen = sizeof(linux_ucred);
1317                                 break;
1318
1319                         case SCM_TIMESTAMP:
1320                                 if (datalen != sizeof(struct timeval)) {
1321                                         error = EMSGSIZE;
1322                                         goto bad;
1323                                 }
1324                                 ftmvl = (struct timeval *)data;
1325                                 ltmvl.tv_sec = ftmvl->tv_sec;
1326                                 ltmvl.tv_usec = ftmvl->tv_usec;
1327                                 data = &ltmvl;
1328                                 datalen = sizeof(ltmvl);
1329                                 break;
1330                         }
1331
1332                         if (outlen + LINUX_CMSG_LEN(datalen) > maxlen) {
1333                                 if (outlen == 0) {
1334                                         error = EMSGSIZE;
1335                                         goto bad;
1336                                 } else {
1337                                         linux_msghdr.msg_flags |= LINUX_MSG_CTRUNC;
1338                                         m_dispose_extcontrolm(control);
1339                                         goto out;
1340                                 }
1341                         }
1342
1343                         linux_cmsg->cmsg_len = LINUX_CMSG_LEN(datalen);
1344
1345                         error = copyout(linux_cmsg, outbuf, L_CMSG_HDRSZ);
1346                         if (error != 0)
1347                                 goto bad;
1348                         outbuf += L_CMSG_HDRSZ;
1349
1350                         error = copyout(data, outbuf, datalen);
1351                         if (error != 0)
1352                                 goto bad;
1353
1354                         outbuf += LINUX_CMSG_ALIGN(datalen);
1355                         outlen += LINUX_CMSG_LEN(datalen);
1356
1357                         cm = CMSG_NXTHDR(msg, cm);
1358                 }
1359                 linux_msghdr.msg_controllen = outlen;
1360         }
1361
1362 out:
1363         error = copyout(&linux_msghdr, msghdr, sizeof(linux_msghdr));
1364
1365 bad:
1366         if (control != NULL) {
1367                 if (error != 0)
1368                         m_dispose_extcontrolm(control);
1369                 m_freem(control);
1370         }
1371         free(iov, M_IOV);
1372         free(linux_cmsg, M_LINUX);
1373         free(sa, M_SONAME);
1374
1375         return (error);
1376 }
1377
1378 int
1379 linux_recvmsg(struct thread *td, struct linux_recvmsg_args *args)
1380 {
1381         struct msghdr bsd_msg;
1382
1383         return (linux_recvmsg_common(td, args->s, PTRIN(args->msg),
1384             args->flags, &bsd_msg));
1385 }
1386
1387 int
1388 linux_recvmmsg(struct thread *td, struct linux_recvmmsg_args *args)
1389 {
1390         struct l_mmsghdr *msg;
1391         struct msghdr bsd_msg;
1392         struct l_timespec lts;
1393         struct timespec ts, tts;
1394         l_uint retval;
1395         int error, datagrams;
1396
1397         if (args->timeout) {
1398                 error = copyin(args->timeout, &lts, sizeof(struct l_timespec));
1399                 if (error != 0)
1400                         return (error);
1401                 error = linux_to_native_timespec(&ts, &lts);
1402                 if (error != 0)
1403                         return (error);
1404                 getnanotime(&tts);
1405                 timespecadd(&tts, &ts, &tts);
1406         }
1407
1408         msg = PTRIN(args->msg);
1409         datagrams = 0;
1410         while (datagrams < args->vlen) {
1411                 error = linux_recvmsg_common(td, args->s, &msg->msg_hdr,
1412                     args->flags & ~LINUX_MSG_WAITFORONE, &bsd_msg);
1413                 if (error != 0)
1414                         break;
1415
1416                 retval = td->td_retval[0];
1417                 error = copyout(&retval, &msg->msg_len, sizeof(msg->msg_len));
1418                 if (error != 0)
1419                         break;
1420                 ++msg;
1421                 ++datagrams;
1422
1423                 /*
1424                  * MSG_WAITFORONE turns on MSG_DONTWAIT after one packet.
1425                  */
1426                 if (args->flags & LINUX_MSG_WAITFORONE)
1427                         args->flags |= LINUX_MSG_DONTWAIT;
1428
1429                 /*
1430                  * See BUGS section of recvmmsg(2).
1431                  */
1432                 if (args->timeout) {
1433                         getnanotime(&ts);
1434                         timespecsub(&ts, &tts, &ts);
1435                         if (!timespecisset(&ts) || ts.tv_sec > 0)
1436                                 break;
1437                 }
1438                 /* Out of band data, return right away. */
1439                 if (bsd_msg.msg_flags & MSG_OOB)
1440                         break;
1441         }
1442         if (error == 0)
1443                 td->td_retval[0] = datagrams;
1444         return (error);
1445 }
1446
1447 int
1448 linux_shutdown(struct thread *td, struct linux_shutdown_args *args)
1449 {
1450
1451         return (kern_shutdown(td, args->s, args->how));
1452 }
1453
1454 int
1455 linux_setsockopt(struct thread *td, struct linux_setsockopt_args *args)
1456 {
1457         l_timeval linux_tv;
1458         struct sockaddr *sa;
1459         struct timeval tv;
1460         socklen_t len;
1461         int error, level, name;
1462
1463         level = linux_to_bsd_sockopt_level(args->level);
1464         switch (level) {
1465         case SOL_SOCKET:
1466                 name = linux_to_bsd_so_sockopt(args->optname);
1467                 switch (name) {
1468                 case LOCAL_CREDS_PERSISTENT:
1469                         level = SOL_LOCAL;
1470                         break;
1471                 case SO_RCVTIMEO:
1472                         /* FALLTHROUGH */
1473                 case SO_SNDTIMEO:
1474                         error = copyin(PTRIN(args->optval), &linux_tv,
1475                             sizeof(linux_tv));
1476                         if (error != 0)
1477                                 return (error);
1478                         tv.tv_sec = linux_tv.tv_sec;
1479                         tv.tv_usec = linux_tv.tv_usec;
1480                         return (kern_setsockopt(td, args->s, level,
1481                             name, &tv, UIO_SYSSPACE, sizeof(tv)));
1482                         /* NOTREACHED */
1483                 default:
1484                         break;
1485                 }
1486                 break;
1487         case IPPROTO_IP:
1488                 if (args->optname == LINUX_IP_RECVERR &&
1489                     linux_ignore_ip_recverr) {
1490                         /*
1491                          * XXX: This is a hack to unbreak DNS resolution
1492                          *      with glibc 2.30 and above.
1493                          */
1494                         return (0);
1495                 }
1496                 name = linux_to_bsd_ip_sockopt(args->optname);
1497                 break;
1498         case IPPROTO_IPV6:
1499                 name = linux_to_bsd_ip6_sockopt(args->optname);
1500                 break;
1501         case IPPROTO_TCP:
1502                 name = linux_to_bsd_tcp_sockopt(args->optname);
1503                 break;
1504         default:
1505                 name = -1;
1506                 break;
1507         }
1508         if (name == -1) {
1509                 linux_msg(curthread,
1510                     "unsupported setsockopt level %d optname %d",
1511                     args->level, args->optname);
1512                 return (ENOPROTOOPT);
1513         }
1514
1515         if (name == IPV6_NEXTHOP) {
1516                 len = args->optlen;
1517                 error = linux_to_bsd_sockaddr(PTRIN(args->optval), &sa, &len);
1518                 if (error != 0)
1519                         return (error);
1520
1521                 error = kern_setsockopt(td, args->s, level,
1522                     name, sa, UIO_SYSSPACE, len);
1523                 free(sa, M_SONAME);
1524         } else {
1525                 error = kern_setsockopt(td, args->s, level,
1526                     name, PTRIN(args->optval), UIO_USERSPACE, args->optlen);
1527         }
1528
1529         return (error);
1530 }
1531
1532 int
1533 linux_getsockopt(struct thread *td, struct linux_getsockopt_args *args)
1534 {
1535         l_timeval linux_tv;
1536         struct timeval tv;
1537         socklen_t tv_len, xulen, len;
1538         struct sockaddr *sa;
1539         struct xucred xu;
1540         struct l_ucred lxu;
1541         int error, level, name, newval;
1542
1543         level = linux_to_bsd_sockopt_level(args->level);
1544         switch (level) {
1545         case SOL_SOCKET:
1546                 name = linux_to_bsd_so_sockopt(args->optname);
1547                 switch (name) {
1548                 case LOCAL_CREDS_PERSISTENT:
1549                         level = SOL_LOCAL;
1550                         break;
1551                 case SO_RCVTIMEO:
1552                         /* FALLTHROUGH */
1553                 case SO_SNDTIMEO:
1554                         tv_len = sizeof(tv);
1555                         error = kern_getsockopt(td, args->s, level,
1556                             name, &tv, UIO_SYSSPACE, &tv_len);
1557                         if (error != 0)
1558                                 return (error);
1559                         linux_tv.tv_sec = tv.tv_sec;
1560                         linux_tv.tv_usec = tv.tv_usec;
1561                         return (copyout(&linux_tv, PTRIN(args->optval),
1562                             sizeof(linux_tv)));
1563                         /* NOTREACHED */
1564                 case LOCAL_PEERCRED:
1565                         if (args->optlen < sizeof(lxu))
1566                                 return (EINVAL);
1567                         /*
1568                          * LOCAL_PEERCRED is not served at the SOL_SOCKET level,
1569                          * but by the Unix socket's level 0.
1570                          */
1571                         level = 0;
1572                         xulen = sizeof(xu);
1573                         error = kern_getsockopt(td, args->s, level,
1574                             name, &xu, UIO_SYSSPACE, &xulen);
1575                         if (error != 0)
1576                                 return (error);
1577                         lxu.pid = xu.cr_pid;
1578                         lxu.uid = xu.cr_uid;
1579                         lxu.gid = xu.cr_gid;
1580                         return (copyout(&lxu, PTRIN(args->optval), sizeof(lxu)));
1581                         /* NOTREACHED */
1582                 case SO_ERROR:
1583                         len = sizeof(newval);
1584                         error = kern_getsockopt(td, args->s, level,
1585                             name, &newval, UIO_SYSSPACE, &len);
1586                         if (error != 0)
1587                                 return (error);
1588                         newval = -bsd_to_linux_errno(newval);
1589                         return (copyout(&newval, PTRIN(args->optval), len));
1590                         /* NOTREACHED */
1591                 default:
1592                         break;
1593                 }
1594                 break;
1595         case IPPROTO_IP:
1596                 name = linux_to_bsd_ip_sockopt(args->optname);
1597                 break;
1598         case IPPROTO_IPV6:
1599                 name = linux_to_bsd_ip6_sockopt(args->optname);
1600                 break;
1601         case IPPROTO_TCP:
1602                 name = linux_to_bsd_tcp_sockopt(args->optname);
1603                 break;
1604         default:
1605                 name = -1;
1606                 break;
1607         }
1608         if (name == -1) {
1609                 linux_msg(curthread,
1610                     "unsupported getsockopt level %d optname %d",
1611                     args->level, args->optname);
1612                 return (EINVAL);
1613         }
1614
1615         if (name == IPV6_NEXTHOP) {
1616                 error = copyin(PTRIN(args->optlen), &len, sizeof(len));
1617                 if (error != 0)
1618                         return (error);
1619                 sa = malloc(len, M_SONAME, M_WAITOK);
1620
1621                 error = kern_getsockopt(td, args->s, level,
1622                     name, sa, UIO_SYSSPACE, &len);
1623                 if (error != 0)
1624                         goto out;
1625
1626                 error = linux_copyout_sockaddr(sa, PTRIN(args->optval), len);
1627                 if (error == 0)
1628                         error = copyout(&len, PTRIN(args->optlen),
1629                             sizeof(len));
1630 out:
1631                 free(sa, M_SONAME);
1632         } else {
1633                 if (args->optval) {
1634                         error = copyin(PTRIN(args->optlen), &len, sizeof(len));
1635                         if (error != 0)
1636                                 return (error);
1637                 }
1638                 error = kern_getsockopt(td, args->s, level,
1639                     name, PTRIN(args->optval), UIO_USERSPACE, &len);
1640                 if (error == 0)
1641                         error = copyout(&len, PTRIN(args->optlen),
1642                             sizeof(len));
1643         }
1644
1645         return (error);
1646 }
1647
1648 static int
1649 linux_sendfile_common(struct thread *td, l_int out, l_int in,
1650     l_loff_t *offset, l_size_t count)
1651 {
1652         off_t bytes_read;
1653         int error;
1654         l_loff_t current_offset;
1655         struct file *fp;
1656
1657         AUDIT_ARG_FD(in);
1658         error = fget_read(td, in, &cap_pread_rights, &fp);
1659         if (error != 0)
1660                 return (error);
1661
1662         if (offset != NULL) {
1663                 current_offset = *offset;
1664         } else {
1665                 error = (fp->f_ops->fo_flags & DFLAG_SEEKABLE) != 0 ?
1666                     fo_seek(fp, 0, SEEK_CUR, td) : ESPIPE;
1667                 if (error != 0)
1668                         goto drop;
1669                 current_offset = td->td_uretoff.tdu_off;
1670         }
1671
1672         bytes_read = 0;
1673
1674         /* Linux cannot have 0 count. */
1675         if (count <= 0 || current_offset < 0) {
1676                 error = EINVAL;
1677                 goto drop;
1678         }
1679
1680         error = fo_sendfile(fp, out, NULL, NULL, current_offset, count,
1681             &bytes_read, 0, td);
1682         if (error != 0)
1683                 goto drop;
1684         current_offset += bytes_read;
1685
1686         if (offset != NULL) {
1687                 *offset = current_offset;
1688         } else {
1689                 error = fo_seek(fp, current_offset, SEEK_SET, td);
1690                 if (error != 0)
1691                         goto drop;
1692         }
1693
1694         td->td_retval[0] = (ssize_t)bytes_read;
1695 drop:
1696         fdrop(fp, td);
1697         return (error);
1698 }
1699
1700 int
1701 linux_sendfile(struct thread *td, struct linux_sendfile_args *arg)
1702 {
1703         /*
1704          * Differences between FreeBSD and Linux sendfile:
1705          * - Linux doesn't send anything when count is 0 (FreeBSD uses 0 to
1706          *   mean send the whole file.)  In linux_sendfile given fds are still
1707          *   checked for validity when the count is 0.
1708          * - Linux can send to any fd whereas FreeBSD only supports sockets.
1709          *   The same restriction follows for linux_sendfile.
1710          * - Linux doesn't have an equivalent for FreeBSD's flags and sf_hdtr.
1711          * - Linux takes an offset pointer and updates it to the read location.
1712          *   FreeBSD takes in an offset and a 'bytes read' parameter which is
1713          *   only filled if it isn't NULL.  We use this parameter to update the
1714          *   offset pointer if it exists.
1715          * - Linux sendfile returns bytes read on success while FreeBSD
1716          *   returns 0.  We use the 'bytes read' parameter to get this value.
1717          */
1718
1719         l_loff_t offset64;
1720         l_long offset;
1721         int ret;
1722         int error;
1723
1724         if (arg->offset != NULL) {
1725                 error = copyin(arg->offset, &offset, sizeof(offset));
1726                 if (error != 0)
1727                         return (error);
1728                 offset64 = (l_loff_t)offset;
1729         }
1730
1731         ret = linux_sendfile_common(td, arg->out, arg->in,
1732             arg->offset != NULL ? &offset64 : NULL, arg->count);
1733
1734         if (arg->offset != NULL) {
1735 #if defined(__i386__) || defined(__arm__) || \
1736     (defined(__amd64__) && defined(COMPAT_LINUX32))
1737                 if (offset64 > INT32_MAX)
1738                         return (EOVERFLOW);
1739 #endif
1740                 offset = (l_long)offset64;
1741                 error = copyout(&offset, arg->offset, sizeof(offset));
1742                 if (error != 0)
1743                         return (error);
1744         }
1745
1746         return (ret);
1747 }
1748
1749 #if defined(__i386__) || defined(__arm__) || \
1750     (defined(__amd64__) && defined(COMPAT_LINUX32))
1751
1752 int
1753 linux_sendfile64(struct thread *td, struct linux_sendfile64_args *arg)
1754 {
1755         l_loff_t offset;
1756         int ret;
1757         int error;
1758
1759         if (arg->offset != NULL) {
1760                 error = copyin(arg->offset, &offset, sizeof(offset));
1761                 if (error != 0)
1762                         return (error);
1763         }
1764
1765         ret = linux_sendfile_common(td, arg->out, arg->in,
1766                 arg->offset != NULL ? &offset : NULL, arg->count);
1767
1768         if (arg->offset != NULL) {
1769                 error = copyout(&offset, arg->offset, sizeof(offset));
1770                 if (error != 0)
1771                         return (error);
1772         }
1773
1774         return (ret);
1775 }
1776
1777 /* Argument list sizes for linux_socketcall */
1778 static const unsigned char lxs_args_cnt[] = {
1779         0 /* unused*/,          3 /* socket */,
1780         3 /* bind */,           3 /* connect */,
1781         2 /* listen */,         3 /* accept */,
1782         3 /* getsockname */,    3 /* getpeername */,
1783         4 /* socketpair */,     4 /* send */,
1784         4 /* recv */,           6 /* sendto */,
1785         6 /* recvfrom */,       2 /* shutdown */,
1786         5 /* setsockopt */,     5 /* getsockopt */,
1787         3 /* sendmsg */,        3 /* recvmsg */,
1788         4 /* accept4 */,        5 /* recvmmsg */,
1789         4 /* sendmmsg */,       4 /* sendfile */
1790 };
1791 #define LINUX_ARGS_CNT          (nitems(lxs_args_cnt) - 1)
1792 #define LINUX_ARG_SIZE(x)       (lxs_args_cnt[x] * sizeof(l_ulong))
1793
1794 int
1795 linux_socketcall(struct thread *td, struct linux_socketcall_args *args)
1796 {
1797         l_ulong a[6];
1798 #if defined(__amd64__) && defined(COMPAT_LINUX32)
1799         register_t l_args[6];
1800 #endif
1801         void *arg;
1802         int error;
1803
1804         if (args->what < LINUX_SOCKET || args->what > LINUX_ARGS_CNT)
1805                 return (EINVAL);
1806         error = copyin(PTRIN(args->args), a, LINUX_ARG_SIZE(args->what));
1807         if (error != 0)
1808                 return (error);
1809
1810 #if defined(__amd64__) && defined(COMPAT_LINUX32)
1811         for (int i = 0; i < lxs_args_cnt[args->what]; ++i)
1812                 l_args[i] = a[i];
1813         arg = l_args;
1814 #else
1815         arg = a;
1816 #endif
1817         switch (args->what) {
1818         case LINUX_SOCKET:
1819                 return (linux_socket(td, arg));
1820         case LINUX_BIND:
1821                 return (linux_bind(td, arg));
1822         case LINUX_CONNECT:
1823                 return (linux_connect(td, arg));
1824         case LINUX_LISTEN:
1825                 return (linux_listen(td, arg));
1826         case LINUX_ACCEPT:
1827                 return (linux_accept(td, arg));
1828         case LINUX_GETSOCKNAME:
1829                 return (linux_getsockname(td, arg));
1830         case LINUX_GETPEERNAME:
1831                 return (linux_getpeername(td, arg));
1832         case LINUX_SOCKETPAIR:
1833                 return (linux_socketpair(td, arg));
1834         case LINUX_SEND:
1835                 return (linux_send(td, arg));
1836         case LINUX_RECV:
1837                 return (linux_recv(td, arg));
1838         case LINUX_SENDTO:
1839                 return (linux_sendto(td, arg));
1840         case LINUX_RECVFROM:
1841                 return (linux_recvfrom(td, arg));
1842         case LINUX_SHUTDOWN:
1843                 return (linux_shutdown(td, arg));
1844         case LINUX_SETSOCKOPT:
1845                 return (linux_setsockopt(td, arg));
1846         case LINUX_GETSOCKOPT:
1847                 return (linux_getsockopt(td, arg));
1848         case LINUX_SENDMSG:
1849                 return (linux_sendmsg(td, arg));
1850         case LINUX_RECVMSG:
1851                 return (linux_recvmsg(td, arg));
1852         case LINUX_ACCEPT4:
1853                 return (linux_accept4(td, arg));
1854         case LINUX_RECVMMSG:
1855                 return (linux_recvmmsg(td, arg));
1856         case LINUX_SENDMMSG:
1857                 return (linux_sendmmsg(td, arg));
1858         case LINUX_SENDFILE:
1859                 return (linux_sendfile(td, arg));
1860         }
1861
1862         linux_msg(td, "socket type %d not implemented", args->what);
1863         return (ENOSYS);
1864 }
1865 #endif /* __i386__ || __arm__ || (__amd64__ && COMPAT_LINUX32) */