]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/compat/linux/linux_socket.c
linux(4): Refactor linux_common_recvmsg()
[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_emul.h>
80 #include <compat/linux/linux_file.h>
81 #include <compat/linux/linux_mib.h>
82 #include <compat/linux/linux_socket.h>
83 #include <compat/linux/linux_timer.h>
84 #include <compat/linux/linux_util.h>
85
86 #define SECURITY_CONTEXT_STRING "unconfined"
87
88 static int linux_sendmsg_common(struct thread *, l_int, struct l_msghdr *,
89                                         l_uint);
90 static int linux_recvmsg_common(struct thread *, l_int, struct l_msghdr *,
91                                         l_uint, struct msghdr *);
92 static int linux_set_socket_flags(int, int *);
93
94 static int
95 linux_to_bsd_sockopt_level(int level)
96 {
97
98         if (level == LINUX_SOL_SOCKET)
99                 return (SOL_SOCKET);
100         /* Remaining values are RFC-defined protocol numbers. */
101         return (level);
102 }
103
104 static int
105 bsd_to_linux_sockopt_level(int level)
106 {
107
108         if (level == SOL_SOCKET)
109                 return (LINUX_SOL_SOCKET);
110         return (level);
111 }
112
113 static int
114 linux_to_bsd_ip_sockopt(int opt)
115 {
116
117         switch (opt) {
118         /* known and translated sockopts */
119         case LINUX_IP_TOS:
120                 return (IP_TOS);
121         case LINUX_IP_TTL:
122                 return (IP_TTL);
123         case LINUX_IP_HDRINCL:
124                 return (IP_HDRINCL);
125         case LINUX_IP_OPTIONS:
126                 return (IP_OPTIONS);
127         case LINUX_IP_RECVOPTS:
128                 LINUX_RATELIMIT_MSG_NOTTESTED("IPv4 socket option IP_RECVOPTS");
129                 return (IP_RECVOPTS);
130         case LINUX_IP_RETOPTS:
131                 LINUX_RATELIMIT_MSG_NOTTESTED("IPv4 socket option IP_REETOPTS");
132                 return (IP_RETOPTS);
133         case LINUX_IP_RECVTTL:
134                 LINUX_RATELIMIT_MSG_NOTTESTED("IPv4 socket option IP_RECVTTL");
135                 return (IP_RECVTTL);
136         case LINUX_IP_RECVTOS:
137                 LINUX_RATELIMIT_MSG_NOTTESTED("IPv4 socket option IP_RECVTOS");
138                 return (IP_RECVTOS);
139         case LINUX_IP_FREEBIND:
140                 LINUX_RATELIMIT_MSG_NOTTESTED("IPv4 socket option IP_FREEBIND");
141                 return (IP_BINDANY);
142         case LINUX_IP_IPSEC_POLICY:
143                 /* we have this option, but not documented in ip(4) manpage */
144                 LINUX_RATELIMIT_MSG_NOTTESTED("IPv4 socket option IP_IPSEC_POLICY");
145                 return (IP_IPSEC_POLICY);
146         case LINUX_IP_MINTTL:
147                 LINUX_RATELIMIT_MSG_NOTTESTED("IPv4 socket option IP_MINTTL");
148                 return (IP_MINTTL);
149         case LINUX_IP_MULTICAST_IF:
150                 return (IP_MULTICAST_IF);
151         case LINUX_IP_MULTICAST_TTL:
152                 return (IP_MULTICAST_TTL);
153         case LINUX_IP_MULTICAST_LOOP:
154                 return (IP_MULTICAST_LOOP);
155         case LINUX_IP_ADD_MEMBERSHIP:
156                 return (IP_ADD_MEMBERSHIP);
157         case LINUX_IP_DROP_MEMBERSHIP:
158                 return (IP_DROP_MEMBERSHIP);
159         case LINUX_IP_UNBLOCK_SOURCE:
160                 LINUX_RATELIMIT_MSG_NOTTESTED("IPv4 socket option IP_UNBLOCK_SOURCE");
161                 return (IP_UNBLOCK_SOURCE);
162         case LINUX_IP_BLOCK_SOURCE:
163                 LINUX_RATELIMIT_MSG_NOTTESTED("IPv4 socket option IP_BLOCK_SOURCE");
164                 return (IP_BLOCK_SOURCE);
165         case LINUX_IP_ADD_SOURCE_MEMBERSHIP:
166                 LINUX_RATELIMIT_MSG_NOTTESTED("IPv4 socket option IP_ADD_SOURCE_MEMBERSHIP");
167                 return (IP_ADD_SOURCE_MEMBERSHIP);
168         case LINUX_IP_DROP_SOURCE_MEMBERSHIP:
169                 LINUX_RATELIMIT_MSG_NOTTESTED("IPv4 socket option IP_DROP_SOURCE_MEMBERSHIP");
170                 return (IP_DROP_SOURCE_MEMBERSHIP);
171         case LINUX_MCAST_JOIN_GROUP:
172                 LINUX_RATELIMIT_MSG_NOTTESTED("IPv4 socket option IP_MCAST_JOIN_GROUP");
173                 return (MCAST_JOIN_GROUP);
174         case LINUX_MCAST_LEAVE_GROUP:
175                 LINUX_RATELIMIT_MSG_NOTTESTED("IPv4 socket option IP_MCAST_LEAVE_GROUP");
176                 return (MCAST_LEAVE_GROUP);
177         case LINUX_MCAST_JOIN_SOURCE_GROUP:
178                 LINUX_RATELIMIT_MSG_NOTTESTED("IPv4 socket option IP_MCAST_JOIN_SOURCE_GROUP");
179                 return (MCAST_JOIN_SOURCE_GROUP);
180         case LINUX_MCAST_LEAVE_SOURCE_GROUP:
181                 LINUX_RATELIMIT_MSG_NOTTESTED("IPv4 socket option IP_MCAST_LEAVE_SOURCE_GROUP");
182                 return (MCAST_LEAVE_SOURCE_GROUP);
183
184         /* known but not implemented sockopts */
185         case LINUX_IP_ROUTER_ALERT:
186                 LINUX_RATELIMIT_MSG_OPT1(
187                     "unsupported IPv4 socket option IP_ROUTER_ALERT (%d), you can not do user-space routing from linux programs",
188                     opt);
189                 return (-2);
190         case LINUX_IP_PKTINFO:
191                 LINUX_RATELIMIT_MSG_OPT1(
192                     "unsupported IPv4 socket option IP_PKTINFO (%d), you can not get extended packet info for datagram sockets in linux programs",
193                     opt);
194                 return (-2);
195         case LINUX_IP_PKTOPTIONS:
196                 LINUX_RATELIMIT_MSG_OPT1(
197                     "unsupported IPv4 socket option IP_PKTOPTIONS (%d)",
198                     opt);
199                 return (-2);
200         case LINUX_IP_MTU_DISCOVER:
201                 LINUX_RATELIMIT_MSG_OPT1(
202                     "unsupported IPv4 socket option IP_MTU_DISCOVER (%d), your linux program can not control path-MTU discovery",
203                     opt);
204                 return (-2);
205         case LINUX_IP_RECVERR:
206                 /* needed by steam */
207                 LINUX_RATELIMIT_MSG_OPT1(
208                     "unsupported IPv4 socket option IP_RECVERR (%d), you can not get extended reliability info in linux programs",
209                     opt);
210                 return (-2);
211         case LINUX_IP_MTU:
212                 LINUX_RATELIMIT_MSG_OPT1(
213                     "unsupported IPv4 socket option IP_MTU (%d), your linux program can not control the MTU on this socket",
214                     opt);
215                 return (-2);
216         case LINUX_IP_XFRM_POLICY:
217                 LINUX_RATELIMIT_MSG_OPT1(
218                     "unsupported IPv4 socket option IP_XFRM_POLICY (%d)",
219                     opt);
220                 return (-2);
221         case LINUX_IP_PASSSEC:
222                 /* needed by steam */
223                 LINUX_RATELIMIT_MSG_OPT1(
224                     "unsupported IPv4 socket option IP_PASSSEC (%d), you can not get IPSEC related credential information associated with this socket in linux programs -- if you do not use IPSEC, you can ignore this",
225                     opt);
226                 return (-2);
227         case LINUX_IP_TRANSPARENT:
228                 /* IP_BINDANY or more? */
229                 LINUX_RATELIMIT_MSG_OPT1(
230                     "unsupported IPv4 socket option IP_TRANSPARENT (%d), you can not enable transparent proxying in linux programs -- note, IP_FREEBIND is supported, no idea if the FreeBSD IP_BINDANY is equivalent to the Linux IP_TRANSPARENT or not, any info is welcome",
231                     opt);
232                 return (-2);
233         case LINUX_IP_NODEFRAG:
234                 LINUX_RATELIMIT_MSG_OPT1(
235                     "unsupported IPv4 socket option IP_NODEFRAG (%d)",
236                     opt);
237                 return (-2);
238         case LINUX_IP_CHECKSUM:
239                 LINUX_RATELIMIT_MSG_OPT1(
240                     "unsupported IPv4 socket option IP_CHECKSUM (%d)",
241                     opt);
242                 return (-2);
243         case LINUX_IP_BIND_ADDRESS_NO_PORT:
244                 LINUX_RATELIMIT_MSG_OPT1(
245                     "unsupported IPv4 socket option IP_BIND_ADDRESS_NO_PORT (%d)",
246                     opt);
247                 return (-2);
248         case LINUX_IP_RECVFRAGSIZE:
249                 LINUX_RATELIMIT_MSG_OPT1(
250                     "unsupported IPv4 socket option IP_RECVFRAGSIZE (%d)",
251                     opt);
252                 return (-2);
253         case LINUX_MCAST_MSFILTER:
254                 LINUX_RATELIMIT_MSG_OPT1(
255                     "unsupported IPv4 socket option IP_MCAST_MSFILTER (%d)",
256                     opt);
257                 return (-2);
258         case LINUX_IP_MULTICAST_ALL:
259                 LINUX_RATELIMIT_MSG_OPT1(
260                     "unsupported IPv4 socket option IP_MULTICAST_ALL (%d), your linux program will not see all multicast groups joined by the entire system, only those the program joined itself on this socket",
261                     opt);
262                 return (-2);
263         case LINUX_IP_UNICAST_IF:
264                 LINUX_RATELIMIT_MSG_OPT1(
265                     "unsupported IPv4 socket option IP_UNICAST_IF (%d)",
266                     opt);
267                 return (-2);
268
269         /* unknown sockopts */
270         default:
271                 return (-1);
272         }
273 }
274
275 static int
276 linux_to_bsd_ip6_sockopt(int opt)
277 {
278
279         switch (opt) {
280         /* known and translated sockopts */
281         case LINUX_IPV6_2292PKTINFO:
282                 LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_2292PKTINFO");
283                 return (IPV6_2292PKTINFO);
284         case LINUX_IPV6_2292HOPOPTS:
285                 LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_2292HOPOPTS");
286                 return (IPV6_2292HOPOPTS);
287         case LINUX_IPV6_2292DSTOPTS:
288                 LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_2292DSTOPTS");
289                 return (IPV6_2292DSTOPTS);
290         case LINUX_IPV6_2292RTHDR:
291                 LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_2292RTHDR");
292                 return (IPV6_2292RTHDR);
293         case LINUX_IPV6_2292PKTOPTIONS:
294                 LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_2292PKTOPTIONS");
295                 return (IPV6_2292PKTOPTIONS);
296         case LINUX_IPV6_CHECKSUM:
297                 LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_CHECKSUM");
298                 return (IPV6_CHECKSUM);
299         case LINUX_IPV6_2292HOPLIMIT:
300                 LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_2292HOPLIMIT");
301                 return (IPV6_2292HOPLIMIT);
302         case LINUX_IPV6_NEXTHOP:
303                 return (IPV6_NEXTHOP);
304         case LINUX_IPV6_UNICAST_HOPS:
305                 return (IPV6_UNICAST_HOPS);
306         case LINUX_IPV6_MULTICAST_IF:
307                 return (IPV6_MULTICAST_IF);
308         case LINUX_IPV6_MULTICAST_HOPS:
309                 return (IPV6_MULTICAST_HOPS);
310         case LINUX_IPV6_MULTICAST_LOOP:
311                 return (IPV6_MULTICAST_LOOP);
312         case LINUX_IPV6_ADD_MEMBERSHIP:
313                 return (IPV6_JOIN_GROUP);
314         case LINUX_IPV6_DROP_MEMBERSHIP:
315                 return (IPV6_LEAVE_GROUP);
316         case LINUX_IPV6_V6ONLY:
317                 return (IPV6_V6ONLY);
318         case LINUX_IPV6_IPSEC_POLICY:
319                 /* we have this option, but not documented in ip6(4) manpage */
320                 LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_IPSEC_POLICY");
321                 return (IPV6_IPSEC_POLICY);
322         case LINUX_MCAST_JOIN_GROUP:
323                 LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_JOIN_GROUP");
324                 return (IPV6_JOIN_GROUP);
325         case LINUX_MCAST_LEAVE_GROUP:
326                 LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_LEAVE_GROUP");
327                 return (IPV6_LEAVE_GROUP);
328         case LINUX_IPV6_RECVPKTINFO:
329                 LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_RECVPKTINFO");
330                 return (IPV6_RECVPKTINFO);
331         case LINUX_IPV6_PKTINFO:
332                 LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_PKTINFO");
333                 return (IPV6_PKTINFO);
334         case LINUX_IPV6_RECVHOPLIMIT:
335                 LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_RECVHOPLIMIT");
336                 return (IPV6_RECVHOPLIMIT);
337         case LINUX_IPV6_HOPLIMIT:
338                 LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_HOPLIMIT");
339                 return (IPV6_HOPLIMIT);
340         case LINUX_IPV6_RECVHOPOPTS:
341                 LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_RECVHOPOPTS");
342                 return (IPV6_RECVHOPOPTS);
343         case LINUX_IPV6_HOPOPTS:
344                 LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_HOPOPTS");
345                 return (IPV6_HOPOPTS);
346         case LINUX_IPV6_RTHDRDSTOPTS:
347                 LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_RTHDRDSTOPTS");
348                 return (IPV6_RTHDRDSTOPTS);
349         case LINUX_IPV6_RECVRTHDR:
350                 LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_RECVRTHDR");
351                 return (IPV6_RECVRTHDR);
352         case LINUX_IPV6_RTHDR:
353                 LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_RTHDR");
354                 return (IPV6_RTHDR);
355         case LINUX_IPV6_RECVDSTOPTS:
356                 LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_RECVDSTOPTS");
357                 return (IPV6_RECVDSTOPTS);
358         case LINUX_IPV6_DSTOPTS:
359                 LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_DSTOPTS");
360                 return (IPV6_DSTOPTS);
361         case LINUX_IPV6_RECVPATHMTU:
362                 LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_RECVPATHMTU");
363                 return (IPV6_RECVPATHMTU);
364         case LINUX_IPV6_PATHMTU:
365                 LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_PATHMTU");
366                 return (IPV6_PATHMTU);
367         case LINUX_IPV6_DONTFRAG:
368                 return (IPV6_DONTFRAG);
369         case LINUX_IPV6_AUTOFLOWLABEL:
370                 LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_AUTOFLOWLABEL");
371                 return (IPV6_AUTOFLOWLABEL);
372         case LINUX_IPV6_ORIGDSTADDR:
373                 LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_ORIGDSTADDR");
374                 return (IPV6_ORIGDSTADDR);
375         case LINUX_IPV6_FREEBIND:
376                 LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_FREEBIND");
377                 return (IPV6_BINDANY);
378
379         /* known but not implemented sockopts */
380         case LINUX_IPV6_ADDRFORM:
381                 LINUX_RATELIMIT_MSG_OPT1(
382                     "unsupported IPv6 socket option IPV6_ADDRFORM (%d), you linux program can not convert the socket to IPv4",
383                     opt);
384                 return (-2);
385         case LINUX_IPV6_AUTHHDR:
386                 LINUX_RATELIMIT_MSG_OPT1(
387                     "unsupported IPv6 socket option IPV6_AUTHHDR (%d), your linux program can not get the authentication header info of IPv6 packets",
388                     opt);
389                 return (-2);
390         case LINUX_IPV6_FLOWINFO:
391                 LINUX_RATELIMIT_MSG_OPT1(
392                     "unsupported IPv6 socket option IPV6_FLOWINFO (%d), your linux program can not get the flowid of IPv6 packets",
393                     opt);
394                 return (-2);
395         case LINUX_IPV6_ROUTER_ALERT:
396                 LINUX_RATELIMIT_MSG_OPT1(
397                     "unsupported IPv6 socket option IPV6_ROUTER_ALERT (%d), you can not do user-space routing from linux programs",
398                     opt);
399                 return (-2);
400         case LINUX_IPV6_MTU_DISCOVER:
401                 LINUX_RATELIMIT_MSG_OPT1(
402                     "unsupported IPv6 socket option IPV6_MTU_DISCOVER (%d), your linux program can not control path-MTU discovery",
403                     opt);
404                 return (-2);
405         case LINUX_IPV6_MTU:
406                 LINUX_RATELIMIT_MSG_OPT1(
407                     "unsupported IPv6 socket option IPV6_MTU (%d), your linux program can not control the MTU on this socket",
408                     opt);
409                 return (-2);
410         case LINUX_IPV6_JOIN_ANYCAST:
411                 LINUX_RATELIMIT_MSG_OPT1(
412                     "unsupported IPv6 socket option IPV6_JOIN_ANYCAST (%d)",
413                     opt);
414                 return (-2);
415         case LINUX_IPV6_LEAVE_ANYCAST:
416                 LINUX_RATELIMIT_MSG_OPT1(
417                     "unsupported IPv6 socket option IPV6_LEAVE_ANYCAST (%d)",
418                     opt);
419                 return (-2);
420         case LINUX_IPV6_MULTICAST_ALL:
421                 LINUX_RATELIMIT_MSG_OPT1(
422                     "unsupported IPv6 socket option IPV6_MULTICAST_ALL (%d)",
423                     opt);
424                 return (-2);
425         case LINUX_IPV6_ROUTER_ALERT_ISOLATE:
426                 LINUX_RATELIMIT_MSG_OPT1(
427                     "unsupported IPv6 socket option IPV6_ROUTER_ALERT_ISOLATE (%d)",
428                     opt);
429                 return (-2);
430         case LINUX_IPV6_FLOWLABEL_MGR:
431                 LINUX_RATELIMIT_MSG_OPT1(
432                     "unsupported IPv6 socket option IPV6_FLOWLABEL_MGR (%d)",
433                     opt);
434                 return (-2);
435         case LINUX_IPV6_FLOWINFO_SEND:
436                 LINUX_RATELIMIT_MSG_OPT1(
437                     "unsupported IPv6 socket option IPV6_FLOWINFO_SEND (%d)",
438                     opt);
439                 return (-2);
440         case LINUX_IPV6_XFRM_POLICY:
441                 LINUX_RATELIMIT_MSG_OPT1(
442                     "unsupported IPv6 socket option IPV6_XFRM_POLICY (%d)",
443                     opt);
444                 return (-2);
445         case LINUX_IPV6_HDRINCL:
446                 LINUX_RATELIMIT_MSG_OPT1(
447                     "unsupported IPv6 socket option IPV6_HDRINCL (%d)",
448                     opt);
449                 return (-2);
450         case LINUX_MCAST_BLOCK_SOURCE:
451                 LINUX_RATELIMIT_MSG_OPT1(
452                     "unsupported IPv6 socket option MCAST_BLOCK_SOURCE (%d), your linux program may see more multicast stuff than it wants",
453                     opt);
454                 return (-2);
455         case LINUX_MCAST_UNBLOCK_SOURCE:
456                 LINUX_RATELIMIT_MSG_OPT1(
457                     "unsupported IPv6 socket option MCAST_UNBLOCK_SOURCE (%d), your linux program may not see all the multicast stuff it wants",
458                     opt);
459                 return (-2);
460         case LINUX_MCAST_JOIN_SOURCE_GROUP:
461                 LINUX_RATELIMIT_MSG_OPT1(
462                     "unsupported IPv6 socket option MCAST_JOIN_SOURCE_GROUP (%d), your linux program is not able to join a multicast source group",
463                     opt);
464                 return (-2);
465         case LINUX_MCAST_LEAVE_SOURCE_GROUP:
466                 LINUX_RATELIMIT_MSG_OPT1(
467                     "unsupported IPv6 socket option MCAST_LEAVE_SOURCE_GROUP (%d), your linux program is not able to leave a multicast source group -- but it was also not able to join one, so no issue",
468                     opt);
469                 return (-2);
470         case LINUX_MCAST_MSFILTER:
471                 LINUX_RATELIMIT_MSG_OPT1(
472                     "unsupported IPv6 socket option MCAST_MSFILTER (%d), your linux program can not manipulate the multicast filter, it may see more multicast data than it wants to see",
473                     opt);
474                 return (-2);
475         case LINUX_IPV6_ADDR_PREFERENCES:
476                 LINUX_RATELIMIT_MSG_OPT1(
477                     "unsupported IPv6 socket option IPV6_ADDR_PREFERENCES (%d)",
478                     opt);
479                 return (-2);
480         case LINUX_IPV6_MINHOPCOUNT:
481                 LINUX_RATELIMIT_MSG_OPT1(
482                     "unsupported IPv6 socket option IPV6_MINHOPCOUNT (%d)",
483                     opt);
484                 return (-2);
485         case LINUX_IPV6_TRANSPARENT:
486                 /* IP_BINDANY or more? */
487                 LINUX_RATELIMIT_MSG_OPT1(
488                     "unsupported IPv6 socket option IPV6_TRANSPARENT (%d), you can not enable transparent proxying in linux programs -- note, IP_FREEBIND is supported, no idea if the FreeBSD IP_BINDANY is equivalent to the Linux IP_TRANSPARENT or not, any info is welcome",
489                     opt);
490                 return (-2);
491         case LINUX_IPV6_UNICAST_IF:
492                 LINUX_RATELIMIT_MSG_OPT1(
493                     "unsupported IPv6 socket option IPV6_UNICAST_IF (%d)",
494                     opt);
495                 return (-2);
496         case LINUX_IPV6_RECVFRAGSIZE:
497                 LINUX_RATELIMIT_MSG_OPT1(
498                     "unsupported IPv6 socket option IPV6_RECVFRAGSIZE (%d)",
499                     opt);
500                 return (-2);
501
502         /* unknown sockopts */
503         default:
504                 return (-1);
505         }
506 }
507
508 static int
509 linux_to_bsd_so_sockopt(int opt)
510 {
511
512         switch (opt) {
513         case LINUX_SO_DEBUG:
514                 return (SO_DEBUG);
515         case LINUX_SO_REUSEADDR:
516                 return (SO_REUSEADDR);
517         case LINUX_SO_TYPE:
518                 return (SO_TYPE);
519         case LINUX_SO_ERROR:
520                 return (SO_ERROR);
521         case LINUX_SO_DONTROUTE:
522                 return (SO_DONTROUTE);
523         case LINUX_SO_BROADCAST:
524                 return (SO_BROADCAST);
525         case LINUX_SO_SNDBUF:
526         case LINUX_SO_SNDBUFFORCE:
527                 return (SO_SNDBUF);
528         case LINUX_SO_RCVBUF:
529         case LINUX_SO_RCVBUFFORCE:
530                 return (SO_RCVBUF);
531         case LINUX_SO_KEEPALIVE:
532                 return (SO_KEEPALIVE);
533         case LINUX_SO_OOBINLINE:
534                 return (SO_OOBINLINE);
535         case LINUX_SO_LINGER:
536                 return (SO_LINGER);
537         case LINUX_SO_REUSEPORT:
538                 return (SO_REUSEPORT_LB);
539         case LINUX_SO_PASSCRED:
540                 return (LOCAL_CREDS_PERSISTENT);
541         case LINUX_SO_PEERCRED:
542                 return (LOCAL_PEERCRED);
543         case LINUX_SO_RCVLOWAT:
544                 return (SO_RCVLOWAT);
545         case LINUX_SO_SNDLOWAT:
546                 return (SO_SNDLOWAT);
547         case LINUX_SO_RCVTIMEO:
548                 return (SO_RCVTIMEO);
549         case LINUX_SO_SNDTIMEO:
550                 return (SO_SNDTIMEO);
551         case LINUX_SO_TIMESTAMPO:
552         case LINUX_SO_TIMESTAMPN:
553                 return (SO_TIMESTAMP);
554         case LINUX_SO_TIMESTAMPNSO:
555         case LINUX_SO_TIMESTAMPNSN:
556                 return (SO_BINTIME);
557         case LINUX_SO_ACCEPTCONN:
558                 return (SO_ACCEPTCONN);
559         case LINUX_SO_PROTOCOL:
560                 return (SO_PROTOCOL);
561         case LINUX_SO_DOMAIN:
562                 return (SO_DOMAIN);
563         }
564         return (-1);
565 }
566
567 static int
568 linux_to_bsd_tcp_sockopt(int opt)
569 {
570
571         switch (opt) {
572         case LINUX_TCP_NODELAY:
573                 return (TCP_NODELAY);
574         case LINUX_TCP_MAXSEG:
575                 return (TCP_MAXSEG);
576         case LINUX_TCP_CORK:
577                 return (TCP_NOPUSH);
578         case LINUX_TCP_KEEPIDLE:
579                 return (TCP_KEEPIDLE);
580         case LINUX_TCP_KEEPINTVL:
581                 return (TCP_KEEPINTVL);
582         case LINUX_TCP_KEEPCNT:
583                 return (TCP_KEEPCNT);
584         case LINUX_TCP_INFO:
585                 LINUX_RATELIMIT_MSG_OPT1(
586                     "unsupported TCP socket option TCP_INFO (%d)", opt);
587                 return (-2);
588         case LINUX_TCP_MD5SIG:
589                 return (TCP_MD5SIG);
590         }
591         return (-1);
592 }
593
594 static int
595 linux_to_bsd_msg_flags(int flags)
596 {
597         int ret_flags = 0;
598
599         if (flags & LINUX_MSG_OOB)
600                 ret_flags |= MSG_OOB;
601         if (flags & LINUX_MSG_PEEK)
602                 ret_flags |= MSG_PEEK;
603         if (flags & LINUX_MSG_DONTROUTE)
604                 ret_flags |= MSG_DONTROUTE;
605         if (flags & LINUX_MSG_CTRUNC)
606                 ret_flags |= MSG_CTRUNC;
607         if (flags & LINUX_MSG_TRUNC)
608                 ret_flags |= MSG_TRUNC;
609         if (flags & LINUX_MSG_DONTWAIT)
610                 ret_flags |= MSG_DONTWAIT;
611         if (flags & LINUX_MSG_EOR)
612                 ret_flags |= MSG_EOR;
613         if (flags & LINUX_MSG_WAITALL)
614                 ret_flags |= MSG_WAITALL;
615         if (flags & LINUX_MSG_NOSIGNAL)
616                 ret_flags |= MSG_NOSIGNAL;
617         if (flags & LINUX_MSG_PROXY)
618                 LINUX_RATELIMIT_MSG_OPT1("socket message flag MSG_PROXY (%d) not handled",
619                     LINUX_MSG_PROXY);
620         if (flags & LINUX_MSG_FIN)
621                 LINUX_RATELIMIT_MSG_OPT1("socket message flag MSG_FIN (%d) not handled",
622                     LINUX_MSG_FIN);
623         if (flags & LINUX_MSG_SYN)
624                 LINUX_RATELIMIT_MSG_OPT1("socket message flag MSG_SYN (%d) not handled",
625                     LINUX_MSG_SYN);
626         if (flags & LINUX_MSG_CONFIRM)
627                 LINUX_RATELIMIT_MSG_OPT1("socket message flag MSG_CONFIRM (%d) not handled",
628                     LINUX_MSG_CONFIRM);
629         if (flags & LINUX_MSG_RST)
630                 LINUX_RATELIMIT_MSG_OPT1("socket message flag MSG_RST (%d) not handled",
631                     LINUX_MSG_RST);
632         if (flags & LINUX_MSG_ERRQUEUE)
633                 LINUX_RATELIMIT_MSG_OPT1("socket message flag MSG_ERRQUEUE (%d) not handled",
634                     LINUX_MSG_ERRQUEUE);
635         return (ret_flags);
636 }
637
638 static int
639 linux_to_bsd_cmsg_type(int cmsg_type)
640 {
641
642         switch (cmsg_type) {
643         case LINUX_SCM_RIGHTS:
644                 return (SCM_RIGHTS);
645         case LINUX_SCM_CREDENTIALS:
646                 return (SCM_CREDS);
647         }
648         return (-1);
649 }
650
651 static int
652 bsd_to_linux_cmsg_type(struct proc *p, int cmsg_type)
653 {
654         struct linux_pemuldata *pem;
655
656         pem = pem_find(p);
657
658         switch (cmsg_type) {
659         case SCM_RIGHTS:
660                 return (LINUX_SCM_RIGHTS);
661         case SCM_CREDS:
662                 return (LINUX_SCM_CREDENTIALS);
663         case SCM_CREDS2:
664                 return (LINUX_SCM_CREDENTIALS);
665         case SCM_TIMESTAMP:
666                 return (pem->so_timestamp);
667         case SCM_BINTIME:
668                 return (pem->so_timestampns);
669         }
670         return (-1);
671 }
672
673 static int
674 linux_to_bsd_msghdr(struct msghdr *bhdr, const struct l_msghdr *lhdr)
675 {
676         if (lhdr->msg_controllen > INT_MAX)
677                 return (ENOBUFS);
678
679         bhdr->msg_name          = PTRIN(lhdr->msg_name);
680         bhdr->msg_namelen       = lhdr->msg_namelen;
681         bhdr->msg_iov           = PTRIN(lhdr->msg_iov);
682         bhdr->msg_iovlen        = lhdr->msg_iovlen;
683         bhdr->msg_control       = PTRIN(lhdr->msg_control);
684
685         /*
686          * msg_controllen is skipped since BSD and LINUX control messages
687          * are potentially different sizes (e.g. the cred structure used
688          * by SCM_CREDS is different between the two operating system).
689          *
690          * The caller can set it (if necessary) after converting all the
691          * control messages.
692          */
693
694         bhdr->msg_flags         = linux_to_bsd_msg_flags(lhdr->msg_flags);
695         return (0);
696 }
697
698 static int
699 bsd_to_linux_msghdr(const struct msghdr *bhdr, struct l_msghdr *lhdr)
700 {
701         lhdr->msg_name          = PTROUT(bhdr->msg_name);
702         lhdr->msg_namelen       = bhdr->msg_namelen;
703         lhdr->msg_iov           = PTROUT(bhdr->msg_iov);
704         lhdr->msg_iovlen        = bhdr->msg_iovlen;
705         lhdr->msg_control       = PTROUT(bhdr->msg_control);
706
707         /*
708          * msg_controllen is skipped since BSD and LINUX control messages
709          * are potentially different sizes (e.g. the cred structure used
710          * by SCM_CREDS is different between the two operating system).
711          *
712          * The caller can set it (if necessary) after converting all the
713          * control messages.
714          */
715
716         /* msg_flags skipped */
717         return (0);
718 }
719
720 static int
721 linux_set_socket_flags(int lflags, int *flags)
722 {
723
724         if (lflags & ~(LINUX_SOCK_CLOEXEC | LINUX_SOCK_NONBLOCK))
725                 return (EINVAL);
726         if (lflags & LINUX_SOCK_NONBLOCK)
727                 *flags |= SOCK_NONBLOCK;
728         if (lflags & LINUX_SOCK_CLOEXEC)
729                 *flags |= SOCK_CLOEXEC;
730         return (0);
731 }
732
733 static int
734 linux_copyout_sockaddr(const struct sockaddr *sa, void *uaddr, size_t len)
735 {
736         struct l_sockaddr *lsa;
737         int error;
738
739         error = bsd_to_linux_sockaddr(sa, &lsa, len);
740         if (error != 0)
741                 return (error);
742
743         error = copyout(lsa, uaddr, len);
744         free(lsa, M_LINUX);
745
746         return (error);
747 }
748
749 static int
750 linux_sendit(struct thread *td, int s, struct msghdr *mp, int flags,
751     struct mbuf *control, enum uio_seg segflg)
752 {
753         struct sockaddr *to;
754         int error, len;
755
756         if (mp->msg_name != NULL) {
757                 len = mp->msg_namelen;
758                 error = linux_to_bsd_sockaddr(mp->msg_name, &to, &len);
759                 if (error != 0)
760                         return (error);
761                 mp->msg_name = to;
762         } else
763                 to = NULL;
764
765         error = kern_sendit(td, s, mp, linux_to_bsd_msg_flags(flags), control,
766             segflg);
767
768         if (to)
769                 free(to, M_SONAME);
770         return (error);
771 }
772
773 /* Return 0 if IP_HDRINCL is set for the given socket. */
774 static int
775 linux_check_hdrincl(struct thread *td, int s)
776 {
777         int error, optval;
778         socklen_t size_val;
779
780         size_val = sizeof(optval);
781         error = kern_getsockopt(td, s, IPPROTO_IP, IP_HDRINCL,
782             &optval, UIO_SYSSPACE, &size_val);
783         if (error != 0)
784                 return (error);
785
786         return (optval == 0);
787 }
788
789 /*
790  * Updated sendto() when IP_HDRINCL is set:
791  * tweak endian-dependent fields in the IP packet.
792  */
793 static int
794 linux_sendto_hdrincl(struct thread *td, struct linux_sendto_args *linux_args)
795 {
796 /*
797  * linux_ip_copysize defines how many bytes we should copy
798  * from the beginning of the IP packet before we customize it for BSD.
799  * It should include all the fields we modify (ip_len and ip_off).
800  */
801 #define linux_ip_copysize       8
802
803         struct ip *packet;
804         struct msghdr msg;
805         struct iovec aiov[1];
806         int error;
807
808         /* Check that the packet isn't too big or too small. */
809         if (linux_args->len < linux_ip_copysize ||
810             linux_args->len > IP_MAXPACKET)
811                 return (EINVAL);
812
813         packet = (struct ip *)malloc(linux_args->len, M_LINUX, M_WAITOK);
814
815         /* Make kernel copy of the packet to be sent */
816         if ((error = copyin(PTRIN(linux_args->msg), packet,
817             linux_args->len)))
818                 goto goout;
819
820         /* Convert fields from Linux to BSD raw IP socket format */
821         packet->ip_len = linux_args->len;
822         packet->ip_off = ntohs(packet->ip_off);
823
824         /* Prepare the msghdr and iovec structures describing the new packet */
825         msg.msg_name = PTRIN(linux_args->to);
826         msg.msg_namelen = linux_args->tolen;
827         msg.msg_iov = aiov;
828         msg.msg_iovlen = 1;
829         msg.msg_control = NULL;
830         msg.msg_flags = 0;
831         aiov[0].iov_base = (char *)packet;
832         aiov[0].iov_len = linux_args->len;
833         error = linux_sendit(td, linux_args->s, &msg, linux_args->flags,
834             NULL, UIO_SYSSPACE);
835 goout:
836         free(packet, M_LINUX);
837         return (error);
838 }
839
840 static const char *linux_netlink_names[] = {
841         [LINUX_NETLINK_ROUTE] = "ROUTE",
842         [LINUX_NETLINK_SOCK_DIAG] = "SOCK_DIAG",
843         [LINUX_NETLINK_NFLOG] = "NFLOG",
844         [LINUX_NETLINK_SELINUX] = "SELINUX",
845         [LINUX_NETLINK_AUDIT] = "AUDIT",
846         [LINUX_NETLINK_FIB_LOOKUP] = "FIB_LOOKUP",
847         [LINUX_NETLINK_NETFILTER] = "NETFILTER",
848         [LINUX_NETLINK_KOBJECT_UEVENT] = "KOBJECT_UEVENT",
849 };
850
851 int
852 linux_socket(struct thread *td, struct linux_socket_args *args)
853 {
854         int domain, retval_socket, type;
855
856         type = args->type & LINUX_SOCK_TYPE_MASK;
857         if (type < 0 || type > LINUX_SOCK_MAX)
858                 return (EINVAL);
859         retval_socket = linux_set_socket_flags(args->type & ~LINUX_SOCK_TYPE_MASK,
860                 &type);
861         if (retval_socket != 0)
862                 return (retval_socket);
863         domain = linux_to_bsd_domain(args->domain);
864         if (domain == -1) {
865                 /* Mask off SOCK_NONBLOCK / CLOEXEC for error messages. */
866                 type = args->type & LINUX_SOCK_TYPE_MASK;
867                 if (args->domain == LINUX_AF_NETLINK &&
868                     args->protocol == LINUX_NETLINK_AUDIT) {
869                         ; /* Do nothing, quietly. */
870                 } else if (args->domain == LINUX_AF_NETLINK) {
871                         const char *nl_name;
872
873                         if (args->protocol >= 0 &&
874                             args->protocol < nitems(linux_netlink_names))
875                                 nl_name = linux_netlink_names[args->protocol];
876                         else
877                                 nl_name = NULL;
878                         if (nl_name != NULL)
879                                 linux_msg(curthread,
880                                     "unsupported socket(AF_NETLINK, %d, "
881                                     "NETLINK_%s)", type, nl_name);
882                         else
883                                 linux_msg(curthread,
884                                     "unsupported socket(AF_NETLINK, %d, %d)",
885                                     type, args->protocol);
886                 } else {
887                         linux_msg(curthread, "unsupported socket domain %d, "
888                             "type %d, protocol %d", args->domain, type,
889                             args->protocol);
890                 }
891                 return (EAFNOSUPPORT);
892         }
893
894         retval_socket = kern_socket(td, domain, type, args->protocol);
895         if (retval_socket)
896                 return (retval_socket);
897
898         if (type == SOCK_RAW
899             && (args->protocol == IPPROTO_RAW || args->protocol == 0)
900             && domain == PF_INET) {
901                 /* It's a raw IP socket: set the IP_HDRINCL option. */
902                 int hdrincl;
903
904                 hdrincl = 1;
905                 /* We ignore any error returned by kern_setsockopt() */
906                 kern_setsockopt(td, td->td_retval[0], IPPROTO_IP, IP_HDRINCL,
907                     &hdrincl, UIO_SYSSPACE, sizeof(hdrincl));
908         }
909 #ifdef INET6
910         /*
911          * Linux AF_INET6 socket has IPV6_V6ONLY setsockopt set to 0 by default
912          * and some apps depend on this. So, set V6ONLY to 0 for Linux apps.
913          * For simplicity we do this unconditionally of the net.inet6.ip6.v6only
914          * sysctl value.
915          */
916         if (domain == PF_INET6) {
917                 int v6only;
918
919                 v6only = 0;
920                 /* We ignore any error returned by setsockopt() */
921                 kern_setsockopt(td, td->td_retval[0], IPPROTO_IPV6, IPV6_V6ONLY,
922                     &v6only, UIO_SYSSPACE, sizeof(v6only));
923         }
924 #endif
925
926         return (retval_socket);
927 }
928
929 int
930 linux_bind(struct thread *td, struct linux_bind_args *args)
931 {
932         struct sockaddr *sa;
933         int error;
934
935         error = linux_to_bsd_sockaddr(PTRIN(args->name), &sa,
936             &args->namelen);
937         if (error != 0)
938                 return (error);
939
940         error = kern_bindat(td, AT_FDCWD, args->s, sa);
941         free(sa, M_SONAME);
942
943         /* XXX */
944         if (error == EADDRNOTAVAIL && args->namelen != sizeof(struct sockaddr_in))
945                 return (EINVAL);
946         return (error);
947 }
948
949 int
950 linux_connect(struct thread *td, struct linux_connect_args *args)
951 {
952         struct socket *so;
953         struct sockaddr *sa;
954         struct file *fp;
955         u_int fflag;
956         int error;
957
958         error = linux_to_bsd_sockaddr(PTRIN(args->name), &sa,
959             &args->namelen);
960         if (error != 0)
961                 return (error);
962
963         error = kern_connectat(td, AT_FDCWD, args->s, sa);
964         free(sa, M_SONAME);
965         if (error != EISCONN)
966                 return (error);
967
968         /*
969          * Linux doesn't return EISCONN the first time it occurs,
970          * when on a non-blocking socket. Instead it returns the
971          * error getsockopt(SOL_SOCKET, SO_ERROR) would return on BSD.
972          */
973         error = getsock_cap(td, args->s, &cap_connect_rights,
974             &fp, &fflag, NULL);
975         if (error != 0)
976                 return (error);
977
978         error = EISCONN;
979         so = fp->f_data;
980         if (fflag & FNONBLOCK) {
981                 SOCK_LOCK(so);
982                 if (so->so_emuldata == 0)
983                         error = so->so_error;
984                 so->so_emuldata = (void *)1;
985                 SOCK_UNLOCK(so);
986         }
987         fdrop(fp, td);
988
989         return (error);
990 }
991
992 int
993 linux_listen(struct thread *td, struct linux_listen_args *args)
994 {
995
996         return (kern_listen(td, args->s, args->backlog));
997 }
998
999 static int
1000 linux_accept_common(struct thread *td, int s, l_uintptr_t addr,
1001     l_uintptr_t namelen, int flags)
1002 {
1003         struct sockaddr *sa;
1004         struct file *fp, *fp1;
1005         int bflags, len;
1006         struct socket *so;
1007         int error, error1;
1008
1009         bflags = 0;
1010         fp = NULL;
1011         sa = NULL;
1012
1013         error = linux_set_socket_flags(flags, &bflags);
1014         if (error != 0)
1015                 return (error);
1016
1017         if (PTRIN(addr) == NULL) {
1018                 len = 0;
1019                 error = kern_accept4(td, s, NULL, NULL, bflags, NULL);
1020         } else {
1021                 error = copyin(PTRIN(namelen), &len, sizeof(len));
1022                 if (error != 0)
1023                         return (error);
1024                 if (len < 0)
1025                         return (EINVAL);
1026                 error = kern_accept4(td, s, &sa, &len, bflags, &fp);
1027         }
1028
1029         /*
1030          * Translate errno values into ones used by Linux.
1031          */
1032         if (error != 0) {
1033                 /*
1034                  * XXX. This is wrong, different sockaddr structures
1035                  * have different sizes.
1036                  */
1037                 switch (error) {
1038                 case EFAULT:
1039                         if (namelen != sizeof(struct sockaddr_in))
1040                                 error = EINVAL;
1041                         break;
1042                 case EINVAL:
1043                         error1 = getsock_cap(td, s, &cap_accept_rights, &fp1, NULL, NULL);
1044                         if (error1 != 0) {
1045                                 error = error1;
1046                                 break;
1047                         }
1048                         so = fp1->f_data;
1049                         if (so->so_type == SOCK_DGRAM)
1050                                 error = EOPNOTSUPP;
1051                         fdrop(fp1, td);
1052                         break;
1053                 }
1054                 return (error);
1055         }
1056
1057         if (len != 0) {
1058                 error = linux_copyout_sockaddr(sa, PTRIN(addr), len);
1059                 if (error == 0)
1060                         error = copyout(&len, PTRIN(namelen),
1061                             sizeof(len));
1062                 if (error != 0) {
1063                         fdclose(td, fp, td->td_retval[0]);
1064                         td->td_retval[0] = 0;
1065                 }
1066         }
1067         if (fp != NULL)
1068                 fdrop(fp, td);
1069         free(sa, M_SONAME);
1070         return (error);
1071 }
1072
1073 int
1074 linux_accept(struct thread *td, struct linux_accept_args *args)
1075 {
1076
1077         return (linux_accept_common(td, args->s, args->addr,
1078             args->namelen, 0));
1079 }
1080
1081 int
1082 linux_accept4(struct thread *td, struct linux_accept4_args *args)
1083 {
1084
1085         return (linux_accept_common(td, args->s, args->addr,
1086             args->namelen, args->flags));
1087 }
1088
1089 int
1090 linux_getsockname(struct thread *td, struct linux_getsockname_args *args)
1091 {
1092         struct sockaddr *sa;
1093         int len, error;
1094
1095         error = copyin(PTRIN(args->namelen), &len, sizeof(len));
1096         if (error != 0)
1097                 return (error);
1098
1099         error = kern_getsockname(td, args->s, &sa, &len);
1100         if (error != 0)
1101                 return (error);
1102
1103         if (len != 0)
1104                 error = linux_copyout_sockaddr(sa, PTRIN(args->addr), len);
1105
1106         free(sa, M_SONAME);
1107         if (error == 0)
1108                 error = copyout(&len, PTRIN(args->namelen), sizeof(len));
1109         return (error);
1110 }
1111
1112 int
1113 linux_getpeername(struct thread *td, struct linux_getpeername_args *args)
1114 {
1115         struct sockaddr *sa;
1116         int len, error;
1117
1118         error = copyin(PTRIN(args->namelen), &len, sizeof(len));
1119         if (error != 0)
1120                 return (error);
1121         if (len < 0)
1122                 return (EINVAL);
1123
1124         error = kern_getpeername(td, args->s, &sa, &len);
1125         if (error != 0)
1126                 return (error);
1127
1128         if (len != 0)
1129                 error = linux_copyout_sockaddr(sa, PTRIN(args->addr), len);
1130
1131         free(sa, M_SONAME);
1132         if (error == 0)
1133                 error = copyout(&len, PTRIN(args->namelen), sizeof(len));
1134         return (error);
1135 }
1136
1137 int
1138 linux_socketpair(struct thread *td, struct linux_socketpair_args *args)
1139 {
1140         int domain, error, sv[2], type;
1141
1142         domain = linux_to_bsd_domain(args->domain);
1143         if (domain != PF_LOCAL)
1144                 return (EAFNOSUPPORT);
1145         type = args->type & LINUX_SOCK_TYPE_MASK;
1146         if (type < 0 || type > LINUX_SOCK_MAX)
1147                 return (EINVAL);
1148         error = linux_set_socket_flags(args->type & ~LINUX_SOCK_TYPE_MASK,
1149             &type);
1150         if (error != 0)
1151                 return (error);
1152         if (args->protocol != 0 && args->protocol != PF_UNIX) {
1153                 /*
1154                  * Use of PF_UNIX as protocol argument is not right,
1155                  * but Linux does it.
1156                  * Do not map PF_UNIX as its Linux value is identical
1157                  * to FreeBSD one.
1158                  */
1159                 return (EPROTONOSUPPORT);
1160         }
1161         error = kern_socketpair(td, domain, type, 0, sv);
1162         if (error != 0)
1163                 return (error);
1164         error = copyout(sv, PTRIN(args->rsv), 2 * sizeof(int));
1165         if (error != 0) {
1166                 (void)kern_close(td, sv[0]);
1167                 (void)kern_close(td, sv[1]);
1168         }
1169         return (error);
1170 }
1171
1172 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1173 struct linux_send_args {
1174         register_t s;
1175         register_t msg;
1176         register_t len;
1177         register_t flags;
1178 };
1179
1180 static int
1181 linux_send(struct thread *td, struct linux_send_args *args)
1182 {
1183         struct sendto_args /* {
1184                 int s;
1185                 caddr_t buf;
1186                 int len;
1187                 int flags;
1188                 caddr_t to;
1189                 int tolen;
1190         } */ bsd_args;
1191         struct file *fp;
1192         int error, fflag;
1193
1194         bsd_args.s = args->s;
1195         bsd_args.buf = (caddr_t)PTRIN(args->msg);
1196         bsd_args.len = args->len;
1197         bsd_args.flags = linux_to_bsd_msg_flags(args->flags);
1198         bsd_args.to = NULL;
1199         bsd_args.tolen = 0;
1200         error = sys_sendto(td, &bsd_args);
1201         if (error == ENOTCONN) {
1202                 /*
1203                  * Linux doesn't return ENOTCONN for non-blocking sockets.
1204                  * Instead it returns the EAGAIN.
1205                  */
1206                 error = getsock_cap(td, args->s, &cap_send_rights, &fp,
1207                     &fflag, NULL);
1208                 if (error == 0) {
1209                         if (fflag & FNONBLOCK)
1210                                 error = EAGAIN;
1211                         fdrop(fp, td);
1212                 }
1213         }
1214         return (error);
1215 }
1216
1217 struct linux_recv_args {
1218         register_t s;
1219         register_t msg;
1220         register_t len;
1221         register_t flags;
1222 };
1223
1224 static int
1225 linux_recv(struct thread *td, struct linux_recv_args *args)
1226 {
1227         struct recvfrom_args /* {
1228                 int s;
1229                 caddr_t buf;
1230                 int len;
1231                 int flags;
1232                 struct sockaddr *from;
1233                 socklen_t fromlenaddr;
1234         } */ bsd_args;
1235
1236         bsd_args.s = args->s;
1237         bsd_args.buf = (caddr_t)PTRIN(args->msg);
1238         bsd_args.len = args->len;
1239         bsd_args.flags = linux_to_bsd_msg_flags(args->flags);
1240         bsd_args.from = NULL;
1241         bsd_args.fromlenaddr = 0;
1242         return (sys_recvfrom(td, &bsd_args));
1243 }
1244 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1245
1246 int
1247 linux_sendto(struct thread *td, struct linux_sendto_args *args)
1248 {
1249         struct msghdr msg;
1250         struct iovec aiov;
1251         struct socket *so;
1252         struct file *fp;
1253         int error;
1254
1255         if (linux_check_hdrincl(td, args->s) == 0)
1256                 /* IP_HDRINCL set, tweak the packet before sending */
1257                 return (linux_sendto_hdrincl(td, args));
1258
1259         bzero(&msg, sizeof(msg));
1260         error = getsock_cap(td, args->s, &cap_send_connect_rights,
1261             &fp, NULL, NULL);
1262         if (error != 0)
1263                 return (error);
1264         so = fp->f_data;
1265         if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0) {
1266                 msg.msg_name = PTRIN(args->to);
1267                 msg.msg_namelen = args->tolen;
1268         }
1269         msg.msg_iov = &aiov;
1270         msg.msg_iovlen = 1;
1271         aiov.iov_base = PTRIN(args->msg);
1272         aiov.iov_len = args->len;
1273         fdrop(fp, td);
1274         return (linux_sendit(td, args->s, &msg, args->flags, NULL,
1275             UIO_USERSPACE));
1276 }
1277
1278 int
1279 linux_recvfrom(struct thread *td, struct linux_recvfrom_args *args)
1280 {
1281         struct sockaddr *sa;
1282         struct msghdr msg;
1283         struct iovec aiov;
1284         int error, fromlen;
1285
1286         if (PTRIN(args->fromlen) != NULL) {
1287                 error = copyin(PTRIN(args->fromlen), &fromlen,
1288                     sizeof(fromlen));
1289                 if (error != 0)
1290                         return (error);
1291                 if (fromlen < 0)
1292                         return (EINVAL);
1293                 fromlen = min(fromlen, SOCK_MAXADDRLEN);
1294                 sa = malloc(fromlen, M_SONAME, M_WAITOK);
1295         } else {
1296                 fromlen = 0;
1297                 sa = NULL;
1298         }
1299
1300         msg.msg_name = sa;
1301         msg.msg_namelen = fromlen;
1302         msg.msg_iov = &aiov;
1303         msg.msg_iovlen = 1;
1304         aiov.iov_base = PTRIN(args->buf);
1305         aiov.iov_len = args->len;
1306         msg.msg_control = 0;
1307         msg.msg_flags = linux_to_bsd_msg_flags(args->flags);
1308
1309         error = kern_recvit(td, args->s, &msg, UIO_SYSSPACE, NULL);
1310         if (error != 0)
1311                 goto out;
1312
1313         /*
1314          * XXX. Seems that FreeBSD is different from Linux here. Linux
1315          * fill source address if underlying protocol provides it, while
1316          * FreeBSD fill it if underlying protocol is not connection-oriented.
1317          * So, kern_recvit() set msg.msg_namelen to 0 if protocol pr_flags
1318          * does not contains PR_ADDR flag.
1319          */
1320         if (PTRIN(args->from) != NULL && msg.msg_namelen != 0)
1321                 error = linux_copyout_sockaddr(sa, PTRIN(args->from),
1322                     msg.msg_namelen);
1323
1324         if (error == 0 && PTRIN(args->fromlen) != NULL)
1325                 error = copyout(&msg.msg_namelen, PTRIN(args->fromlen),
1326                     sizeof(msg.msg_namelen));
1327 out:
1328         free(sa, M_SONAME);
1329         return (error);
1330 }
1331
1332 static int
1333 linux_sendmsg_common(struct thread *td, l_int s, struct l_msghdr *msghdr,
1334     l_uint flags)
1335 {
1336         struct cmsghdr *cmsg;
1337         struct mbuf *control;
1338         struct msghdr msg;
1339         struct l_cmsghdr linux_cmsg;
1340         struct l_cmsghdr *ptr_cmsg;
1341         struct l_msghdr linux_msghdr;
1342         struct iovec *iov;
1343         socklen_t datalen;
1344         struct sockaddr *sa;
1345         struct socket *so;
1346         sa_family_t sa_family;
1347         struct file *fp;
1348         void *data;
1349         l_size_t len;
1350         l_size_t clen;
1351         int error, fflag;
1352
1353         error = copyin(msghdr, &linux_msghdr, sizeof(linux_msghdr));
1354         if (error != 0)
1355                 return (error);
1356
1357         /*
1358          * Some Linux applications (ping) define a non-NULL control data
1359          * pointer, but a msg_controllen of 0, which is not allowed in the
1360          * FreeBSD system call interface.  NULL the msg_control pointer in
1361          * order to handle this case.  This should be checked, but allows the
1362          * Linux ping to work.
1363          */
1364         if (PTRIN(linux_msghdr.msg_control) != NULL &&
1365             linux_msghdr.msg_controllen == 0)
1366                 linux_msghdr.msg_control = PTROUT(NULL);
1367
1368         error = linux_to_bsd_msghdr(&msg, &linux_msghdr);
1369         if (error != 0)
1370                 return (error);
1371
1372 #ifdef COMPAT_LINUX32
1373         error = linux32_copyiniov(PTRIN(msg.msg_iov), msg.msg_iovlen,
1374             &iov, EMSGSIZE);
1375 #else
1376         error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
1377 #endif
1378         if (error != 0)
1379                 return (error);
1380
1381         control = NULL;
1382
1383         error = kern_getsockname(td, s, &sa, &datalen);
1384         if (error != 0)
1385                 goto bad;
1386         sa_family = sa->sa_family;
1387         free(sa, M_SONAME);
1388
1389         if (flags & LINUX_MSG_OOB) {
1390                 error = EOPNOTSUPP;
1391                 if (sa_family == AF_UNIX)
1392                         goto bad;
1393
1394                 error = getsock_cap(td, s, &cap_send_rights, &fp,
1395                     &fflag, NULL);
1396                 if (error != 0)
1397                         goto bad;
1398                 so = fp->f_data;
1399                 if (so->so_type != SOCK_STREAM)
1400                         error = EOPNOTSUPP;
1401                 fdrop(fp, td);
1402                 if (error != 0)
1403                         goto bad;
1404         }
1405
1406         if (linux_msghdr.msg_controllen >= sizeof(struct l_cmsghdr)) {
1407                 error = ENOBUFS;
1408                 control = m_get(M_WAITOK, MT_CONTROL);
1409                 MCLGET(control, M_WAITOK);
1410                 data = mtod(control, void *);
1411                 datalen = 0;
1412
1413                 ptr_cmsg = PTRIN(linux_msghdr.msg_control);
1414                 clen = linux_msghdr.msg_controllen;
1415                 do {
1416                         error = copyin(ptr_cmsg, &linux_cmsg,
1417                             sizeof(struct l_cmsghdr));
1418                         if (error != 0)
1419                                 goto bad;
1420
1421                         error = EINVAL;
1422                         if (linux_cmsg.cmsg_len < sizeof(struct l_cmsghdr) ||
1423                             linux_cmsg.cmsg_len > clen)
1424                                 goto bad;
1425
1426                         if (datalen + CMSG_HDRSZ > MCLBYTES)
1427                                 goto bad;
1428
1429                         /*
1430                          * Now we support only SCM_RIGHTS and SCM_CRED,
1431                          * so return EINVAL in any other cmsg_type
1432                          */
1433                         cmsg = data;
1434                         cmsg->cmsg_type =
1435                             linux_to_bsd_cmsg_type(linux_cmsg.cmsg_type);
1436                         cmsg->cmsg_level =
1437                             linux_to_bsd_sockopt_level(linux_cmsg.cmsg_level);
1438                         if (cmsg->cmsg_type == -1
1439                             || cmsg->cmsg_level != SOL_SOCKET) {
1440                                 linux_msg(curthread,
1441                                     "unsupported sendmsg cmsg level %d type %d",
1442                                     linux_cmsg.cmsg_level, linux_cmsg.cmsg_type);
1443                                 goto bad;
1444                         }
1445
1446                         /*
1447                          * Some applications (e.g. pulseaudio) attempt to
1448                          * send ancillary data even if the underlying protocol
1449                          * doesn't support it which is not allowed in the
1450                          * FreeBSD system call interface.
1451                          */
1452                         if (sa_family != AF_UNIX)
1453                                 goto next;
1454
1455                         if (cmsg->cmsg_type == SCM_CREDS) {
1456                                 len = sizeof(struct cmsgcred);
1457                                 if (datalen + CMSG_SPACE(len) > MCLBYTES)
1458                                         goto bad;
1459
1460                                 /*
1461                                  * The lower levels will fill in the structure
1462                                  */
1463                                 memset(CMSG_DATA(data), 0, len);
1464                         } else {
1465                                 len = linux_cmsg.cmsg_len - L_CMSG_HDRSZ;
1466                                 if (datalen + CMSG_SPACE(len) < datalen ||
1467                                     datalen + CMSG_SPACE(len) > MCLBYTES)
1468                                         goto bad;
1469
1470                                 error = copyin(LINUX_CMSG_DATA(ptr_cmsg),
1471                                     CMSG_DATA(data), len);
1472                                 if (error != 0)
1473                                         goto bad;
1474                         }
1475
1476                         cmsg->cmsg_len = CMSG_LEN(len);
1477                         data = (char *)data + CMSG_SPACE(len);
1478                         datalen += CMSG_SPACE(len);
1479
1480 next:
1481                         if (clen <= LINUX_CMSG_ALIGN(linux_cmsg.cmsg_len))
1482                                 break;
1483
1484                         clen -= LINUX_CMSG_ALIGN(linux_cmsg.cmsg_len);
1485                         ptr_cmsg = (struct l_cmsghdr *)((char *)ptr_cmsg +
1486                             LINUX_CMSG_ALIGN(linux_cmsg.cmsg_len));
1487                 } while(clen >= sizeof(struct l_cmsghdr));
1488
1489                 control->m_len = datalen;
1490                 if (datalen == 0) {
1491                         m_freem(control);
1492                         control = NULL;
1493                 }
1494         }
1495
1496         msg.msg_iov = iov;
1497         msg.msg_flags = 0;
1498         error = linux_sendit(td, s, &msg, flags, control, UIO_USERSPACE);
1499         control = NULL;
1500
1501 bad:
1502         m_freem(control);
1503         free(iov, M_IOV);
1504         return (error);
1505 }
1506
1507 int
1508 linux_sendmsg(struct thread *td, struct linux_sendmsg_args *args)
1509 {
1510
1511         return (linux_sendmsg_common(td, args->s, PTRIN(args->msg),
1512             args->flags));
1513 }
1514
1515 int
1516 linux_sendmmsg(struct thread *td, struct linux_sendmmsg_args *args)
1517 {
1518         struct l_mmsghdr *msg;
1519         l_uint retval;
1520         int error, datagrams;
1521
1522         if (args->vlen > UIO_MAXIOV)
1523                 args->vlen = UIO_MAXIOV;
1524
1525         msg = PTRIN(args->msg);
1526         datagrams = 0;
1527         while (datagrams < args->vlen) {
1528                 error = linux_sendmsg_common(td, args->s, &msg->msg_hdr,
1529                     args->flags);
1530                 if (error != 0)
1531                         break;
1532
1533                 retval = td->td_retval[0];
1534                 error = copyout(&retval, &msg->msg_len, sizeof(msg->msg_len));
1535                 if (error != 0)
1536                         break;
1537                 ++msg;
1538                 ++datagrams;
1539         }
1540         if (error == 0)
1541                 td->td_retval[0] = datagrams;
1542         return (error);
1543 }
1544
1545 static int
1546 recvmsg_scm_rights(struct thread *td, l_uint flags, socklen_t *datalen,
1547     void **data, void **udata)
1548 {
1549         int i, fd, fds, *fdp;
1550
1551         if (flags & LINUX_MSG_CMSG_CLOEXEC) {
1552                 fds = *datalen / sizeof(int);
1553                 fdp = *data;
1554                 for (i = 0; i < fds; i++) {
1555                         fd = *fdp++;
1556                         (void)kern_fcntl(td, fd, F_SETFD, FD_CLOEXEC);
1557                 }
1558         }
1559         return (0);
1560 }
1561
1562
1563 static int
1564 recvmsg_scm_creds(socklen_t *datalen, void **data, void **udata)
1565 {
1566         struct cmsgcred *cmcred;
1567         struct l_ucred lu;
1568
1569         cmcred = *data;
1570         lu.pid = cmcred->cmcred_pid;
1571         lu.uid = cmcred->cmcred_uid;
1572         lu.gid = cmcred->cmcred_gid;
1573         memmove(*data, &lu, sizeof(lu));
1574         *datalen = sizeof(lu);
1575         return (0);
1576 }
1577 _Static_assert(sizeof(struct cmsgcred) >= sizeof(struct l_ucred),
1578     "scm_creds sizeof l_ucred");
1579
1580 static int
1581 recvmsg_scm_creds2(socklen_t *datalen, void **data, void **udata)
1582 {
1583         struct sockcred2 *scred;
1584         struct l_ucred lu;
1585
1586         scred = *data;
1587         lu.pid = scred->sc_pid;
1588         lu.uid = scred->sc_uid;
1589         lu.gid = scred->sc_gid;
1590         memmove(*data, &lu, sizeof(lu));
1591         *datalen = sizeof(lu);
1592         return (0);
1593 }
1594 _Static_assert(sizeof(struct sockcred2) >= sizeof(struct l_ucred),
1595     "scm_creds2 sizeof l_ucred");
1596
1597 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1598 static int
1599 recvmsg_scm_timestamp(l_int msg_type, socklen_t *datalen, void **data,
1600     void **udata)
1601 {
1602         l_sock_timeval ltv64;
1603         l_timeval ltv;
1604         struct timeval *tv;
1605         socklen_t len;
1606         void *buf;
1607
1608         if (*datalen != sizeof(struct timeval))
1609                 return (EMSGSIZE);
1610
1611         tv = *data;
1612 #if defined(COMPAT_LINUX32)
1613         if (msg_type == LINUX_SCM_TIMESTAMPO &&
1614             (tv->tv_sec > INT_MAX || tv->tv_sec < INT_MIN))
1615                 return (EOVERFLOW);
1616 #endif
1617         if (msg_type == LINUX_SCM_TIMESTAMPN)
1618                 len = sizeof(ltv64);
1619         else
1620                 len = sizeof(ltv);
1621
1622         buf = malloc(len, M_LINUX, M_WAITOK);
1623         if (msg_type == LINUX_SCM_TIMESTAMPN) {
1624                 ltv64.tv_sec = tv->tv_sec;
1625                 ltv64.tv_usec = tv->tv_usec;
1626                 memmove(buf, &ltv64, len);
1627         } else {
1628                 ltv.tv_sec = tv->tv_sec;
1629                 ltv.tv_usec = tv->tv_usec;
1630                 memmove(buf, &ltv, len);
1631         }
1632         *data = *udata = buf;
1633         *datalen = len;
1634         return (0);
1635 }
1636 #else
1637 _Static_assert(sizeof(struct timeval) == sizeof(l_timeval),
1638     "scm_timestamp sizeof l_timeval");
1639 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1640
1641 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1642 static int
1643 recvmsg_scm_timestampns(l_int msg_type, socklen_t *datalen, void **data,
1644     void **udata)
1645 {
1646         struct l_timespec64 ts64;
1647         struct l_timespec ts32;
1648         struct timespec ts;
1649         socklen_t len;
1650         void *buf;
1651
1652         if (msg_type == LINUX_SCM_TIMESTAMPNSO)
1653                 len = sizeof(ts32);
1654         else
1655                 len = sizeof(ts64);
1656
1657         buf = malloc(len, M_LINUX, M_WAITOK);
1658         bintime2timespec(*data, &ts);
1659         if (msg_type == LINUX_SCM_TIMESTAMPNSO) {
1660                 ts32.tv_sec = ts.tv_sec;
1661                 ts32.tv_nsec = ts.tv_nsec;
1662                 memmove(buf, &ts32, len);
1663         } else {
1664                 ts64.tv_sec = ts.tv_sec;
1665                 ts64.tv_nsec = ts.tv_nsec;
1666                 memmove(buf, &ts64, len);
1667         }
1668         *data = *udata = buf;
1669         *datalen = len;
1670         return (0);
1671 }
1672 #else
1673 static int
1674 recvmsg_scm_timestampns(l_int msg_type, socklen_t *datalen, void **data,
1675     void **udata)
1676 {
1677         struct timespec ts;
1678
1679         bintime2timespec(*data, &ts);
1680         memmove(*data, &ts, sizeof(struct timespec));
1681         *datalen = sizeof(struct timespec);
1682         return (0);
1683 }
1684 _Static_assert(sizeof(struct bintime) >= sizeof(struct timespec),
1685     "scm_timestampns sizeof timespec");
1686 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1687
1688 static int
1689 linux_recvmsg_common(struct thread *td, l_int s, struct l_msghdr *msghdr,
1690     l_uint flags, struct msghdr *msg)
1691 {
1692         struct proc *p = td->td_proc;
1693         struct cmsghdr *cm;
1694         struct l_cmsghdr *lcm = NULL;
1695         socklen_t datalen, maxlen, outlen;
1696         struct l_msghdr l_msghdr;
1697         struct iovec *iov, *uiov;
1698         struct mbuf *control = NULL;
1699         struct mbuf **controlp;
1700         struct sockaddr *sa;
1701         caddr_t outbuf;
1702         void *data, *udata;
1703         int error;
1704
1705         error = copyin(msghdr, &l_msghdr, sizeof(l_msghdr));
1706         if (error != 0)
1707                 return (error);
1708
1709         /*
1710          * Pass user-supplied recvmsg() flags in msg_flags field,
1711          * following sys_recvmsg() convention.
1712         */
1713         l_msghdr.msg_flags = flags;
1714
1715         error = linux_to_bsd_msghdr(msg, &l_msghdr);
1716         if (error != 0)
1717                 return (error);
1718
1719 #ifdef COMPAT_LINUX32
1720         error = linux32_copyiniov(PTRIN(msg->msg_iov), msg->msg_iovlen,
1721             &iov, EMSGSIZE);
1722 #else
1723         error = copyiniov(msg->msg_iov, msg->msg_iovlen, &iov, EMSGSIZE);
1724 #endif
1725         if (error != 0)
1726                 return (error);
1727
1728         if (msg->msg_name != NULL && msg->msg_namelen > 0) {
1729                 msg->msg_namelen = min(msg->msg_namelen, SOCK_MAXADDRLEN);
1730                 sa = malloc(msg->msg_namelen, M_SONAME, M_WAITOK);
1731                 msg->msg_name = sa;
1732         } else {
1733                 sa = NULL;
1734                 msg->msg_name = NULL;
1735         }
1736
1737         uiov = msg->msg_iov;
1738         msg->msg_iov = iov;
1739         controlp = (msg->msg_control != NULL) ? &control : NULL;
1740         error = kern_recvit(td, s, msg, UIO_SYSSPACE, controlp);
1741         msg->msg_iov = uiov;
1742         if (error != 0)
1743                 goto bad;
1744
1745         /*
1746          * Note that kern_recvit() updates msg->msg_namelen.
1747          */
1748         if (msg->msg_name != NULL && msg->msg_namelen > 0) {
1749                 msg->msg_name = PTRIN(l_msghdr.msg_name);
1750                 error = linux_copyout_sockaddr(sa, msg->msg_name,
1751                     msg->msg_namelen);
1752                 if (error != 0)
1753                         goto bad;
1754         }
1755
1756         error = bsd_to_linux_msghdr(msg, &l_msghdr);
1757         if (error != 0)
1758                 goto bad;
1759
1760         maxlen = l_msghdr.msg_controllen;
1761         l_msghdr.msg_controllen = 0;
1762         if (control == NULL)
1763                 goto out;
1764
1765         lcm = malloc(L_CMSG_HDRSZ, M_LINUX, M_WAITOK | M_ZERO);
1766         msg->msg_control = mtod(control, struct cmsghdr *);
1767         msg->msg_controllen = control->m_len;
1768         cm = CMSG_FIRSTHDR(msg);
1769         outbuf = PTRIN(l_msghdr.msg_control);
1770         outlen = 0;
1771         while (cm != NULL) {
1772                 lcm->cmsg_type = bsd_to_linux_cmsg_type(p, cm->cmsg_type);
1773                 lcm->cmsg_level = bsd_to_linux_sockopt_level(cm->cmsg_level);
1774                 if (lcm->cmsg_type == -1 ||
1775                     cm->cmsg_level != SOL_SOCKET) {
1776                         linux_msg(curthread,
1777                             "unsupported recvmsg cmsg level %d type %d",
1778                             cm->cmsg_level, cm->cmsg_type);
1779                         error = EINVAL;
1780                         goto bad;
1781                 }
1782
1783                 data = CMSG_DATA(cm);
1784                 datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
1785                 udata = NULL;
1786
1787                 switch (cm->cmsg_type) {
1788                 case SCM_RIGHTS:
1789                         error = recvmsg_scm_rights(td, flags,
1790                             &datalen, &data, &udata);
1791                         break;
1792                 case SCM_CREDS:
1793                         error = recvmsg_scm_creds(&datalen,
1794                             &data, &udata);
1795                         break;
1796                 case SCM_CREDS2:
1797                         error = recvmsg_scm_creds2(&datalen,
1798                             &data, &udata);
1799                         break;
1800                 case SCM_TIMESTAMP:
1801 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1802                         error = recvmsg_scm_timestamp(lcm->cmsg_type,
1803                             &datalen, &data, &udata);
1804 #endif
1805                         break;
1806                 case SCM_BINTIME:
1807                         error = recvmsg_scm_timestampns(lcm->cmsg_type,
1808                             &datalen, &data, &udata);
1809                         break;
1810                 }
1811                 if (error != 0)
1812                         goto bad;
1813
1814                 if (outlen + LINUX_CMSG_LEN(datalen) > maxlen) {
1815                         if (outlen == 0) {
1816                                 error = EMSGSIZE;
1817                                 goto err;
1818                         } else {
1819                                 l_msghdr.msg_flags |= LINUX_MSG_CTRUNC;
1820                                 m_dispose_extcontrolm(control);
1821                                 free(udata, M_LINUX);
1822                                 goto out;
1823                         }
1824                 }
1825
1826                 lcm->cmsg_len = LINUX_CMSG_LEN(datalen);
1827                 error = copyout(lcm, outbuf, L_CMSG_HDRSZ);
1828                 if (error == 0) {
1829                         outbuf += L_CMSG_HDRSZ;
1830                         error = copyout(data, outbuf, datalen);
1831                         if (error == 0) {
1832                                 outbuf += LINUX_CMSG_ALIGN(datalen);
1833                                 outlen += LINUX_CMSG_LEN(datalen);
1834                                 cm = CMSG_NXTHDR(msg, cm);
1835                         }
1836                 }
1837 err:
1838                 free(udata, M_LINUX);
1839                 if (error != 0)
1840                         goto bad;
1841         }
1842         l_msghdr.msg_controllen = outlen;
1843
1844 out:
1845         error = copyout(&l_msghdr, msghdr, sizeof(l_msghdr));
1846
1847 bad:
1848         if (control != NULL) {
1849                 if (error != 0)
1850                         m_dispose_extcontrolm(control);
1851                 m_freem(control);
1852         }
1853         free(iov, M_IOV);
1854         free(lcm, M_LINUX);
1855         free(sa, M_SONAME);
1856
1857         return (error);
1858 }
1859
1860 int
1861 linux_recvmsg(struct thread *td, struct linux_recvmsg_args *args)
1862 {
1863         struct msghdr bsd_msg;
1864         struct file *fp;
1865         int error;
1866
1867         error = getsock_cap(td, args->s, &cap_recv_rights,
1868             &fp, NULL, NULL);
1869         if (error != 0)
1870                 return (error);
1871         fdrop(fp, td);
1872         return (linux_recvmsg_common(td, args->s, PTRIN(args->msg),
1873             args->flags, &bsd_msg));
1874 }
1875
1876 static int
1877 linux_recvmmsg_common(struct thread *td, l_int s, struct l_mmsghdr *msg,
1878     l_uint vlen, l_uint flags, struct timespec *tts)
1879 {
1880         struct msghdr bsd_msg;
1881         struct timespec ts;
1882         struct file *fp;
1883         l_uint retval;
1884         int error, datagrams;
1885
1886         error = getsock_cap(td, s, &cap_recv_rights,
1887             &fp, NULL, NULL);
1888         if (error != 0)
1889                 return (error);
1890         datagrams = 0;
1891         while (datagrams < vlen) {
1892                 error = linux_recvmsg_common(td, s, &msg->msg_hdr,
1893                     flags & ~LINUX_MSG_WAITFORONE, &bsd_msg);
1894                 if (error != 0)
1895                         break;
1896
1897                 retval = td->td_retval[0];
1898                 error = copyout(&retval, &msg->msg_len, sizeof(msg->msg_len));
1899                 if (error != 0)
1900                         break;
1901                 ++msg;
1902                 ++datagrams;
1903
1904                 /*
1905                  * MSG_WAITFORONE turns on MSG_DONTWAIT after one packet.
1906                  */
1907                 if (flags & LINUX_MSG_WAITFORONE)
1908                         flags |= LINUX_MSG_DONTWAIT;
1909
1910                 /*
1911                  * See BUGS section of recvmmsg(2).
1912                  */
1913                 if (tts) {
1914                         getnanotime(&ts);
1915                         timespecsub(&ts, tts, &ts);
1916                         if (!timespecisset(&ts) || ts.tv_sec > 0)
1917                                 break;
1918                 }
1919                 /* Out of band data, return right away. */
1920                 if (bsd_msg.msg_flags & MSG_OOB)
1921                         break;
1922         }
1923         if (error == 0)
1924                 td->td_retval[0] = datagrams;
1925         fdrop(fp, td);
1926         return (error);
1927 }
1928
1929 int
1930 linux_recvmmsg(struct thread *td, struct linux_recvmmsg_args *args)
1931 {
1932         struct timespec ts, tts, *ptts;
1933         int error;
1934
1935         if (args->timeout) {
1936                 error = linux_get_timespec(&ts, args->timeout);
1937                 if (error != 0)
1938                         return (error);
1939                 getnanotime(&tts);
1940                 timespecadd(&tts, &ts, &tts);
1941                 ptts = &tts;
1942         }
1943                 else ptts = NULL;
1944
1945         return (linux_recvmmsg_common(td, args->s, PTRIN(args->msg),
1946             args->vlen, args->flags, ptts));
1947 }
1948
1949 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1950 int
1951 linux_recvmmsg_time64(struct thread *td, struct linux_recvmmsg_time64_args *args)
1952 {
1953         struct timespec ts, tts, *ptts;
1954         int error;
1955
1956         if (args->timeout) {
1957                 error = linux_get_timespec64(&ts, args->timeout);
1958                 if (error != 0)
1959                         return (error);
1960                 getnanotime(&tts);
1961                 timespecadd(&tts, &ts, &tts);
1962                 ptts = &tts;
1963         }
1964                 else ptts = NULL;
1965
1966         return (linux_recvmmsg_common(td, args->s, PTRIN(args->msg),
1967             args->vlen, args->flags, ptts));
1968 }
1969 #endif
1970
1971 int
1972 linux_shutdown(struct thread *td, struct linux_shutdown_args *args)
1973 {
1974
1975         return (kern_shutdown(td, args->s, args->how));
1976 }
1977
1978 int
1979 linux_setsockopt(struct thread *td, struct linux_setsockopt_args *args)
1980 {
1981         struct proc *p = td->td_proc;
1982         struct linux_pemuldata *pem;
1983         l_timeval linux_tv;
1984         struct sockaddr *sa;
1985         struct timeval tv;
1986         socklen_t len;
1987         int error, level, name;
1988
1989         level = linux_to_bsd_sockopt_level(args->level);
1990         switch (level) {
1991         case SOL_SOCKET:
1992                 name = linux_to_bsd_so_sockopt(args->optname);
1993                 switch (name) {
1994                 case LOCAL_CREDS_PERSISTENT:
1995                         level = SOL_LOCAL;
1996                         break;
1997                 case SO_RCVTIMEO:
1998                         /* FALLTHROUGH */
1999                 case SO_SNDTIMEO:
2000                         error = copyin(PTRIN(args->optval), &linux_tv,
2001                             sizeof(linux_tv));
2002                         if (error != 0)
2003                                 return (error);
2004                         tv.tv_sec = linux_tv.tv_sec;
2005                         tv.tv_usec = linux_tv.tv_usec;
2006                         return (kern_setsockopt(td, args->s, level,
2007                             name, &tv, UIO_SYSSPACE, sizeof(tv)));
2008                         /* NOTREACHED */
2009                 case SO_TIMESTAMP:
2010                         pem = pem_find(p);
2011                         pem->so_timestamp = args->optname;
2012                         break;
2013                 case SO_BINTIME:
2014                         pem = pem_find(p);
2015                         pem->so_timestampns = args->optname;
2016                         break;
2017                 default:
2018                         break;
2019                 }
2020                 break;
2021         case IPPROTO_IP:
2022                 if (args->optname == LINUX_IP_RECVERR &&
2023                     linux_ignore_ip_recverr) {
2024                         /*
2025                          * XXX: This is a hack to unbreak DNS resolution
2026                          *      with glibc 2.30 and above.
2027                          */
2028                         return (0);
2029                 }
2030                 name = linux_to_bsd_ip_sockopt(args->optname);
2031                 break;
2032         case IPPROTO_IPV6:
2033                 name = linux_to_bsd_ip6_sockopt(args->optname);
2034                 break;
2035         case IPPROTO_TCP:
2036                 name = linux_to_bsd_tcp_sockopt(args->optname);
2037                 break;
2038         default:
2039                 name = -1;
2040                 break;
2041         }
2042         if (name < 0) {
2043                 if (name == -1)
2044                         linux_msg(curthread,
2045                             "unsupported setsockopt level %d optname %d",
2046                             args->level, args->optname);
2047                 return (ENOPROTOOPT);
2048         }
2049
2050         if (name == IPV6_NEXTHOP) {
2051                 len = args->optlen;
2052                 error = linux_to_bsd_sockaddr(PTRIN(args->optval), &sa, &len);
2053                 if (error != 0)
2054                         return (error);
2055
2056                 error = kern_setsockopt(td, args->s, level,
2057                     name, sa, UIO_SYSSPACE, len);
2058                 free(sa, M_SONAME);
2059         } else {
2060                 error = kern_setsockopt(td, args->s, level,
2061                     name, PTRIN(args->optval), UIO_USERSPACE, args->optlen);
2062         }
2063
2064         return (error);
2065 }
2066
2067 static int
2068 linux_sockopt_copyout(struct thread *td, void *val, socklen_t len,
2069     struct linux_getsockopt_args *args)
2070 {
2071         int error;
2072
2073         error = copyout(val, PTRIN(args->optval), len);
2074         if (error == 0)
2075                 error = copyout(&len, PTRIN(args->optlen), sizeof(len));
2076         return (error);
2077 }
2078
2079 static int
2080 linux_getsockopt_so_peergroups(struct thread *td,
2081     struct linux_getsockopt_args *args)
2082 {
2083         struct xucred xu;
2084         socklen_t xulen, len;
2085         int error, i;
2086
2087         xulen = sizeof(xu);
2088         error = kern_getsockopt(td, args->s, 0,
2089             LOCAL_PEERCRED, &xu, UIO_SYSSPACE, &xulen);
2090         if (error != 0)
2091                 return (error);
2092
2093         len = xu.cr_ngroups * sizeof(l_gid_t);
2094         if (args->optlen < len) {
2095                 error = copyout(&len, PTRIN(args->optlen), sizeof(len));
2096                 if (error == 0)
2097                         error = ERANGE;
2098                 return (error);
2099         }
2100
2101         /*
2102          * "- 1" to skip the primary group.
2103          */
2104         for (i = 0; i < xu.cr_ngroups - 1; i++) {
2105                 error = copyout(xu.cr_groups + i + 1,
2106                     (void *)(args->optval + i * sizeof(l_gid_t)),
2107                     sizeof(l_gid_t));
2108                 if (error != 0)
2109                         return (error);
2110         }
2111
2112         error = copyout(&len, PTRIN(args->optlen), sizeof(len));
2113         return (error);
2114 }
2115
2116 static int
2117 linux_getsockopt_so_peersec(struct thread *td,
2118     struct linux_getsockopt_args *args)
2119 {
2120         socklen_t len;
2121         int error;
2122
2123         len = sizeof(SECURITY_CONTEXT_STRING);
2124         if (args->optlen < len) {
2125                 error = copyout(&len, PTRIN(args->optlen), sizeof(len));
2126                 if (error == 0)
2127                         error = ERANGE;
2128                 return (error);
2129         }
2130
2131         return (linux_sockopt_copyout(td, SECURITY_CONTEXT_STRING,
2132             len, args));
2133 }
2134
2135 static int
2136 linux_getsockopt_so_linger(struct thread *td,
2137     struct linux_getsockopt_args *args)
2138 {
2139         struct linger ling;
2140         socklen_t len;
2141         int error;
2142
2143         len = sizeof(ling);
2144         error = kern_getsockopt(td, args->s, SOL_SOCKET,
2145             SO_LINGER, &ling, UIO_SYSSPACE, &len);
2146         if (error != 0)
2147                 return (error);
2148         ling.l_onoff = ((ling.l_onoff & SO_LINGER) != 0);
2149         return (linux_sockopt_copyout(td, &ling, len, args));
2150 }
2151
2152 int
2153 linux_getsockopt(struct thread *td, struct linux_getsockopt_args *args)
2154 {
2155         l_timeval linux_tv;
2156         struct timeval tv;
2157         socklen_t tv_len, xulen, len;
2158         struct sockaddr *sa;
2159         struct xucred xu;
2160         struct l_ucred lxu;
2161         int error, level, name, newval;
2162
2163         level = linux_to_bsd_sockopt_level(args->level);
2164         switch (level) {
2165         case SOL_SOCKET:
2166                 switch (args->optname) {
2167                 case LINUX_SO_PEERGROUPS:
2168                         return (linux_getsockopt_so_peergroups(td, args));
2169                 case LINUX_SO_PEERSEC:
2170                         return (linux_getsockopt_so_peersec(td, args));
2171                 default:
2172                         break;
2173                 }
2174
2175                 name = linux_to_bsd_so_sockopt(args->optname);
2176                 switch (name) {
2177                 case LOCAL_CREDS_PERSISTENT:
2178                         level = SOL_LOCAL;
2179                         break;
2180                 case SO_RCVTIMEO:
2181                         /* FALLTHROUGH */
2182                 case SO_SNDTIMEO:
2183                         tv_len = sizeof(tv);
2184                         error = kern_getsockopt(td, args->s, level,
2185                             name, &tv, UIO_SYSSPACE, &tv_len);
2186                         if (error != 0)
2187                                 return (error);
2188                         linux_tv.tv_sec = tv.tv_sec;
2189                         linux_tv.tv_usec = tv.tv_usec;
2190                         return (linux_sockopt_copyout(td, &linux_tv,
2191                             sizeof(linux_tv), args));
2192                         /* NOTREACHED */
2193                 case LOCAL_PEERCRED:
2194                         if (args->optlen < sizeof(lxu))
2195                                 return (EINVAL);
2196                         /*
2197                          * LOCAL_PEERCRED is not served at the SOL_SOCKET level,
2198                          * but by the Unix socket's level 0.
2199                          */
2200                         level = 0;
2201                         xulen = sizeof(xu);
2202                         error = kern_getsockopt(td, args->s, level,
2203                             name, &xu, UIO_SYSSPACE, &xulen);
2204                         if (error != 0)
2205                                 return (error);
2206                         lxu.pid = xu.cr_pid;
2207                         lxu.uid = xu.cr_uid;
2208                         lxu.gid = xu.cr_gid;
2209                         return (linux_sockopt_copyout(td, &lxu,
2210                             sizeof(lxu), args));
2211                         /* NOTREACHED */
2212                 case SO_ERROR:
2213                         len = sizeof(newval);
2214                         error = kern_getsockopt(td, args->s, level,
2215                             name, &newval, UIO_SYSSPACE, &len);
2216                         if (error != 0)
2217                                 return (error);
2218                         newval = -bsd_to_linux_errno(newval);
2219                         return (linux_sockopt_copyout(td, &newval,
2220                             len, args));
2221                         /* NOTREACHED */
2222                 case SO_DOMAIN:
2223                         len = sizeof(newval);
2224                         error = kern_getsockopt(td, args->s, level,
2225                             name, &newval, UIO_SYSSPACE, &len);
2226                         if (error != 0)
2227                                 return (error);
2228                         newval = bsd_to_linux_domain(newval);
2229                         if (newval == -1)
2230                                 return (ENOPROTOOPT);
2231                         return (linux_sockopt_copyout(td, &newval,
2232                             len, args));
2233                         /* NOTREACHED */
2234                 case SO_LINGER:
2235                         return (linux_getsockopt_so_linger(td, args));
2236                         /* NOTREACHED */
2237                 default:
2238                         break;
2239                 }
2240                 break;
2241         case IPPROTO_IP:
2242                 name = linux_to_bsd_ip_sockopt(args->optname);
2243                 break;
2244         case IPPROTO_IPV6:
2245                 name = linux_to_bsd_ip6_sockopt(args->optname);
2246                 break;
2247         case IPPROTO_TCP:
2248                 name = linux_to_bsd_tcp_sockopt(args->optname);
2249                 break;
2250         default:
2251                 name = -1;
2252                 break;
2253         }
2254         if (name < 0) {
2255                 if (name == -1)
2256                         linux_msg(curthread,
2257                             "unsupported getsockopt level %d optname %d",
2258                             args->level, args->optname);
2259                 return (EINVAL);
2260         }
2261
2262         if (name == IPV6_NEXTHOP) {
2263                 error = copyin(PTRIN(args->optlen), &len, sizeof(len));
2264                 if (error != 0)
2265                         return (error);
2266                 sa = malloc(len, M_SONAME, M_WAITOK);
2267
2268                 error = kern_getsockopt(td, args->s, level,
2269                     name, sa, UIO_SYSSPACE, &len);
2270                 if (error != 0)
2271                         goto out;
2272
2273                 error = linux_copyout_sockaddr(sa, PTRIN(args->optval), len);
2274                 if (error == 0)
2275                         error = copyout(&len, PTRIN(args->optlen),
2276                             sizeof(len));
2277 out:
2278                 free(sa, M_SONAME);
2279         } else {
2280                 if (args->optval) {
2281                         error = copyin(PTRIN(args->optlen), &len, sizeof(len));
2282                         if (error != 0)
2283                                 return (error);
2284                 }
2285                 error = kern_getsockopt(td, args->s, level,
2286                     name, PTRIN(args->optval), UIO_USERSPACE, &len);
2287                 if (error == 0)
2288                         error = copyout(&len, PTRIN(args->optlen),
2289                             sizeof(len));
2290         }
2291
2292         return (error);
2293 }
2294
2295 static int
2296 linux_sendfile_common(struct thread *td, l_int out, l_int in,
2297     l_loff_t *offset, l_size_t count)
2298 {
2299         off_t bytes_read;
2300         int error;
2301         l_loff_t current_offset;
2302         struct file *fp;
2303
2304         AUDIT_ARG_FD(in);
2305         error = fget_read(td, in, &cap_pread_rights, &fp);
2306         if (error != 0)
2307                 return (error);
2308
2309         if (offset != NULL) {
2310                 current_offset = *offset;
2311         } else {
2312                 error = (fp->f_ops->fo_flags & DFLAG_SEEKABLE) != 0 ?
2313                     fo_seek(fp, 0, SEEK_CUR, td) : ESPIPE;
2314                 if (error != 0)
2315                         goto drop;
2316                 current_offset = td->td_uretoff.tdu_off;
2317         }
2318
2319         bytes_read = 0;
2320
2321         /* Linux cannot have 0 count. */
2322         if (count <= 0 || current_offset < 0) {
2323                 error = EINVAL;
2324                 goto drop;
2325         }
2326
2327         error = fo_sendfile(fp, out, NULL, NULL, current_offset, count,
2328             &bytes_read, 0, td);
2329         if (error != 0)
2330                 goto drop;
2331         current_offset += bytes_read;
2332
2333         if (offset != NULL) {
2334                 *offset = current_offset;
2335         } else {
2336                 error = fo_seek(fp, current_offset, SEEK_SET, td);
2337                 if (error != 0)
2338                         goto drop;
2339         }
2340
2341         td->td_retval[0] = (ssize_t)bytes_read;
2342 drop:
2343         fdrop(fp, td);
2344         if (error == ENOTSOCK)
2345                 error = EINVAL;
2346         return (error);
2347 }
2348
2349 int
2350 linux_sendfile(struct thread *td, struct linux_sendfile_args *arg)
2351 {
2352         /*
2353          * Differences between FreeBSD and Linux sendfile:
2354          * - Linux doesn't send anything when count is 0 (FreeBSD uses 0 to
2355          *   mean send the whole file.)  In linux_sendfile given fds are still
2356          *   checked for validity when the count is 0.
2357          * - Linux can send to any fd whereas FreeBSD only supports sockets.
2358          *   The same restriction follows for linux_sendfile.
2359          * - Linux doesn't have an equivalent for FreeBSD's flags and sf_hdtr.
2360          * - Linux takes an offset pointer and updates it to the read location.
2361          *   FreeBSD takes in an offset and a 'bytes read' parameter which is
2362          *   only filled if it isn't NULL.  We use this parameter to update the
2363          *   offset pointer if it exists.
2364          * - Linux sendfile returns bytes read on success while FreeBSD
2365          *   returns 0.  We use the 'bytes read' parameter to get this value.
2366          */
2367
2368         l_loff_t offset64;
2369         l_long offset;
2370         int ret;
2371         int error;
2372
2373         if (arg->offset != NULL) {
2374                 error = copyin(arg->offset, &offset, sizeof(offset));
2375                 if (error != 0)
2376                         return (error);
2377                 offset64 = (l_loff_t)offset;
2378         }
2379
2380         ret = linux_sendfile_common(td, arg->out, arg->in,
2381             arg->offset != NULL ? &offset64 : NULL, arg->count);
2382
2383         if (arg->offset != NULL) {
2384 #if defined(__i386__) || defined(__arm__) || \
2385     (defined(__amd64__) && defined(COMPAT_LINUX32))
2386                 if (offset64 > INT32_MAX)
2387                         return (EOVERFLOW);
2388 #endif
2389                 offset = (l_long)offset64;
2390                 error = copyout(&offset, arg->offset, sizeof(offset));
2391                 if (error != 0)
2392                         return (error);
2393         }
2394
2395         return (ret);
2396 }
2397
2398 #if defined(__i386__) || defined(__arm__) || \
2399     (defined(__amd64__) && defined(COMPAT_LINUX32))
2400
2401 int
2402 linux_sendfile64(struct thread *td, struct linux_sendfile64_args *arg)
2403 {
2404         l_loff_t offset;
2405         int ret;
2406         int error;
2407
2408         if (arg->offset != NULL) {
2409                 error = copyin(arg->offset, &offset, sizeof(offset));
2410                 if (error != 0)
2411                         return (error);
2412         }
2413
2414         ret = linux_sendfile_common(td, arg->out, arg->in,
2415                 arg->offset != NULL ? &offset : NULL, arg->count);
2416
2417         if (arg->offset != NULL) {
2418                 error = copyout(&offset, arg->offset, sizeof(offset));
2419                 if (error != 0)
2420                         return (error);
2421         }
2422
2423         return (ret);
2424 }
2425
2426 /* Argument list sizes for linux_socketcall */
2427 static const unsigned char lxs_args_cnt[] = {
2428         0 /* unused*/,          3 /* socket */,
2429         3 /* bind */,           3 /* connect */,
2430         2 /* listen */,         3 /* accept */,
2431         3 /* getsockname */,    3 /* getpeername */,
2432         4 /* socketpair */,     4 /* send */,
2433         4 /* recv */,           6 /* sendto */,
2434         6 /* recvfrom */,       2 /* shutdown */,
2435         5 /* setsockopt */,     5 /* getsockopt */,
2436         3 /* sendmsg */,        3 /* recvmsg */,
2437         4 /* accept4 */,        5 /* recvmmsg */,
2438         4 /* sendmmsg */,       4 /* sendfile */
2439 };
2440 #define LINUX_ARGS_CNT          (nitems(lxs_args_cnt) - 1)
2441 #define LINUX_ARG_SIZE(x)       (lxs_args_cnt[x] * sizeof(l_ulong))
2442
2443 int
2444 linux_socketcall(struct thread *td, struct linux_socketcall_args *args)
2445 {
2446         l_ulong a[6];
2447 #if defined(__amd64__) && defined(COMPAT_LINUX32)
2448         register_t l_args[6];
2449 #endif
2450         void *arg;
2451         int error;
2452
2453         if (args->what < LINUX_SOCKET || args->what > LINUX_ARGS_CNT)
2454                 return (EINVAL);
2455         error = copyin(PTRIN(args->args), a, LINUX_ARG_SIZE(args->what));
2456         if (error != 0)
2457                 return (error);
2458
2459 #if defined(__amd64__) && defined(COMPAT_LINUX32)
2460         for (int i = 0; i < lxs_args_cnt[args->what]; ++i)
2461                 l_args[i] = a[i];
2462         arg = l_args;
2463 #else
2464         arg = a;
2465 #endif
2466         switch (args->what) {
2467         case LINUX_SOCKET:
2468                 return (linux_socket(td, arg));
2469         case LINUX_BIND:
2470                 return (linux_bind(td, arg));
2471         case LINUX_CONNECT:
2472                 return (linux_connect(td, arg));
2473         case LINUX_LISTEN:
2474                 return (linux_listen(td, arg));
2475         case LINUX_ACCEPT:
2476                 return (linux_accept(td, arg));
2477         case LINUX_GETSOCKNAME:
2478                 return (linux_getsockname(td, arg));
2479         case LINUX_GETPEERNAME:
2480                 return (linux_getpeername(td, arg));
2481         case LINUX_SOCKETPAIR:
2482                 return (linux_socketpair(td, arg));
2483         case LINUX_SEND:
2484                 return (linux_send(td, arg));
2485         case LINUX_RECV:
2486                 return (linux_recv(td, arg));
2487         case LINUX_SENDTO:
2488                 return (linux_sendto(td, arg));
2489         case LINUX_RECVFROM:
2490                 return (linux_recvfrom(td, arg));
2491         case LINUX_SHUTDOWN:
2492                 return (linux_shutdown(td, arg));
2493         case LINUX_SETSOCKOPT:
2494                 return (linux_setsockopt(td, arg));
2495         case LINUX_GETSOCKOPT:
2496                 return (linux_getsockopt(td, arg));
2497         case LINUX_SENDMSG:
2498                 return (linux_sendmsg(td, arg));
2499         case LINUX_RECVMSG:
2500                 return (linux_recvmsg(td, arg));
2501         case LINUX_ACCEPT4:
2502                 return (linux_accept4(td, arg));
2503         case LINUX_RECVMMSG:
2504                 return (linux_recvmmsg(td, arg));
2505         case LINUX_SENDMMSG:
2506                 return (linux_sendmmsg(td, arg));
2507         case LINUX_SENDFILE:
2508                 return (linux_sendfile(td, arg));
2509         }
2510
2511         linux_msg(td, "socket type %d not implemented", args->what);
2512         return (ENOSYS);
2513 }
2514 #endif /* __i386__ || __arm__ || (__amd64__ && COMPAT_LINUX32) */