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