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