xref: /freebsd/sys/compat/linux/linux_socket.c (revision 643ac419fafba89f5adda0e0ea75b538727453fb)
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 	u_int fflag;
974 	int error;
975 
976 	error = linux_to_bsd_sockaddr(PTRIN(args->name), &sa,
977 	    &args->namelen);
978 	if (error != 0)
979 		return (error);
980 
981 	error = kern_connectat(td, AT_FDCWD, args->s, sa);
982 	free(sa, M_SONAME);
983 	if (error != EISCONN)
984 		return (error);
985 
986 	/*
987 	 * Linux doesn't return EISCONN the first time it occurs,
988 	 * when on a non-blocking socket. Instead it returns the
989 	 * error getsockopt(SOL_SOCKET, SO_ERROR) would return on BSD.
990 	 */
991 	error = getsock_cap(td, args->s, &cap_connect_rights,
992 	    &fp, &fflag, NULL);
993 	if (error != 0)
994 		return (error);
995 
996 	error = EISCONN;
997 	so = fp->f_data;
998 	if (fflag & FNONBLOCK) {
999 		SOCK_LOCK(so);
1000 		if (so->so_emuldata == 0)
1001 			error = so->so_error;
1002 		so->so_emuldata = (void *)1;
1003 		SOCK_UNLOCK(so);
1004 	}
1005 	fdrop(fp, td);
1006 
1007 	return (error);
1008 }
1009 
1010 int
1011 linux_listen(struct thread *td, struct linux_listen_args *args)
1012 {
1013 
1014 	return (kern_listen(td, args->s, args->backlog));
1015 }
1016 
1017 static int
1018 linux_accept_common(struct thread *td, int s, l_uintptr_t addr,
1019     l_uintptr_t namelen, int flags)
1020 {
1021 	struct sockaddr *sa;
1022 	struct file *fp, *fp1;
1023 	int bflags, len;
1024 	struct socket *so;
1025 	int error, error1;
1026 
1027 	bflags = 0;
1028 	fp = NULL;
1029 	sa = NULL;
1030 
1031 	error = linux_set_socket_flags(flags, &bflags);
1032 	if (error != 0)
1033 		return (error);
1034 
1035 	if (PTRIN(addr) == NULL) {
1036 		len = 0;
1037 		error = kern_accept4(td, s, NULL, NULL, bflags, NULL);
1038 	} else {
1039 		error = copyin(PTRIN(namelen), &len, sizeof(len));
1040 		if (error != 0)
1041 			return (error);
1042 		if (len < 0)
1043 			return (EINVAL);
1044 		error = kern_accept4(td, s, &sa, &len, bflags, &fp);
1045 	}
1046 
1047 	/*
1048 	 * Translate errno values into ones used by Linux.
1049 	 */
1050 	if (error != 0) {
1051 		/*
1052 		 * XXX. This is wrong, different sockaddr structures
1053 		 * have different sizes.
1054 		 */
1055 		switch (error) {
1056 		case EFAULT:
1057 			if (namelen != sizeof(struct sockaddr_in))
1058 				error = EINVAL;
1059 			break;
1060 		case EINVAL:
1061 			error1 = getsock_cap(td, s, &cap_accept_rights, &fp1, NULL, NULL);
1062 			if (error1 != 0) {
1063 				error = error1;
1064 				break;
1065 			}
1066 			so = fp1->f_data;
1067 			if (so->so_type == SOCK_DGRAM)
1068 				error = EOPNOTSUPP;
1069 			fdrop(fp1, td);
1070 			break;
1071 		}
1072 		return (error);
1073 	}
1074 
1075 	if (len != 0) {
1076 		error = linux_copyout_sockaddr(sa, PTRIN(addr), len);
1077 		if (error == 0)
1078 			error = copyout(&len, PTRIN(namelen),
1079 			    sizeof(len));
1080 		if (error != 0) {
1081 			fdclose(td, fp, td->td_retval[0]);
1082 			td->td_retval[0] = 0;
1083 		}
1084 	}
1085 	if (fp != NULL)
1086 		fdrop(fp, td);
1087 	free(sa, M_SONAME);
1088 	return (error);
1089 }
1090 
1091 int
1092 linux_accept(struct thread *td, struct linux_accept_args *args)
1093 {
1094 
1095 	return (linux_accept_common(td, args->s, args->addr,
1096 	    args->namelen, 0));
1097 }
1098 
1099 int
1100 linux_accept4(struct thread *td, struct linux_accept4_args *args)
1101 {
1102 
1103 	return (linux_accept_common(td, args->s, args->addr,
1104 	    args->namelen, args->flags));
1105 }
1106 
1107 int
1108 linux_getsockname(struct thread *td, struct linux_getsockname_args *args)
1109 {
1110 	struct sockaddr *sa;
1111 	int len, error;
1112 
1113 	error = copyin(PTRIN(args->namelen), &len, sizeof(len));
1114 	if (error != 0)
1115 		return (error);
1116 
1117 	error = kern_getsockname(td, args->s, &sa, &len);
1118 	if (error != 0)
1119 		return (error);
1120 
1121 	if (len != 0)
1122 		error = linux_copyout_sockaddr(sa, PTRIN(args->addr), len);
1123 
1124 	free(sa, M_SONAME);
1125 	if (error == 0)
1126 		error = copyout(&len, PTRIN(args->namelen), sizeof(len));
1127 	return (error);
1128 }
1129 
1130 int
1131 linux_getpeername(struct thread *td, struct linux_getpeername_args *args)
1132 {
1133 	struct sockaddr *sa;
1134 	int len, error;
1135 
1136 	error = copyin(PTRIN(args->namelen), &len, sizeof(len));
1137 	if (error != 0)
1138 		return (error);
1139 	if (len < 0)
1140 		return (EINVAL);
1141 
1142 	error = kern_getpeername(td, args->s, &sa, &len);
1143 	if (error != 0)
1144 		return (error);
1145 
1146 	if (len != 0)
1147 		error = linux_copyout_sockaddr(sa, PTRIN(args->addr), len);
1148 
1149 	free(sa, M_SONAME);
1150 	if (error == 0)
1151 		error = copyout(&len, PTRIN(args->namelen), sizeof(len));
1152 	return (error);
1153 }
1154 
1155 int
1156 linux_socketpair(struct thread *td, struct linux_socketpair_args *args)
1157 {
1158 	int domain, error, sv[2], type;
1159 
1160 	domain = linux_to_bsd_domain(args->domain);
1161 	if (domain != PF_LOCAL)
1162 		return (EAFNOSUPPORT);
1163 	type = args->type & LINUX_SOCK_TYPE_MASK;
1164 	if (type < 0 || type > LINUX_SOCK_MAX)
1165 		return (EINVAL);
1166 	error = linux_set_socket_flags(args->type & ~LINUX_SOCK_TYPE_MASK,
1167 	    &type);
1168 	if (error != 0)
1169 		return (error);
1170 	if (args->protocol != 0 && args->protocol != PF_UNIX) {
1171 		/*
1172 		 * Use of PF_UNIX as protocol argument is not right,
1173 		 * but Linux does it.
1174 		 * Do not map PF_UNIX as its Linux value is identical
1175 		 * to FreeBSD one.
1176 		 */
1177 		return (EPROTONOSUPPORT);
1178 	}
1179 	error = kern_socketpair(td, domain, type, 0, sv);
1180 	if (error != 0)
1181                 return (error);
1182         error = copyout(sv, PTRIN(args->rsv), 2 * sizeof(int));
1183         if (error != 0) {
1184                 (void)kern_close(td, sv[0]);
1185                 (void)kern_close(td, sv[1]);
1186         }
1187 	return (error);
1188 }
1189 
1190 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1191 struct linux_send_args {
1192 	register_t s;
1193 	register_t msg;
1194 	register_t len;
1195 	register_t flags;
1196 };
1197 
1198 static int
1199 linux_send(struct thread *td, struct linux_send_args *args)
1200 {
1201 	struct sendto_args /* {
1202 		int s;
1203 		caddr_t buf;
1204 		int len;
1205 		int flags;
1206 		caddr_t to;
1207 		int tolen;
1208 	} */ bsd_args;
1209 	struct file *fp;
1210 	int error, fflag;
1211 
1212 	bsd_args.s = args->s;
1213 	bsd_args.buf = (caddr_t)PTRIN(args->msg);
1214 	bsd_args.len = args->len;
1215 	bsd_args.flags = linux_to_bsd_msg_flags(args->flags);
1216 	bsd_args.to = NULL;
1217 	bsd_args.tolen = 0;
1218 	error = sys_sendto(td, &bsd_args);
1219 	if (error == ENOTCONN) {
1220 		/*
1221 		 * Linux doesn't return ENOTCONN for non-blocking sockets.
1222 		 * Instead it returns the EAGAIN.
1223 		 */
1224 		error = getsock_cap(td, args->s, &cap_send_rights, &fp,
1225 		    &fflag, NULL);
1226 		if (error == 0) {
1227 			if (fflag & FNONBLOCK)
1228 				error = EAGAIN;
1229 			fdrop(fp, td);
1230 		}
1231 	}
1232 	return (error);
1233 }
1234 
1235 struct linux_recv_args {
1236 	register_t s;
1237 	register_t msg;
1238 	register_t len;
1239 	register_t flags;
1240 };
1241 
1242 static int
1243 linux_recv(struct thread *td, struct linux_recv_args *args)
1244 {
1245 	struct recvfrom_args /* {
1246 		int s;
1247 		caddr_t buf;
1248 		int len;
1249 		int flags;
1250 		struct sockaddr *from;
1251 		socklen_t fromlenaddr;
1252 	} */ bsd_args;
1253 
1254 	bsd_args.s = args->s;
1255 	bsd_args.buf = (caddr_t)PTRIN(args->msg);
1256 	bsd_args.len = args->len;
1257 	bsd_args.flags = linux_to_bsd_msg_flags(args->flags);
1258 	bsd_args.from = NULL;
1259 	bsd_args.fromlenaddr = 0;
1260 	return (sys_recvfrom(td, &bsd_args));
1261 }
1262 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1263 
1264 int
1265 linux_sendto(struct thread *td, struct linux_sendto_args *args)
1266 {
1267 	struct msghdr msg;
1268 	struct iovec aiov;
1269 	struct socket *so;
1270 	struct file *fp;
1271 	int error;
1272 
1273 	if (linux_check_hdrincl(td, args->s) == 0)
1274 		/* IP_HDRINCL set, tweak the packet before sending */
1275 		return (linux_sendto_hdrincl(td, args));
1276 
1277 	bzero(&msg, sizeof(msg));
1278 	error = getsock_cap(td, args->s, &cap_send_connect_rights,
1279 	    &fp, NULL, NULL);
1280 	if (error != 0)
1281 		return (error);
1282 	so = fp->f_data;
1283 	if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0) {
1284 		msg.msg_name = PTRIN(args->to);
1285 		msg.msg_namelen = args->tolen;
1286 	}
1287 	msg.msg_iov = &aiov;
1288 	msg.msg_iovlen = 1;
1289 	aiov.iov_base = PTRIN(args->msg);
1290 	aiov.iov_len = args->len;
1291 	fdrop(fp, td);
1292 	return (linux_sendit(td, args->s, &msg, args->flags, NULL,
1293 	    UIO_USERSPACE));
1294 }
1295 
1296 int
1297 linux_recvfrom(struct thread *td, struct linux_recvfrom_args *args)
1298 {
1299 	struct sockaddr *sa;
1300 	struct msghdr msg;
1301 	struct iovec aiov;
1302 	int error, fromlen;
1303 
1304 	if (PTRIN(args->fromlen) != NULL) {
1305 		error = copyin(PTRIN(args->fromlen), &fromlen,
1306 		    sizeof(fromlen));
1307 		if (error != 0)
1308 			return (error);
1309 		if (fromlen < 0)
1310 			return (EINVAL);
1311 		fromlen = min(fromlen, SOCK_MAXADDRLEN);
1312 		sa = malloc(fromlen, M_SONAME, M_WAITOK);
1313 	} else {
1314 		fromlen = 0;
1315 		sa = NULL;
1316 	}
1317 
1318 	msg.msg_name = sa;
1319 	msg.msg_namelen = fromlen;
1320 	msg.msg_iov = &aiov;
1321 	msg.msg_iovlen = 1;
1322 	aiov.iov_base = PTRIN(args->buf);
1323 	aiov.iov_len = args->len;
1324 	msg.msg_control = 0;
1325 	msg.msg_flags = linux_to_bsd_msg_flags(args->flags);
1326 
1327 	error = kern_recvit(td, args->s, &msg, UIO_SYSSPACE, NULL);
1328 	if (error != 0)
1329 		goto out;
1330 
1331 	/*
1332 	 * XXX. Seems that FreeBSD is different from Linux here. Linux
1333 	 * fill source address if underlying protocol provides it, while
1334 	 * FreeBSD fill it if underlying protocol is not connection-oriented.
1335 	 * So, kern_recvit() set msg.msg_namelen to 0 if protocol pr_flags
1336 	 * does not contains PR_ADDR flag.
1337 	 */
1338 	if (PTRIN(args->from) != NULL && msg.msg_namelen != 0)
1339 		error = linux_copyout_sockaddr(sa, PTRIN(args->from),
1340 		    msg.msg_namelen);
1341 
1342 	if (error == 0 && PTRIN(args->fromlen) != NULL)
1343 		error = copyout(&msg.msg_namelen, PTRIN(args->fromlen),
1344 		    sizeof(msg.msg_namelen));
1345 out:
1346 	free(sa, M_SONAME);
1347 	return (error);
1348 }
1349 
1350 static int
1351 linux_sendmsg_common(struct thread *td, l_int s, struct l_msghdr *msghdr,
1352     l_uint flags)
1353 {
1354 	struct cmsghdr *cmsg;
1355 	struct mbuf *control;
1356 	struct msghdr msg;
1357 	struct l_cmsghdr linux_cmsg;
1358 	struct l_cmsghdr *ptr_cmsg;
1359 	struct l_msghdr linux_msghdr;
1360 	struct iovec *iov;
1361 	socklen_t datalen;
1362 	struct sockaddr *sa;
1363 	struct socket *so;
1364 	sa_family_t sa_family;
1365 	struct file *fp;
1366 	void *data;
1367 	l_size_t len;
1368 	l_size_t clen;
1369 	int error, fflag;
1370 
1371 	error = copyin(msghdr, &linux_msghdr, sizeof(linux_msghdr));
1372 	if (error != 0)
1373 		return (error);
1374 
1375 	/*
1376 	 * Some Linux applications (ping) define a non-NULL control data
1377 	 * pointer, but a msg_controllen of 0, which is not allowed in the
1378 	 * FreeBSD system call interface.  NULL the msg_control pointer in
1379 	 * order to handle this case.  This should be checked, but allows the
1380 	 * Linux ping to work.
1381 	 */
1382 	if (PTRIN(linux_msghdr.msg_control) != NULL &&
1383 	    linux_msghdr.msg_controllen == 0)
1384 		linux_msghdr.msg_control = PTROUT(NULL);
1385 
1386 	error = linux_to_bsd_msghdr(&msg, &linux_msghdr);
1387 	if (error != 0)
1388 		return (error);
1389 
1390 #ifdef COMPAT_LINUX32
1391 	error = linux32_copyiniov(PTRIN(msg.msg_iov), msg.msg_iovlen,
1392 	    &iov, EMSGSIZE);
1393 #else
1394 	error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
1395 #endif
1396 	if (error != 0)
1397 		return (error);
1398 
1399 	control = NULL;
1400 
1401 	error = kern_getsockname(td, s, &sa, &datalen);
1402 	if (error != 0)
1403 		goto bad;
1404 	sa_family = sa->sa_family;
1405 	free(sa, M_SONAME);
1406 
1407 	if (flags & LINUX_MSG_OOB) {
1408 		error = EOPNOTSUPP;
1409 		if (sa_family == AF_UNIX)
1410 			goto bad;
1411 
1412 		error = getsock_cap(td, s, &cap_send_rights, &fp,
1413 		    &fflag, NULL);
1414 		if (error != 0)
1415 			goto bad;
1416 		so = fp->f_data;
1417 		if (so->so_type != SOCK_STREAM)
1418 			error = EOPNOTSUPP;
1419 		fdrop(fp, td);
1420 		if (error != 0)
1421 			goto bad;
1422 	}
1423 
1424 	if (linux_msghdr.msg_controllen >= sizeof(struct l_cmsghdr)) {
1425 		error = ENOBUFS;
1426 		control = m_get(M_WAITOK, MT_CONTROL);
1427 		MCLGET(control, M_WAITOK);
1428 		data = mtod(control, void *);
1429 		datalen = 0;
1430 
1431 		ptr_cmsg = PTRIN(linux_msghdr.msg_control);
1432 		clen = linux_msghdr.msg_controllen;
1433 		do {
1434 			error = copyin(ptr_cmsg, &linux_cmsg,
1435 			    sizeof(struct l_cmsghdr));
1436 			if (error != 0)
1437 				goto bad;
1438 
1439 			error = EINVAL;
1440 			if (linux_cmsg.cmsg_len < sizeof(struct l_cmsghdr) ||
1441 			    linux_cmsg.cmsg_len > clen)
1442 				goto bad;
1443 
1444 			if (datalen + CMSG_HDRSZ > MCLBYTES)
1445 				goto bad;
1446 
1447 			/*
1448 			 * Now we support only SCM_RIGHTS and SCM_CRED,
1449 			 * so return EINVAL in any other cmsg_type
1450 			 */
1451 			cmsg = data;
1452 			cmsg->cmsg_type =
1453 			    linux_to_bsd_cmsg_type(linux_cmsg.cmsg_type);
1454 			cmsg->cmsg_level =
1455 			    linux_to_bsd_sockopt_level(linux_cmsg.cmsg_level);
1456 			if (cmsg->cmsg_type == -1
1457 			    || cmsg->cmsg_level != SOL_SOCKET) {
1458 				linux_msg(curthread,
1459 				    "unsupported sendmsg cmsg level %d type %d",
1460 				    linux_cmsg.cmsg_level, linux_cmsg.cmsg_type);
1461 				goto bad;
1462 			}
1463 
1464 			/*
1465 			 * Some applications (e.g. pulseaudio) attempt to
1466 			 * send ancillary data even if the underlying protocol
1467 			 * doesn't support it which is not allowed in the
1468 			 * FreeBSD system call interface.
1469 			 */
1470 			if (sa_family != AF_UNIX)
1471 				goto next;
1472 
1473 			if (cmsg->cmsg_type == SCM_CREDS) {
1474 				len = sizeof(struct cmsgcred);
1475 				if (datalen + CMSG_SPACE(len) > MCLBYTES)
1476 					goto bad;
1477 
1478 				/*
1479 				 * The lower levels will fill in the structure
1480 				 */
1481 				memset(CMSG_DATA(data), 0, len);
1482 			} else {
1483 				len = linux_cmsg.cmsg_len - L_CMSG_HDRSZ;
1484 				if (datalen + CMSG_SPACE(len) < datalen ||
1485 				    datalen + CMSG_SPACE(len) > MCLBYTES)
1486 					goto bad;
1487 
1488 				error = copyin(LINUX_CMSG_DATA(ptr_cmsg),
1489 				    CMSG_DATA(data), len);
1490 				if (error != 0)
1491 					goto bad;
1492 			}
1493 
1494 			cmsg->cmsg_len = CMSG_LEN(len);
1495 			data = (char *)data + CMSG_SPACE(len);
1496 			datalen += CMSG_SPACE(len);
1497 
1498 next:
1499 			if (clen <= LINUX_CMSG_ALIGN(linux_cmsg.cmsg_len))
1500 				break;
1501 
1502 			clen -= LINUX_CMSG_ALIGN(linux_cmsg.cmsg_len);
1503 			ptr_cmsg = (struct l_cmsghdr *)((char *)ptr_cmsg +
1504 			    LINUX_CMSG_ALIGN(linux_cmsg.cmsg_len));
1505 		} while(clen >= sizeof(struct l_cmsghdr));
1506 
1507 		control->m_len = datalen;
1508 		if (datalen == 0) {
1509 			m_freem(control);
1510 			control = NULL;
1511 		}
1512 	}
1513 
1514 	msg.msg_iov = iov;
1515 	msg.msg_flags = 0;
1516 	error = linux_sendit(td, s, &msg, flags, control, UIO_USERSPACE);
1517 	control = NULL;
1518 
1519 bad:
1520 	m_freem(control);
1521 	free(iov, M_IOV);
1522 	return (error);
1523 }
1524 
1525 int
1526 linux_sendmsg(struct thread *td, struct linux_sendmsg_args *args)
1527 {
1528 
1529 	return (linux_sendmsg_common(td, args->s, PTRIN(args->msg),
1530 	    args->flags));
1531 }
1532 
1533 int
1534 linux_sendmmsg(struct thread *td, struct linux_sendmmsg_args *args)
1535 {
1536 	struct l_mmsghdr *msg;
1537 	l_uint retval;
1538 	int error, datagrams;
1539 
1540 	if (args->vlen > UIO_MAXIOV)
1541 		args->vlen = UIO_MAXIOV;
1542 
1543 	msg = PTRIN(args->msg);
1544 	datagrams = 0;
1545 	while (datagrams < args->vlen) {
1546 		error = linux_sendmsg_common(td, args->s, &msg->msg_hdr,
1547 		    args->flags);
1548 		if (error != 0)
1549 			break;
1550 
1551 		retval = td->td_retval[0];
1552 		error = copyout(&retval, &msg->msg_len, sizeof(msg->msg_len));
1553 		if (error != 0)
1554 			break;
1555 		++msg;
1556 		++datagrams;
1557 	}
1558 	if (error == 0)
1559 		td->td_retval[0] = datagrams;
1560 	return (error);
1561 }
1562 
1563 static int
1564 recvmsg_scm_rights(struct thread *td, l_uint flags, socklen_t *datalen,
1565     void **data, void **udata)
1566 {
1567 	int i, fd, fds, *fdp;
1568 
1569 	if (flags & LINUX_MSG_CMSG_CLOEXEC) {
1570 		fds = *datalen / sizeof(int);
1571 		fdp = *data;
1572 		for (i = 0; i < fds; i++) {
1573 			fd = *fdp++;
1574 			(void)kern_fcntl(td, fd, F_SETFD, FD_CLOEXEC);
1575 		}
1576 	}
1577 	return (0);
1578 }
1579 
1580 
1581 static int
1582 recvmsg_scm_creds(socklen_t *datalen, void **data, void **udata)
1583 {
1584 	struct cmsgcred *cmcred;
1585 	struct l_ucred lu;
1586 
1587 	cmcred = *data;
1588 	lu.pid = cmcred->cmcred_pid;
1589 	lu.uid = cmcred->cmcred_uid;
1590 	lu.gid = cmcred->cmcred_gid;
1591 	memmove(*data, &lu, sizeof(lu));
1592 	*datalen = sizeof(lu);
1593 	return (0);
1594 }
1595 _Static_assert(sizeof(struct cmsgcred) >= sizeof(struct l_ucred),
1596     "scm_creds sizeof l_ucred");
1597 
1598 static int
1599 recvmsg_scm_creds2(socklen_t *datalen, void **data, void **udata)
1600 {
1601 	struct sockcred2 *scred;
1602 	struct l_ucred lu;
1603 
1604 	scred = *data;
1605 	lu.pid = scred->sc_pid;
1606 	lu.uid = scred->sc_uid;
1607 	lu.gid = scred->sc_gid;
1608 	memmove(*data, &lu, sizeof(lu));
1609 	*datalen = sizeof(lu);
1610 	return (0);
1611 }
1612 _Static_assert(sizeof(struct sockcred2) >= sizeof(struct l_ucred),
1613     "scm_creds2 sizeof l_ucred");
1614 
1615 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1616 static int
1617 recvmsg_scm_timestamp(l_int msg_type, socklen_t *datalen, void **data,
1618     void **udata)
1619 {
1620 	l_sock_timeval ltv64;
1621 	l_timeval ltv;
1622 	struct timeval *tv;
1623 	socklen_t len;
1624 	void *buf;
1625 
1626 	if (*datalen != sizeof(struct timeval))
1627 		return (EMSGSIZE);
1628 
1629 	tv = *data;
1630 #if defined(COMPAT_LINUX32)
1631 	if (msg_type == LINUX_SCM_TIMESTAMPO &&
1632 	    (tv->tv_sec > INT_MAX || tv->tv_sec < INT_MIN))
1633 		return (EOVERFLOW);
1634 #endif
1635 	if (msg_type == LINUX_SCM_TIMESTAMPN)
1636 		len = sizeof(ltv64);
1637 	else
1638 		len = sizeof(ltv);
1639 
1640 	buf = malloc(len, M_LINUX, M_WAITOK);
1641 	if (msg_type == LINUX_SCM_TIMESTAMPN) {
1642 		ltv64.tv_sec = tv->tv_sec;
1643 		ltv64.tv_usec = tv->tv_usec;
1644 		memmove(buf, &ltv64, len);
1645 	} else {
1646 		ltv.tv_sec = tv->tv_sec;
1647 		ltv.tv_usec = tv->tv_usec;
1648 		memmove(buf, &ltv, len);
1649 	}
1650 	*data = *udata = buf;
1651 	*datalen = len;
1652 	return (0);
1653 }
1654 #else
1655 _Static_assert(sizeof(struct timeval) == sizeof(l_timeval),
1656     "scm_timestamp sizeof l_timeval");
1657 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1658 
1659 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1660 static int
1661 recvmsg_scm_timestampns(l_int msg_type, socklen_t *datalen, void **data,
1662     void **udata)
1663 {
1664 	struct l_timespec64 ts64;
1665 	struct l_timespec ts32;
1666 	struct timespec ts;
1667 	socklen_t len;
1668 	void *buf;
1669 
1670 	if (msg_type == LINUX_SCM_TIMESTAMPNSO)
1671 		len = sizeof(ts32);
1672 	else
1673 		len = sizeof(ts64);
1674 
1675 	buf = malloc(len, M_LINUX, M_WAITOK);
1676 	bintime2timespec(*data, &ts);
1677 	if (msg_type == LINUX_SCM_TIMESTAMPNSO) {
1678 		ts32.tv_sec = ts.tv_sec;
1679 		ts32.tv_nsec = ts.tv_nsec;
1680 		memmove(buf, &ts32, len);
1681 	} else {
1682 		ts64.tv_sec = ts.tv_sec;
1683 		ts64.tv_nsec = ts.tv_nsec;
1684 		memmove(buf, &ts64, len);
1685 	}
1686 	*data = *udata = buf;
1687 	*datalen = len;
1688 	return (0);
1689 }
1690 #else
1691 static int
1692 recvmsg_scm_timestampns(l_int msg_type, socklen_t *datalen, void **data,
1693     void **udata)
1694 {
1695 	struct timespec ts;
1696 
1697 	bintime2timespec(*data, &ts);
1698 	memmove(*data, &ts, sizeof(struct timespec));
1699 	*datalen = sizeof(struct timespec);
1700 	return (0);
1701 }
1702 _Static_assert(sizeof(struct bintime) >= sizeof(struct timespec),
1703     "scm_timestampns sizeof timespec");
1704 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1705 
1706 static int
1707 recvmsg_scm_ip_origdstaddr(socklen_t *datalen, void **data, void **udata)
1708 {
1709 	struct l_sockaddr *lsa;
1710 	int error;
1711 
1712 	error = bsd_to_linux_sockaddr(*data, &lsa, *datalen);
1713 	if (error == 0) {
1714 		*data = *udata = lsa;
1715 		*datalen = sizeof(*lsa);
1716 	}
1717 	return (error);
1718 }
1719 
1720 static int
1721 linux_recvmsg_common(struct thread *td, l_int s, struct l_msghdr *msghdr,
1722     l_uint flags, struct msghdr *msg)
1723 {
1724 	struct proc *p = td->td_proc;
1725 	struct cmsghdr *cm;
1726 	struct l_cmsghdr *lcm = NULL;
1727 	socklen_t datalen, maxlen, outlen;
1728 	struct l_msghdr l_msghdr;
1729 	struct iovec *iov, *uiov;
1730 	struct mbuf *m, *control = NULL;
1731 	struct mbuf **controlp;
1732 	struct sockaddr *sa;
1733 	caddr_t outbuf;
1734 	void *data, *udata;
1735 	int error;
1736 
1737 	error = copyin(msghdr, &l_msghdr, sizeof(l_msghdr));
1738 	if (error != 0)
1739 		return (error);
1740 
1741 	/*
1742 	 * Pass user-supplied recvmsg() flags in msg_flags field,
1743 	 * following sys_recvmsg() convention.
1744 	*/
1745 	l_msghdr.msg_flags = flags;
1746 
1747 	error = linux_to_bsd_msghdr(msg, &l_msghdr);
1748 	if (error != 0)
1749 		return (error);
1750 
1751 #ifdef COMPAT_LINUX32
1752 	error = linux32_copyiniov(PTRIN(msg->msg_iov), msg->msg_iovlen,
1753 	    &iov, EMSGSIZE);
1754 #else
1755 	error = copyiniov(msg->msg_iov, msg->msg_iovlen, &iov, EMSGSIZE);
1756 #endif
1757 	if (error != 0)
1758 		return (error);
1759 
1760 	if (msg->msg_name != NULL && msg->msg_namelen > 0) {
1761 		msg->msg_namelen = min(msg->msg_namelen, SOCK_MAXADDRLEN);
1762 		sa = malloc(msg->msg_namelen, M_SONAME, M_WAITOK);
1763 		msg->msg_name = sa;
1764 	} else {
1765 		sa = NULL;
1766 		msg->msg_name = NULL;
1767 	}
1768 
1769 	uiov = msg->msg_iov;
1770 	msg->msg_iov = iov;
1771 	controlp = (msg->msg_control != NULL) ? &control : NULL;
1772 	error = kern_recvit(td, s, msg, UIO_SYSSPACE, controlp);
1773 	msg->msg_iov = uiov;
1774 	if (error != 0)
1775 		goto bad;
1776 
1777 	/*
1778 	 * Note that kern_recvit() updates msg->msg_namelen.
1779 	 */
1780 	if (msg->msg_name != NULL && msg->msg_namelen > 0) {
1781 		msg->msg_name = PTRIN(l_msghdr.msg_name);
1782 		error = linux_copyout_sockaddr(sa, msg->msg_name,
1783 		    msg->msg_namelen);
1784 		if (error != 0)
1785 			goto bad;
1786 	}
1787 
1788 	error = bsd_to_linux_msghdr(msg, &l_msghdr);
1789 	if (error != 0)
1790 		goto bad;
1791 
1792 	maxlen = l_msghdr.msg_controllen;
1793 	l_msghdr.msg_controllen = 0;
1794 	if (control == NULL)
1795 		goto out;
1796 
1797 	lcm = malloc(L_CMSG_HDRSZ, M_LINUX, M_WAITOK | M_ZERO);
1798 	msg->msg_control = mtod(control, struct cmsghdr *);
1799 	msg->msg_controllen = control->m_len;
1800 	outbuf = PTRIN(l_msghdr.msg_control);
1801 	outlen = 0;
1802 	for (m = control; m != NULL; m = m->m_next) {
1803 		cm = mtod(m, struct cmsghdr *);
1804 		lcm->cmsg_type = bsd_to_linux_cmsg_type(p, cm->cmsg_type,
1805 		    cm->cmsg_level);
1806 		lcm->cmsg_level = bsd_to_linux_sockopt_level(cm->cmsg_level);
1807 
1808 		data = CMSG_DATA(cm);
1809 		datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
1810 		udata = NULL;
1811 		error = 0;
1812 
1813 		/* Process non SOL_SOCKET types. */
1814 		if (cm->cmsg_level == IPPROTO_IP &&
1815 		    lcm->cmsg_type == LINUX_IP_ORIGDSTADDR) {
1816 			error = recvmsg_scm_ip_origdstaddr(&datalen, &data, &udata);
1817 			goto cont;
1818 		}
1819 
1820 		if (lcm->cmsg_type == -1 ||
1821 		    cm->cmsg_level != SOL_SOCKET) {
1822 			LINUX_RATELIMIT_MSG_OPT2(
1823 			    "unsupported recvmsg cmsg level %d type %d",
1824 			    cm->cmsg_level, cm->cmsg_type);
1825 			error = EINVAL;
1826 			goto bad;
1827 		}
1828 
1829 
1830 		switch (cm->cmsg_type) {
1831 		case SCM_RIGHTS:
1832 			error = recvmsg_scm_rights(td, flags,
1833 			    &datalen, &data, &udata);
1834 			break;
1835 		case SCM_CREDS:
1836 			error = recvmsg_scm_creds(&datalen,
1837 			    &data, &udata);
1838 			break;
1839 		case SCM_CREDS2:
1840 			error = recvmsg_scm_creds2(&datalen,
1841 			    &data, &udata);
1842 			break;
1843 		case SCM_TIMESTAMP:
1844 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1845 			error = recvmsg_scm_timestamp(lcm->cmsg_type,
1846 			    &datalen, &data, &udata);
1847 #endif
1848 			break;
1849 		case SCM_BINTIME:
1850 			error = recvmsg_scm_timestampns(lcm->cmsg_type,
1851 			    &datalen, &data, &udata);
1852 			break;
1853 		}
1854 
1855 cont:
1856 		if (error != 0)
1857 			goto bad;
1858 
1859 		if (outlen + LINUX_CMSG_LEN(datalen) > maxlen) {
1860 			if (outlen == 0) {
1861 				error = EMSGSIZE;
1862 				goto err;
1863 			} else {
1864 				l_msghdr.msg_flags |= LINUX_MSG_CTRUNC;
1865 				m_dispose_extcontrolm(control);
1866 				free(udata, M_LINUX);
1867 				goto out;
1868 			}
1869 		}
1870 
1871 		lcm->cmsg_len = LINUX_CMSG_LEN(datalen);
1872 		error = copyout(lcm, outbuf, L_CMSG_HDRSZ);
1873 		if (error == 0) {
1874 			outbuf += L_CMSG_HDRSZ;
1875 			error = copyout(data, outbuf, datalen);
1876 			if (error == 0) {
1877 				outbuf += LINUX_CMSG_ALIGN(datalen);
1878 				outlen += LINUX_CMSG_LEN(datalen);
1879 			}
1880 		}
1881 err:
1882 		free(udata, M_LINUX);
1883 		if (error != 0)
1884 			goto bad;
1885 	}
1886 	l_msghdr.msg_controllen = outlen;
1887 
1888 out:
1889 	error = copyout(&l_msghdr, msghdr, sizeof(l_msghdr));
1890 
1891 bad:
1892 	if (control != NULL) {
1893 		if (error != 0)
1894 			m_dispose_extcontrolm(control);
1895 		m_freem(control);
1896 	}
1897 	free(iov, M_IOV);
1898 	free(lcm, M_LINUX);
1899 	free(sa, M_SONAME);
1900 
1901 	return (error);
1902 }
1903 
1904 int
1905 linux_recvmsg(struct thread *td, struct linux_recvmsg_args *args)
1906 {
1907 	struct msghdr bsd_msg;
1908 	struct file *fp;
1909 	int error;
1910 
1911 	error = getsock_cap(td, args->s, &cap_recv_rights,
1912 	    &fp, NULL, NULL);
1913 	if (error != 0)
1914 		return (error);
1915 	fdrop(fp, td);
1916 	return (linux_recvmsg_common(td, args->s, PTRIN(args->msg),
1917 	    args->flags, &bsd_msg));
1918 }
1919 
1920 static int
1921 linux_recvmmsg_common(struct thread *td, l_int s, struct l_mmsghdr *msg,
1922     l_uint vlen, l_uint flags, struct timespec *tts)
1923 {
1924 	struct msghdr bsd_msg;
1925 	struct timespec ts;
1926 	struct file *fp;
1927 	l_uint retval;
1928 	int error, datagrams;
1929 
1930 	error = getsock_cap(td, s, &cap_recv_rights,
1931 	    &fp, NULL, NULL);
1932 	if (error != 0)
1933 		return (error);
1934 	datagrams = 0;
1935 	while (datagrams < vlen) {
1936 		error = linux_recvmsg_common(td, s, &msg->msg_hdr,
1937 		    flags & ~LINUX_MSG_WAITFORONE, &bsd_msg);
1938 		if (error != 0)
1939 			break;
1940 
1941 		retval = td->td_retval[0];
1942 		error = copyout(&retval, &msg->msg_len, sizeof(msg->msg_len));
1943 		if (error != 0)
1944 			break;
1945 		++msg;
1946 		++datagrams;
1947 
1948 		/*
1949 		 * MSG_WAITFORONE turns on MSG_DONTWAIT after one packet.
1950 		 */
1951 		if (flags & LINUX_MSG_WAITFORONE)
1952 			flags |= LINUX_MSG_DONTWAIT;
1953 
1954 		/*
1955 		 * See BUGS section of recvmmsg(2).
1956 		 */
1957 		if (tts) {
1958 			getnanotime(&ts);
1959 			timespecsub(&ts, tts, &ts);
1960 			if (!timespecisset(&ts) || ts.tv_sec > 0)
1961 				break;
1962 		}
1963 		/* Out of band data, return right away. */
1964 		if (bsd_msg.msg_flags & MSG_OOB)
1965 			break;
1966 	}
1967 	if (error == 0)
1968 		td->td_retval[0] = datagrams;
1969 	fdrop(fp, td);
1970 	return (error);
1971 }
1972 
1973 int
1974 linux_recvmmsg(struct thread *td, struct linux_recvmmsg_args *args)
1975 {
1976 	struct timespec ts, tts, *ptts;
1977 	int error;
1978 
1979 	if (args->timeout) {
1980 		error = linux_get_timespec(&ts, args->timeout);
1981 		if (error != 0)
1982 			return (error);
1983 		getnanotime(&tts);
1984 		timespecadd(&tts, &ts, &tts);
1985 		ptts = &tts;
1986 	}
1987 		else ptts = NULL;
1988 
1989 	return (linux_recvmmsg_common(td, args->s, PTRIN(args->msg),
1990 	    args->vlen, args->flags, ptts));
1991 }
1992 
1993 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1994 int
1995 linux_recvmmsg_time64(struct thread *td, struct linux_recvmmsg_time64_args *args)
1996 {
1997 	struct timespec ts, tts, *ptts;
1998 	int error;
1999 
2000 	if (args->timeout) {
2001 		error = linux_get_timespec64(&ts, args->timeout);
2002 		if (error != 0)
2003 			return (error);
2004 		getnanotime(&tts);
2005 		timespecadd(&tts, &ts, &tts);
2006 		ptts = &tts;
2007 	}
2008 		else ptts = NULL;
2009 
2010 	return (linux_recvmmsg_common(td, args->s, PTRIN(args->msg),
2011 	    args->vlen, args->flags, ptts));
2012 }
2013 #endif
2014 
2015 int
2016 linux_shutdown(struct thread *td, struct linux_shutdown_args *args)
2017 {
2018 
2019 	return (kern_shutdown(td, args->s, args->how));
2020 }
2021 
2022 int
2023 linux_setsockopt(struct thread *td, struct linux_setsockopt_args *args)
2024 {
2025 	struct proc *p = td->td_proc;
2026 	struct linux_pemuldata *pem;
2027 	l_timeval linux_tv;
2028 	struct sockaddr *sa;
2029 	struct timeval tv;
2030 	socklen_t len;
2031 	int error, level, name, val;
2032 
2033 	level = linux_to_bsd_sockopt_level(args->level);
2034 	switch (level) {
2035 	case SOL_SOCKET:
2036 		name = linux_to_bsd_so_sockopt(args->optname);
2037 		switch (name) {
2038 		case LOCAL_CREDS_PERSISTENT:
2039 			level = SOL_LOCAL;
2040 			break;
2041 		case SO_RCVTIMEO:
2042 			/* FALLTHROUGH */
2043 		case SO_SNDTIMEO:
2044 			error = copyin(PTRIN(args->optval), &linux_tv,
2045 			    sizeof(linux_tv));
2046 			if (error != 0)
2047 				return (error);
2048 			tv.tv_sec = linux_tv.tv_sec;
2049 			tv.tv_usec = linux_tv.tv_usec;
2050 			return (kern_setsockopt(td, args->s, level,
2051 			    name, &tv, UIO_SYSSPACE, sizeof(tv)));
2052 			/* NOTREACHED */
2053 		case SO_TIMESTAMP:
2054 			/* overwrite SO_BINTIME */
2055 			val = 0;
2056 			error = kern_setsockopt(td, args->s, level,
2057 			    SO_BINTIME, &val, UIO_SYSSPACE, sizeof(val));
2058 			if (error != 0)
2059 				return (error);
2060 			pem = pem_find(p);
2061 			pem->so_timestamp = args->optname;
2062 			break;
2063 		case SO_BINTIME:
2064 			/* overwrite SO_TIMESTAMP */
2065 			val = 0;
2066 			error = kern_setsockopt(td, args->s, level,
2067 			    SO_TIMESTAMP, &val, UIO_SYSSPACE, sizeof(val));
2068 			if (error != 0)
2069 				return (error);
2070 			pem = pem_find(p);
2071 			pem->so_timestampns = args->optname;
2072 			break;
2073 		default:
2074 			break;
2075 		}
2076 		break;
2077 	case IPPROTO_IP:
2078 		if (args->optname == LINUX_IP_RECVERR &&
2079 		    linux_ignore_ip_recverr) {
2080 			/*
2081 			 * XXX: This is a hack to unbreak DNS resolution
2082 			 *	with glibc 2.30 and above.
2083 			 */
2084 			return (0);
2085 		}
2086 		name = linux_to_bsd_ip_sockopt(args->optname);
2087 		break;
2088 	case IPPROTO_IPV6:
2089 		name = linux_to_bsd_ip6_sockopt(args->optname);
2090 		break;
2091 	case IPPROTO_TCP:
2092 		name = linux_to_bsd_tcp_sockopt(args->optname);
2093 		break;
2094 	default:
2095 		name = -1;
2096 		break;
2097 	}
2098 	if (name < 0) {
2099 		if (name == -1)
2100 			linux_msg(curthread,
2101 			    "unsupported setsockopt level %d optname %d",
2102 			    args->level, args->optname);
2103 		return (ENOPROTOOPT);
2104 	}
2105 
2106 	if (name == IPV6_NEXTHOP) {
2107 		len = args->optlen;
2108 		error = linux_to_bsd_sockaddr(PTRIN(args->optval), &sa, &len);
2109 		if (error != 0)
2110 			return (error);
2111 
2112 		error = kern_setsockopt(td, args->s, level,
2113 		    name, sa, UIO_SYSSPACE, len);
2114 		free(sa, M_SONAME);
2115 	} else {
2116 		error = kern_setsockopt(td, args->s, level,
2117 		    name, PTRIN(args->optval), UIO_USERSPACE, args->optlen);
2118 	}
2119 
2120 	return (error);
2121 }
2122 
2123 static int
2124 linux_sockopt_copyout(struct thread *td, void *val, socklen_t len,
2125     struct linux_getsockopt_args *args)
2126 {
2127 	int error;
2128 
2129 	error = copyout(val, PTRIN(args->optval), len);
2130 	if (error == 0)
2131 		error = copyout(&len, PTRIN(args->optlen), sizeof(len));
2132 	return (error);
2133 }
2134 
2135 static int
2136 linux_getsockopt_so_peergroups(struct thread *td,
2137     struct linux_getsockopt_args *args)
2138 {
2139 	struct xucred xu;
2140 	socklen_t xulen, len;
2141 	int error, i;
2142 
2143 	xulen = sizeof(xu);
2144 	error = kern_getsockopt(td, args->s, 0,
2145 	    LOCAL_PEERCRED, &xu, UIO_SYSSPACE, &xulen);
2146 	if (error != 0)
2147 		return (error);
2148 
2149 	len = xu.cr_ngroups * sizeof(l_gid_t);
2150 	if (args->optlen < len) {
2151 		error = copyout(&len, PTRIN(args->optlen), sizeof(len));
2152 		if (error == 0)
2153 			error = ERANGE;
2154 		return (error);
2155 	}
2156 
2157 	/*
2158 	 * "- 1" to skip the primary group.
2159 	 */
2160 	for (i = 0; i < xu.cr_ngroups - 1; i++) {
2161 		error = copyout(xu.cr_groups + i + 1,
2162 		    (void *)(args->optval + i * sizeof(l_gid_t)),
2163 		    sizeof(l_gid_t));
2164 		if (error != 0)
2165 			return (error);
2166 	}
2167 
2168 	error = copyout(&len, PTRIN(args->optlen), sizeof(len));
2169 	return (error);
2170 }
2171 
2172 static int
2173 linux_getsockopt_so_peersec(struct thread *td,
2174     struct linux_getsockopt_args *args)
2175 {
2176 	socklen_t len;
2177 	int error;
2178 
2179 	len = sizeof(SECURITY_CONTEXT_STRING);
2180 	if (args->optlen < len) {
2181 		error = copyout(&len, PTRIN(args->optlen), sizeof(len));
2182 		if (error == 0)
2183 			error = ERANGE;
2184 		return (error);
2185 	}
2186 
2187 	return (linux_sockopt_copyout(td, SECURITY_CONTEXT_STRING,
2188 	    len, args));
2189 }
2190 
2191 static int
2192 linux_getsockopt_so_linger(struct thread *td,
2193     struct linux_getsockopt_args *args)
2194 {
2195 	struct linger ling;
2196 	socklen_t len;
2197 	int error;
2198 
2199 	len = sizeof(ling);
2200 	error = kern_getsockopt(td, args->s, SOL_SOCKET,
2201 	    SO_LINGER, &ling, UIO_SYSSPACE, &len);
2202 	if (error != 0)
2203 		return (error);
2204 	ling.l_onoff = ((ling.l_onoff & SO_LINGER) != 0);
2205 	return (linux_sockopt_copyout(td, &ling, len, args));
2206 }
2207 
2208 int
2209 linux_getsockopt(struct thread *td, struct linux_getsockopt_args *args)
2210 {
2211 	l_timeval linux_tv;
2212 	struct timeval tv;
2213 	socklen_t tv_len, xulen, len;
2214 	struct sockaddr *sa;
2215 	struct xucred xu;
2216 	struct l_ucred lxu;
2217 	int error, level, name, newval;
2218 
2219 	level = linux_to_bsd_sockopt_level(args->level);
2220 	switch (level) {
2221 	case SOL_SOCKET:
2222 		switch (args->optname) {
2223 		case LINUX_SO_PEERGROUPS:
2224 			return (linux_getsockopt_so_peergroups(td, args));
2225 		case LINUX_SO_PEERSEC:
2226 			return (linux_getsockopt_so_peersec(td, args));
2227 		default:
2228 			break;
2229 		}
2230 
2231 		name = linux_to_bsd_so_sockopt(args->optname);
2232 		switch (name) {
2233 		case LOCAL_CREDS_PERSISTENT:
2234 			level = SOL_LOCAL;
2235 			break;
2236 		case SO_RCVTIMEO:
2237 			/* FALLTHROUGH */
2238 		case SO_SNDTIMEO:
2239 			tv_len = sizeof(tv);
2240 			error = kern_getsockopt(td, args->s, level,
2241 			    name, &tv, UIO_SYSSPACE, &tv_len);
2242 			if (error != 0)
2243 				return (error);
2244 			linux_tv.tv_sec = tv.tv_sec;
2245 			linux_tv.tv_usec = tv.tv_usec;
2246 			return (linux_sockopt_copyout(td, &linux_tv,
2247 			    sizeof(linux_tv), args));
2248 			/* NOTREACHED */
2249 		case LOCAL_PEERCRED:
2250 			if (args->optlen < sizeof(lxu))
2251 				return (EINVAL);
2252 			/*
2253 			 * LOCAL_PEERCRED is not served at the SOL_SOCKET level,
2254 			 * but by the Unix socket's level 0.
2255 			 */
2256 			level = 0;
2257 			xulen = sizeof(xu);
2258 			error = kern_getsockopt(td, args->s, level,
2259 			    name, &xu, UIO_SYSSPACE, &xulen);
2260 			if (error != 0)
2261 				return (error);
2262 			lxu.pid = xu.cr_pid;
2263 			lxu.uid = xu.cr_uid;
2264 			lxu.gid = xu.cr_gid;
2265 			return (linux_sockopt_copyout(td, &lxu,
2266 			    sizeof(lxu), args));
2267 			/* NOTREACHED */
2268 		case SO_ERROR:
2269 			len = sizeof(newval);
2270 			error = kern_getsockopt(td, args->s, level,
2271 			    name, &newval, UIO_SYSSPACE, &len);
2272 			if (error != 0)
2273 				return (error);
2274 			newval = -bsd_to_linux_errno(newval);
2275 			return (linux_sockopt_copyout(td, &newval,
2276 			    len, args));
2277 			/* NOTREACHED */
2278 		case SO_DOMAIN:
2279 			len = sizeof(newval);
2280 			error = kern_getsockopt(td, args->s, level,
2281 			    name, &newval, UIO_SYSSPACE, &len);
2282 			if (error != 0)
2283 				return (error);
2284 			newval = bsd_to_linux_domain(newval);
2285 			if (newval == -1)
2286 				return (ENOPROTOOPT);
2287 			return (linux_sockopt_copyout(td, &newval,
2288 			    len, args));
2289 			/* NOTREACHED */
2290 		case SO_LINGER:
2291 			return (linux_getsockopt_so_linger(td, args));
2292 			/* NOTREACHED */
2293 		default:
2294 			break;
2295 		}
2296 		break;
2297 	case IPPROTO_IP:
2298 		name = linux_to_bsd_ip_sockopt(args->optname);
2299 		break;
2300 	case IPPROTO_IPV6:
2301 		name = linux_to_bsd_ip6_sockopt(args->optname);
2302 		break;
2303 	case IPPROTO_TCP:
2304 		name = linux_to_bsd_tcp_sockopt(args->optname);
2305 		break;
2306 	default:
2307 		name = -1;
2308 		break;
2309 	}
2310 	if (name < 0) {
2311 		if (name == -1)
2312 			linux_msg(curthread,
2313 			    "unsupported getsockopt level %d optname %d",
2314 			    args->level, args->optname);
2315 		return (EINVAL);
2316 	}
2317 
2318 	if (name == IPV6_NEXTHOP) {
2319 		error = copyin(PTRIN(args->optlen), &len, sizeof(len));
2320                 if (error != 0)
2321                         return (error);
2322 		sa = malloc(len, M_SONAME, M_WAITOK);
2323 
2324 		error = kern_getsockopt(td, args->s, level,
2325 		    name, sa, UIO_SYSSPACE, &len);
2326 		if (error != 0)
2327 			goto out;
2328 
2329 		error = linux_copyout_sockaddr(sa, PTRIN(args->optval), len);
2330 		if (error == 0)
2331 			error = copyout(&len, PTRIN(args->optlen),
2332 			    sizeof(len));
2333 out:
2334 		free(sa, M_SONAME);
2335 	} else {
2336 		if (args->optval) {
2337 			error = copyin(PTRIN(args->optlen), &len, sizeof(len));
2338 			if (error != 0)
2339 				return (error);
2340 		}
2341 		error = kern_getsockopt(td, args->s, level,
2342 		    name, PTRIN(args->optval), UIO_USERSPACE, &len);
2343 		if (error == 0)
2344 			error = copyout(&len, PTRIN(args->optlen),
2345 			    sizeof(len));
2346 	}
2347 
2348 	return (error);
2349 }
2350 
2351 static int
2352 linux_sendfile_common(struct thread *td, l_int out, l_int in,
2353     l_loff_t *offset, l_size_t count)
2354 {
2355 	off_t bytes_read;
2356 	int error;
2357 	l_loff_t current_offset;
2358 	struct file *fp;
2359 
2360 	AUDIT_ARG_FD(in);
2361 	error = fget_read(td, in, &cap_pread_rights, &fp);
2362 	if (error != 0)
2363 		return (error);
2364 
2365 	if (offset != NULL) {
2366 		current_offset = *offset;
2367 	} else {
2368 		error = (fp->f_ops->fo_flags & DFLAG_SEEKABLE) != 0 ?
2369 		    fo_seek(fp, 0, SEEK_CUR, td) : ESPIPE;
2370 		if (error != 0)
2371 			goto drop;
2372 		current_offset = td->td_uretoff.tdu_off;
2373 	}
2374 
2375 	bytes_read = 0;
2376 
2377 	/* Linux cannot have 0 count. */
2378 	if (count <= 0 || current_offset < 0) {
2379 		error = EINVAL;
2380 		goto drop;
2381 	}
2382 
2383 	error = fo_sendfile(fp, out, NULL, NULL, current_offset, count,
2384 	    &bytes_read, 0, td);
2385 	if (error != 0)
2386 		goto drop;
2387 	current_offset += bytes_read;
2388 
2389 	if (offset != NULL) {
2390 		*offset = current_offset;
2391 	} else {
2392 		error = fo_seek(fp, current_offset, SEEK_SET, td);
2393 		if (error != 0)
2394 			goto drop;
2395 	}
2396 
2397 	td->td_retval[0] = (ssize_t)bytes_read;
2398 drop:
2399 	fdrop(fp, td);
2400 	if (error == ENOTSOCK)
2401 		error = EINVAL;
2402 	return (error);
2403 }
2404 
2405 int
2406 linux_sendfile(struct thread *td, struct linux_sendfile_args *arg)
2407 {
2408 	/*
2409 	 * Differences between FreeBSD and Linux sendfile:
2410 	 * - Linux doesn't send anything when count is 0 (FreeBSD uses 0 to
2411 	 *   mean send the whole file.)  In linux_sendfile given fds are still
2412 	 *   checked for validity when the count is 0.
2413 	 * - Linux can send to any fd whereas FreeBSD only supports sockets.
2414 	 *   The same restriction follows for linux_sendfile.
2415 	 * - Linux doesn't have an equivalent for FreeBSD's flags and sf_hdtr.
2416 	 * - Linux takes an offset pointer and updates it to the read location.
2417 	 *   FreeBSD takes in an offset and a 'bytes read' parameter which is
2418 	 *   only filled if it isn't NULL.  We use this parameter to update the
2419 	 *   offset pointer if it exists.
2420 	 * - Linux sendfile returns bytes read on success while FreeBSD
2421 	 *   returns 0.  We use the 'bytes read' parameter to get this value.
2422 	 */
2423 
2424 	l_loff_t offset64;
2425 	l_long offset;
2426 	int ret;
2427 	int error;
2428 
2429 	if (arg->offset != NULL) {
2430 		error = copyin(arg->offset, &offset, sizeof(offset));
2431 		if (error != 0)
2432 			return (error);
2433 		offset64 = (l_loff_t)offset;
2434 	}
2435 
2436 	ret = linux_sendfile_common(td, arg->out, arg->in,
2437 	    arg->offset != NULL ? &offset64 : NULL, arg->count);
2438 
2439 	if (arg->offset != NULL) {
2440 #if defined(__i386__) || defined(__arm__) || \
2441     (defined(__amd64__) && defined(COMPAT_LINUX32))
2442 		if (offset64 > INT32_MAX)
2443 			return (EOVERFLOW);
2444 #endif
2445 		offset = (l_long)offset64;
2446 		error = copyout(&offset, arg->offset, sizeof(offset));
2447 		if (error != 0)
2448 			return (error);
2449 	}
2450 
2451 	return (ret);
2452 }
2453 
2454 #if defined(__i386__) || defined(__arm__) || \
2455     (defined(__amd64__) && defined(COMPAT_LINUX32))
2456 
2457 int
2458 linux_sendfile64(struct thread *td, struct linux_sendfile64_args *arg)
2459 {
2460 	l_loff_t offset;
2461 	int ret;
2462 	int error;
2463 
2464 	if (arg->offset != NULL) {
2465 		error = copyin(arg->offset, &offset, sizeof(offset));
2466 		if (error != 0)
2467 			return (error);
2468 	}
2469 
2470 	ret = linux_sendfile_common(td, arg->out, arg->in,
2471 		arg->offset != NULL ? &offset : NULL, arg->count);
2472 
2473 	if (arg->offset != NULL) {
2474 		error = copyout(&offset, arg->offset, sizeof(offset));
2475 		if (error != 0)
2476 			return (error);
2477 	}
2478 
2479 	return (ret);
2480 }
2481 
2482 /* Argument list sizes for linux_socketcall */
2483 static const unsigned char lxs_args_cnt[] = {
2484 	0 /* unused*/,		3 /* socket */,
2485 	3 /* bind */,		3 /* connect */,
2486 	2 /* listen */,		3 /* accept */,
2487 	3 /* getsockname */,	3 /* getpeername */,
2488 	4 /* socketpair */,	4 /* send */,
2489 	4 /* recv */,		6 /* sendto */,
2490 	6 /* recvfrom */,	2 /* shutdown */,
2491 	5 /* setsockopt */,	5 /* getsockopt */,
2492 	3 /* sendmsg */,	3 /* recvmsg */,
2493 	4 /* accept4 */,	5 /* recvmmsg */,
2494 	4 /* sendmmsg */,	4 /* sendfile */
2495 };
2496 #define	LINUX_ARGS_CNT		(nitems(lxs_args_cnt) - 1)
2497 #define	LINUX_ARG_SIZE(x)	(lxs_args_cnt[x] * sizeof(l_ulong))
2498 
2499 int
2500 linux_socketcall(struct thread *td, struct linux_socketcall_args *args)
2501 {
2502 	l_ulong a[6];
2503 #if defined(__amd64__) && defined(COMPAT_LINUX32)
2504 	register_t l_args[6];
2505 #endif
2506 	void *arg;
2507 	int error;
2508 
2509 	if (args->what < LINUX_SOCKET || args->what > LINUX_ARGS_CNT)
2510 		return (EINVAL);
2511 	error = copyin(PTRIN(args->args), a, LINUX_ARG_SIZE(args->what));
2512 	if (error != 0)
2513 		return (error);
2514 
2515 #if defined(__amd64__) && defined(COMPAT_LINUX32)
2516 	for (int i = 0; i < lxs_args_cnt[args->what]; ++i)
2517 		l_args[i] = a[i];
2518 	arg = l_args;
2519 #else
2520 	arg = a;
2521 #endif
2522 	switch (args->what) {
2523 	case LINUX_SOCKET:
2524 		return (linux_socket(td, arg));
2525 	case LINUX_BIND:
2526 		return (linux_bind(td, arg));
2527 	case LINUX_CONNECT:
2528 		return (linux_connect(td, arg));
2529 	case LINUX_LISTEN:
2530 		return (linux_listen(td, arg));
2531 	case LINUX_ACCEPT:
2532 		return (linux_accept(td, arg));
2533 	case LINUX_GETSOCKNAME:
2534 		return (linux_getsockname(td, arg));
2535 	case LINUX_GETPEERNAME:
2536 		return (linux_getpeername(td, arg));
2537 	case LINUX_SOCKETPAIR:
2538 		return (linux_socketpair(td, arg));
2539 	case LINUX_SEND:
2540 		return (linux_send(td, arg));
2541 	case LINUX_RECV:
2542 		return (linux_recv(td, arg));
2543 	case LINUX_SENDTO:
2544 		return (linux_sendto(td, arg));
2545 	case LINUX_RECVFROM:
2546 		return (linux_recvfrom(td, arg));
2547 	case LINUX_SHUTDOWN:
2548 		return (linux_shutdown(td, arg));
2549 	case LINUX_SETSOCKOPT:
2550 		return (linux_setsockopt(td, arg));
2551 	case LINUX_GETSOCKOPT:
2552 		return (linux_getsockopt(td, arg));
2553 	case LINUX_SENDMSG:
2554 		return (linux_sendmsg(td, arg));
2555 	case LINUX_RECVMSG:
2556 		return (linux_recvmsg(td, arg));
2557 	case LINUX_ACCEPT4:
2558 		return (linux_accept4(td, arg));
2559 	case LINUX_RECVMMSG:
2560 		return (linux_recvmmsg(td, arg));
2561 	case LINUX_SENDMMSG:
2562 		return (linux_sendmmsg(td, arg));
2563 	case LINUX_SENDFILE:
2564 		return (linux_sendfile(td, arg));
2565 	}
2566 
2567 	linux_msg(td, "socket type %d not implemented", args->what);
2568 	return (ENOSYS);
2569 }
2570 #endif /* __i386__ || __arm__ || (__amd64__ && COMPAT_LINUX32) */
2571