xref: /freebsd/sys/compat/linux/linux_socket.c (revision 5ca8e32633c4ffbbcd6762e5888b6a4ba0708c6c)
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 retval_socket, type;
874 	sa_family_t domain;
875 
876 	type = args->type & LINUX_SOCK_TYPE_MASK;
877 	if (type < 0 || type > LINUX_SOCK_MAX)
878 		return (EINVAL);
879 	retval_socket = linux_set_socket_flags(args->type & ~LINUX_SOCK_TYPE_MASK,
880 		&type);
881 	if (retval_socket != 0)
882 		return (retval_socket);
883 	domain = linux_to_bsd_domain(args->domain);
884 	if (domain == AF_UNKNOWN) {
885 		/* Mask off SOCK_NONBLOCK / CLOEXEC for error messages. */
886 		type = args->type & LINUX_SOCK_TYPE_MASK;
887 		if (args->domain == LINUX_AF_NETLINK &&
888 		    args->protocol == LINUX_NETLINK_AUDIT) {
889 			; /* Do nothing, quietly. */
890 		} else if (args->domain == LINUX_AF_NETLINK) {
891 			const char *nl_name;
892 
893 			if (args->protocol >= 0 &&
894 			    args->protocol < nitems(linux_netlink_names))
895 				nl_name = linux_netlink_names[args->protocol];
896 			else
897 				nl_name = NULL;
898 			if (nl_name != NULL)
899 				linux_msg(curthread,
900 				    "unsupported socket(AF_NETLINK, %d, "
901 				    "NETLINK_%s)", type, nl_name);
902 			else
903 				linux_msg(curthread,
904 				    "unsupported socket(AF_NETLINK, %d, %d)",
905 				    type, args->protocol);
906 		} else {
907 			linux_msg(curthread, "unsupported socket domain %d, "
908 			    "type %d, protocol %d", args->domain, type,
909 			    args->protocol);
910 		}
911 		return (EAFNOSUPPORT);
912 	}
913 
914 	retval_socket = kern_socket(td, domain, type, args->protocol);
915 	if (retval_socket)
916 		return (retval_socket);
917 
918 	if (type == SOCK_RAW
919 	    && (args->protocol == IPPROTO_RAW || args->protocol == 0)
920 	    && domain == PF_INET) {
921 		/* It's a raw IP socket: set the IP_HDRINCL option. */
922 		int hdrincl;
923 
924 		hdrincl = 1;
925 		/* We ignore any error returned by kern_setsockopt() */
926 		kern_setsockopt(td, td->td_retval[0], IPPROTO_IP, IP_HDRINCL,
927 		    &hdrincl, UIO_SYSSPACE, sizeof(hdrincl));
928 	}
929 #ifdef INET6
930 	/*
931 	 * Linux AF_INET6 socket has IPV6_V6ONLY setsockopt set to 0 by default
932 	 * and some apps depend on this. So, set V6ONLY to 0 for Linux apps.
933 	 * For simplicity we do this unconditionally of the net.inet6.ip6.v6only
934 	 * sysctl value.
935 	 */
936 	if (domain == PF_INET6) {
937 		int v6only;
938 
939 		v6only = 0;
940 		/* We ignore any error returned by setsockopt() */
941 		kern_setsockopt(td, td->td_retval[0], IPPROTO_IPV6, IPV6_V6ONLY,
942 		    &v6only, UIO_SYSSPACE, sizeof(v6only));
943 	}
944 #endif
945 
946 	return (retval_socket);
947 }
948 
949 int
950 linux_bind(struct thread *td, struct linux_bind_args *args)
951 {
952 	struct sockaddr *sa;
953 	int error;
954 
955 	error = linux_to_bsd_sockaddr(PTRIN(args->name), &sa,
956 	    &args->namelen);
957 	if (error != 0)
958 		return (error);
959 
960 	error = kern_bindat(td, AT_FDCWD, args->s, sa);
961 	free(sa, M_SONAME);
962 
963 	/* XXX */
964 	if (error == EADDRNOTAVAIL && args->namelen != sizeof(struct sockaddr_in))
965 		return (EINVAL);
966 	return (error);
967 }
968 
969 int
970 linux_connect(struct thread *td, struct linux_connect_args *args)
971 {
972 	struct socket *so;
973 	struct sockaddr *sa;
974 	struct file *fp;
975 	int error;
976 
977 	error = linux_to_bsd_sockaddr(PTRIN(args->name), &sa,
978 	    &args->namelen);
979 	if (error != 0)
980 		return (error);
981 
982 	error = kern_connectat(td, AT_FDCWD, args->s, sa);
983 	free(sa, M_SONAME);
984 	if (error != EISCONN)
985 		return (error);
986 
987 	/*
988 	 * Linux doesn't return EISCONN the first time it occurs,
989 	 * when on a non-blocking socket. Instead it returns the
990 	 * error getsockopt(SOL_SOCKET, SO_ERROR) would return on BSD.
991 	 */
992 	error = getsock(td, args->s, &cap_connect_rights, &fp);
993 	if (error != 0)
994 		return (error);
995 
996 	error = EISCONN;
997 	so = fp->f_data;
998 	if (atomic_load_int(&fp->f_flag) & 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_storage ss = { .ss_len = sizeof(ss) };
1022 	struct file *fp, *fp1;
1023 	struct socket *so;
1024 	socklen_t len;
1025 	int bflags, error, error1;
1026 
1027 	bflags = 0;
1028 	fp = NULL;
1029 
1030 	error = linux_set_socket_flags(flags, &bflags);
1031 	if (error != 0)
1032 		return (error);
1033 
1034 	if (PTRIN(addr) != NULL) {
1035 		error = copyin(PTRIN(namelen), &len, sizeof(len));
1036 		if (error != 0)
1037 			return (error);
1038 		if (len < 0)
1039 			return (EINVAL);
1040 	} else
1041 		len = 0;
1042 
1043 	error = kern_accept4(td, s, (struct sockaddr *)&ss, bflags, &fp);
1044 
1045 	/*
1046 	 * Translate errno values into ones used by Linux.
1047 	 */
1048 	if (error != 0) {
1049 		/*
1050 		 * XXX. This is wrong, different sockaddr structures
1051 		 * have different sizes.
1052 		 */
1053 		switch (error) {
1054 		case EFAULT:
1055 			if (namelen != sizeof(struct sockaddr_in))
1056 				error = EINVAL;
1057 			break;
1058 		case EINVAL:
1059 			error1 = getsock(td, s, &cap_accept_rights, &fp1);
1060 			if (error1 != 0) {
1061 				error = error1;
1062 				break;
1063 			}
1064 			so = fp1->f_data;
1065 			if (so->so_type == SOCK_DGRAM)
1066 				error = EOPNOTSUPP;
1067 			fdrop(fp1, td);
1068 			break;
1069 		}
1070 		return (error);
1071 	}
1072 
1073 	if (PTRIN(addr) != NULL) {
1074 		len = min(ss.ss_len, len);
1075 		error = linux_copyout_sockaddr((struct sockaddr *)&ss,
1076 		    PTRIN(addr), len);
1077 		if (error == 0) {
1078 			len = ss.ss_len;
1079 			error = copyout(&len, PTRIN(namelen), sizeof(len));
1080 		}
1081 		if (error != 0) {
1082 			fdclose(td, fp, td->td_retval[0]);
1083 			td->td_retval[0] = 0;
1084 		}
1085 	}
1086 	if (fp != NULL)
1087 		fdrop(fp, td);
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_storage ss = { .ss_len = sizeof(ss) };
1111 	socklen_t len;
1112 	int error;
1113 
1114 	error = copyin(PTRIN(args->namelen), &len, sizeof(len));
1115 	if (error != 0)
1116 		return (error);
1117 
1118 	error = kern_getsockname(td, args->s, (struct sockaddr *)&ss);
1119 	if (error != 0)
1120 		return (error);
1121 
1122 	len = min(ss.ss_len, len);
1123 	error = linux_copyout_sockaddr((struct sockaddr *)&ss,
1124 	    PTRIN(args->addr), len);
1125 	if (error == 0) {
1126 		len = ss.ss_len;
1127 		error = copyout(&len, PTRIN(args->namelen), sizeof(len));
1128 	}
1129 	return (error);
1130 }
1131 
1132 int
1133 linux_getpeername(struct thread *td, struct linux_getpeername_args *args)
1134 {
1135 	struct sockaddr_storage ss = { .ss_len = sizeof(ss) };
1136 	socklen_t len;
1137 	int error;
1138 
1139 	error = copyin(PTRIN(args->namelen), &len, sizeof(len));
1140 	if (error != 0)
1141 		return (error);
1142 
1143 	error = kern_getpeername(td, args->s, (struct sockaddr *)&ss);
1144 	if (error != 0)
1145 		return (error);
1146 
1147 	len = min(ss.ss_len, len);
1148 	error = linux_copyout_sockaddr((struct sockaddr *)&ss,
1149 	    PTRIN(args->addr), len);
1150 	if (error == 0) {
1151 		len = ss.ss_len;
1152 		error = copyout(&len, PTRIN(args->namelen), sizeof(len));
1153 	}
1154 	return (error);
1155 }
1156 
1157 int
1158 linux_socketpair(struct thread *td, struct linux_socketpair_args *args)
1159 {
1160 	int domain, error, sv[2], type;
1161 
1162 	domain = linux_to_bsd_domain(args->domain);
1163 	if (domain != PF_LOCAL)
1164 		return (EAFNOSUPPORT);
1165 	type = args->type & LINUX_SOCK_TYPE_MASK;
1166 	if (type < 0 || type > LINUX_SOCK_MAX)
1167 		return (EINVAL);
1168 	error = linux_set_socket_flags(args->type & ~LINUX_SOCK_TYPE_MASK,
1169 	    &type);
1170 	if (error != 0)
1171 		return (error);
1172 	if (args->protocol != 0 && args->protocol != PF_UNIX) {
1173 		/*
1174 		 * Use of PF_UNIX as protocol argument is not right,
1175 		 * but Linux does it.
1176 		 * Do not map PF_UNIX as its Linux value is identical
1177 		 * to FreeBSD one.
1178 		 */
1179 		return (EPROTONOSUPPORT);
1180 	}
1181 	error = kern_socketpair(td, domain, type, 0, sv);
1182 	if (error != 0)
1183                 return (error);
1184         error = copyout(sv, PTRIN(args->rsv), 2 * sizeof(int));
1185         if (error != 0) {
1186                 (void)kern_close(td, sv[0]);
1187                 (void)kern_close(td, sv[1]);
1188         }
1189 	return (error);
1190 }
1191 
1192 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1193 struct linux_send_args {
1194 	register_t s;
1195 	register_t msg;
1196 	register_t len;
1197 	register_t flags;
1198 };
1199 
1200 static int
1201 linux_send(struct thread *td, struct linux_send_args *args)
1202 {
1203 	struct sendto_args /* {
1204 		int s;
1205 		caddr_t buf;
1206 		int len;
1207 		int flags;
1208 		caddr_t to;
1209 		int tolen;
1210 	} */ bsd_args;
1211 	struct file *fp;
1212 	int error;
1213 
1214 	bsd_args.s = args->s;
1215 	bsd_args.buf = (caddr_t)PTRIN(args->msg);
1216 	bsd_args.len = args->len;
1217 	bsd_args.flags = linux_to_bsd_msg_flags(args->flags);
1218 	bsd_args.to = NULL;
1219 	bsd_args.tolen = 0;
1220 	error = sys_sendto(td, &bsd_args);
1221 	if (error == ENOTCONN) {
1222 		/*
1223 		 * Linux doesn't return ENOTCONN for non-blocking sockets.
1224 		 * Instead it returns the EAGAIN.
1225 		 */
1226 		error = getsock(td, args->s, &cap_send_rights, &fp);
1227 		if (error == 0) {
1228 			if (atomic_load_int(&fp->f_flag) & FNONBLOCK)
1229 				error = EAGAIN;
1230 			fdrop(fp, td);
1231 		}
1232 	}
1233 	return (error);
1234 }
1235 
1236 struct linux_recv_args {
1237 	register_t s;
1238 	register_t msg;
1239 	register_t len;
1240 	register_t flags;
1241 };
1242 
1243 static int
1244 linux_recv(struct thread *td, struct linux_recv_args *args)
1245 {
1246 	struct recvfrom_args /* {
1247 		int s;
1248 		caddr_t buf;
1249 		int len;
1250 		int flags;
1251 		struct sockaddr *from;
1252 		socklen_t fromlenaddr;
1253 	} */ bsd_args;
1254 
1255 	bsd_args.s = args->s;
1256 	bsd_args.buf = (caddr_t)PTRIN(args->msg);
1257 	bsd_args.len = args->len;
1258 	bsd_args.flags = linux_to_bsd_msg_flags(args->flags);
1259 	bsd_args.from = NULL;
1260 	bsd_args.fromlenaddr = 0;
1261 	return (sys_recvfrom(td, &bsd_args));
1262 }
1263 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1264 
1265 int
1266 linux_sendto(struct thread *td, struct linux_sendto_args *args)
1267 {
1268 	struct msghdr msg;
1269 	struct iovec aiov;
1270 	struct socket *so;
1271 	struct file *fp;
1272 	int error;
1273 
1274 	if (linux_check_hdrincl(td, args->s) == 0)
1275 		/* IP_HDRINCL set, tweak the packet before sending */
1276 		return (linux_sendto_hdrincl(td, args));
1277 
1278 	bzero(&msg, sizeof(msg));
1279 	error = getsock(td, args->s, &cap_send_connect_rights, &fp);
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 sockaddr_storage ss = { .ss_len = sizeof(ss) };
1355 	struct cmsghdr *cmsg;
1356 	struct mbuf *control;
1357 	struct msghdr msg;
1358 	struct l_cmsghdr linux_cmsg;
1359 	struct l_cmsghdr *ptr_cmsg;
1360 	struct l_msghdr linux_msghdr;
1361 	struct iovec *iov;
1362 	socklen_t datalen;
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;
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 = freebsd32_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, (struct sockaddr *)&ss);
1402 	if (error != 0)
1403 		goto bad;
1404 	sa_family = ss.ss_family;
1405 
1406 	if (flags & LINUX_MSG_OOB) {
1407 		error = EOPNOTSUPP;
1408 		if (sa_family == AF_UNIX)
1409 			goto bad;
1410 
1411 		error = getsock(td, s, &cap_send_rights, &fp);
1412 		if (error != 0)
1413 			goto bad;
1414 		so = fp->f_data;
1415 		if (so->so_type != SOCK_STREAM)
1416 			error = EOPNOTSUPP;
1417 		fdrop(fp, td);
1418 		if (error != 0)
1419 			goto bad;
1420 	}
1421 
1422 	if (linux_msghdr.msg_controllen >= sizeof(struct l_cmsghdr)) {
1423 		error = ENOBUFS;
1424 		control = m_get(M_WAITOK, MT_CONTROL);
1425 		MCLGET(control, M_WAITOK);
1426 		data = mtod(control, void *);
1427 		datalen = 0;
1428 
1429 		ptr_cmsg = PTRIN(linux_msghdr.msg_control);
1430 		clen = linux_msghdr.msg_controllen;
1431 		do {
1432 			error = copyin(ptr_cmsg, &linux_cmsg,
1433 			    sizeof(struct l_cmsghdr));
1434 			if (error != 0)
1435 				goto bad;
1436 
1437 			error = EINVAL;
1438 			if (linux_cmsg.cmsg_len < sizeof(struct l_cmsghdr) ||
1439 			    linux_cmsg.cmsg_len > clen)
1440 				goto bad;
1441 
1442 			if (datalen + CMSG_HDRSZ > MCLBYTES)
1443 				goto bad;
1444 
1445 			/*
1446 			 * Now we support only SCM_RIGHTS and SCM_CRED,
1447 			 * so return EINVAL in any other cmsg_type
1448 			 */
1449 			cmsg = data;
1450 			cmsg->cmsg_type =
1451 			    linux_to_bsd_cmsg_type(linux_cmsg.cmsg_type);
1452 			cmsg->cmsg_level =
1453 			    linux_to_bsd_sockopt_level(linux_cmsg.cmsg_level);
1454 			if (cmsg->cmsg_type == -1
1455 			    || cmsg->cmsg_level != SOL_SOCKET) {
1456 				linux_msg(curthread,
1457 				    "unsupported sendmsg cmsg level %d type %d",
1458 				    linux_cmsg.cmsg_level, linux_cmsg.cmsg_type);
1459 				goto bad;
1460 			}
1461 
1462 			/*
1463 			 * Some applications (e.g. pulseaudio) attempt to
1464 			 * send ancillary data even if the underlying protocol
1465 			 * doesn't support it which is not allowed in the
1466 			 * FreeBSD system call interface.
1467 			 */
1468 			if (sa_family != AF_UNIX)
1469 				goto next;
1470 
1471 			if (cmsg->cmsg_type == SCM_CREDS) {
1472 				len = sizeof(struct cmsgcred);
1473 				if (datalen + CMSG_SPACE(len) > MCLBYTES)
1474 					goto bad;
1475 
1476 				/*
1477 				 * The lower levels will fill in the structure
1478 				 */
1479 				memset(CMSG_DATA(data), 0, len);
1480 			} else {
1481 				len = linux_cmsg.cmsg_len - L_CMSG_HDRSZ;
1482 				if (datalen + CMSG_SPACE(len) < datalen ||
1483 				    datalen + CMSG_SPACE(len) > MCLBYTES)
1484 					goto bad;
1485 
1486 				error = copyin(LINUX_CMSG_DATA(ptr_cmsg),
1487 				    CMSG_DATA(data), len);
1488 				if (error != 0)
1489 					goto bad;
1490 			}
1491 
1492 			cmsg->cmsg_len = CMSG_LEN(len);
1493 			data = (char *)data + CMSG_SPACE(len);
1494 			datalen += CMSG_SPACE(len);
1495 
1496 next:
1497 			if (clen <= LINUX_CMSG_ALIGN(linux_cmsg.cmsg_len))
1498 				break;
1499 
1500 			clen -= LINUX_CMSG_ALIGN(linux_cmsg.cmsg_len);
1501 			ptr_cmsg = (struct l_cmsghdr *)((char *)ptr_cmsg +
1502 			    LINUX_CMSG_ALIGN(linux_cmsg.cmsg_len));
1503 		} while(clen >= sizeof(struct l_cmsghdr));
1504 
1505 		control->m_len = datalen;
1506 		if (datalen == 0) {
1507 			m_freem(control);
1508 			control = NULL;
1509 		}
1510 	}
1511 
1512 	msg.msg_iov = iov;
1513 	msg.msg_flags = 0;
1514 	error = linux_sendit(td, s, &msg, flags, control, UIO_USERSPACE);
1515 	control = NULL;
1516 
1517 bad:
1518 	m_freem(control);
1519 	free(iov, M_IOV);
1520 	return (error);
1521 }
1522 
1523 int
1524 linux_sendmsg(struct thread *td, struct linux_sendmsg_args *args)
1525 {
1526 
1527 	return (linux_sendmsg_common(td, args->s, PTRIN(args->msg),
1528 	    args->flags));
1529 }
1530 
1531 int
1532 linux_sendmmsg(struct thread *td, struct linux_sendmmsg_args *args)
1533 {
1534 	struct l_mmsghdr *msg;
1535 	l_uint retval;
1536 	int error, datagrams;
1537 
1538 	if (args->vlen > UIO_MAXIOV)
1539 		args->vlen = UIO_MAXIOV;
1540 
1541 	msg = PTRIN(args->msg);
1542 	datagrams = 0;
1543 	while (datagrams < args->vlen) {
1544 		error = linux_sendmsg_common(td, args->s, &msg->msg_hdr,
1545 		    args->flags);
1546 		if (error != 0)
1547 			break;
1548 
1549 		retval = td->td_retval[0];
1550 		error = copyout(&retval, &msg->msg_len, sizeof(msg->msg_len));
1551 		if (error != 0)
1552 			break;
1553 		++msg;
1554 		++datagrams;
1555 	}
1556 	if (error == 0)
1557 		td->td_retval[0] = datagrams;
1558 	return (error);
1559 }
1560 
1561 static int
1562 recvmsg_scm_rights(struct thread *td, l_uint flags, socklen_t *datalen,
1563     void **data, void **udata)
1564 {
1565 	int i, fd, fds, *fdp;
1566 
1567 	if (flags & LINUX_MSG_CMSG_CLOEXEC) {
1568 		fds = *datalen / sizeof(int);
1569 		fdp = *data;
1570 		for (i = 0; i < fds; i++) {
1571 			fd = *fdp++;
1572 			(void)kern_fcntl(td, fd, F_SETFD, FD_CLOEXEC);
1573 		}
1574 	}
1575 	return (0);
1576 }
1577 
1578 
1579 static int
1580 recvmsg_scm_creds(socklen_t *datalen, void **data, void **udata)
1581 {
1582 	struct cmsgcred *cmcred;
1583 	struct l_ucred lu;
1584 
1585 	cmcred = *data;
1586 	lu.pid = cmcred->cmcred_pid;
1587 	lu.uid = cmcred->cmcred_uid;
1588 	lu.gid = cmcred->cmcred_gid;
1589 	memmove(*data, &lu, sizeof(lu));
1590 	*datalen = sizeof(lu);
1591 	return (0);
1592 }
1593 _Static_assert(sizeof(struct cmsgcred) >= sizeof(struct l_ucred),
1594     "scm_creds sizeof l_ucred");
1595 
1596 static int
1597 recvmsg_scm_creds2(socklen_t *datalen, void **data, void **udata)
1598 {
1599 	struct sockcred2 *scred;
1600 	struct l_ucred lu;
1601 
1602 	scred = *data;
1603 	lu.pid = scred->sc_pid;
1604 	lu.uid = scred->sc_uid;
1605 	lu.gid = scred->sc_gid;
1606 	memmove(*data, &lu, sizeof(lu));
1607 	*datalen = sizeof(lu);
1608 	return (0);
1609 }
1610 _Static_assert(sizeof(struct sockcred2) >= sizeof(struct l_ucred),
1611     "scm_creds2 sizeof l_ucred");
1612 
1613 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1614 static int
1615 recvmsg_scm_timestamp(l_int msg_type, socklen_t *datalen, void **data,
1616     void **udata)
1617 {
1618 	l_sock_timeval ltv64;
1619 	l_timeval ltv;
1620 	struct timeval *tv;
1621 	socklen_t len;
1622 	void *buf;
1623 
1624 	if (*datalen != sizeof(struct timeval))
1625 		return (EMSGSIZE);
1626 
1627 	tv = *data;
1628 #if defined(COMPAT_LINUX32)
1629 	if (msg_type == LINUX_SCM_TIMESTAMPO &&
1630 	    (tv->tv_sec > INT_MAX || tv->tv_sec < INT_MIN))
1631 		return (EOVERFLOW);
1632 #endif
1633 	if (msg_type == LINUX_SCM_TIMESTAMPN)
1634 		len = sizeof(ltv64);
1635 	else
1636 		len = sizeof(ltv);
1637 
1638 	buf = malloc(len, M_LINUX, M_WAITOK);
1639 	if (msg_type == LINUX_SCM_TIMESTAMPN) {
1640 		ltv64.tv_sec = tv->tv_sec;
1641 		ltv64.tv_usec = tv->tv_usec;
1642 		memmove(buf, &ltv64, len);
1643 	} else {
1644 		ltv.tv_sec = tv->tv_sec;
1645 		ltv.tv_usec = tv->tv_usec;
1646 		memmove(buf, &ltv, len);
1647 	}
1648 	*data = *udata = buf;
1649 	*datalen = len;
1650 	return (0);
1651 }
1652 #else
1653 _Static_assert(sizeof(struct timeval) == sizeof(l_timeval),
1654     "scm_timestamp sizeof l_timeval");
1655 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1656 
1657 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1658 static int
1659 recvmsg_scm_timestampns(l_int msg_type, socklen_t *datalen, void **data,
1660     void **udata)
1661 {
1662 	struct l_timespec64 ts64;
1663 	struct l_timespec ts32;
1664 	struct timespec ts;
1665 	socklen_t len;
1666 	void *buf;
1667 
1668 	if (msg_type == LINUX_SCM_TIMESTAMPNSO)
1669 		len = sizeof(ts32);
1670 	else
1671 		len = sizeof(ts64);
1672 
1673 	buf = malloc(len, M_LINUX, M_WAITOK);
1674 	bintime2timespec(*data, &ts);
1675 	if (msg_type == LINUX_SCM_TIMESTAMPNSO) {
1676 		ts32.tv_sec = ts.tv_sec;
1677 		ts32.tv_nsec = ts.tv_nsec;
1678 		memmove(buf, &ts32, len);
1679 	} else {
1680 		ts64.tv_sec = ts.tv_sec;
1681 		ts64.tv_nsec = ts.tv_nsec;
1682 		memmove(buf, &ts64, len);
1683 	}
1684 	*data = *udata = buf;
1685 	*datalen = len;
1686 	return (0);
1687 }
1688 #else
1689 static int
1690 recvmsg_scm_timestampns(l_int msg_type, socklen_t *datalen, void **data,
1691     void **udata)
1692 {
1693 	struct timespec ts;
1694 
1695 	bintime2timespec(*data, &ts);
1696 	memmove(*data, &ts, sizeof(struct timespec));
1697 	*datalen = sizeof(struct timespec);
1698 	return (0);
1699 }
1700 _Static_assert(sizeof(struct bintime) >= sizeof(struct timespec),
1701     "scm_timestampns sizeof timespec");
1702 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1703 
1704 static int
1705 recvmsg_scm_sol_socket(struct thread *td, l_int msg_type, l_int lmsg_type,
1706     l_uint flags, socklen_t *datalen, void **data, void **udata)
1707 {
1708 	int error;
1709 
1710 	error = 0;
1711 	switch (msg_type) {
1712 	case SCM_RIGHTS:
1713 		error = recvmsg_scm_rights(td, flags, datalen,
1714 		    data, udata);
1715 		break;
1716 	case SCM_CREDS:
1717 		error = recvmsg_scm_creds(datalen, data, udata);
1718 		break;
1719 	case SCM_CREDS2:
1720 		error = recvmsg_scm_creds2(datalen, data, udata);
1721 		break;
1722 	case SCM_TIMESTAMP:
1723 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1724 		error = recvmsg_scm_timestamp(lmsg_type, datalen,
1725 		    data, udata);
1726 #endif
1727 		break;
1728 	case SCM_BINTIME:
1729 		error = recvmsg_scm_timestampns(lmsg_type, datalen,
1730 		    data, udata);
1731 		break;
1732 	}
1733 
1734 	return (error);
1735 }
1736 
1737 static int
1738 recvmsg_scm_ip_origdstaddr(socklen_t *datalen, void **data, void **udata)
1739 {
1740 	struct l_sockaddr *lsa;
1741 	int error;
1742 
1743 	error = bsd_to_linux_sockaddr(*data, &lsa, *datalen);
1744 	if (error == 0) {
1745 		*data = *udata = lsa;
1746 		*datalen = sizeof(*lsa);
1747 	}
1748 	return (error);
1749 }
1750 
1751 static int
1752 recvmsg_scm_ipproto_ip(l_int msg_type, l_int lmsg_type, socklen_t *datalen,
1753     void **data, void **udata)
1754 {
1755 	int error;
1756 
1757 	error = 0;
1758 	switch (msg_type) {
1759 	case IP_ORIGDSTADDR:
1760 		error = recvmsg_scm_ip_origdstaddr(datalen, data,
1761 		    udata);
1762 		break;
1763 	}
1764 
1765 	return (error);
1766 }
1767 
1768 static int
1769 linux_recvmsg_common(struct thread *td, l_int s, struct l_msghdr *msghdr,
1770     l_uint flags, struct msghdr *msg)
1771 {
1772 	struct proc *p = td->td_proc;
1773 	struct cmsghdr *cm;
1774 	struct l_cmsghdr *lcm = NULL;
1775 	socklen_t datalen, maxlen, outlen;
1776 	struct l_msghdr l_msghdr;
1777 	struct iovec *iov, *uiov;
1778 	struct mbuf *m, *control = NULL;
1779 	struct mbuf **controlp;
1780 	struct sockaddr *sa;
1781 	caddr_t outbuf;
1782 	void *data, *udata;
1783 	int error, skiped;
1784 
1785 	error = copyin(msghdr, &l_msghdr, sizeof(l_msghdr));
1786 	if (error != 0)
1787 		return (error);
1788 
1789 	/*
1790 	 * Pass user-supplied recvmsg() flags in msg_flags field,
1791 	 * following sys_recvmsg() convention.
1792 	*/
1793 	l_msghdr.msg_flags = flags;
1794 
1795 	error = linux_to_bsd_msghdr(msg, &l_msghdr);
1796 	if (error != 0)
1797 		return (error);
1798 
1799 #ifdef COMPAT_LINUX32
1800 	error = freebsd32_copyiniov(PTRIN(msg->msg_iov), msg->msg_iovlen,
1801 	    &iov, EMSGSIZE);
1802 #else
1803 	error = copyiniov(msg->msg_iov, msg->msg_iovlen, &iov, EMSGSIZE);
1804 #endif
1805 	if (error != 0)
1806 		return (error);
1807 
1808 	if (msg->msg_name != NULL && msg->msg_namelen > 0) {
1809 		msg->msg_namelen = min(msg->msg_namelen, SOCK_MAXADDRLEN);
1810 		sa = malloc(msg->msg_namelen, M_SONAME, M_WAITOK);
1811 		msg->msg_name = sa;
1812 	} else {
1813 		sa = NULL;
1814 		msg->msg_name = NULL;
1815 	}
1816 
1817 	uiov = msg->msg_iov;
1818 	msg->msg_iov = iov;
1819 	controlp = (msg->msg_control != NULL) ? &control : NULL;
1820 	error = kern_recvit(td, s, msg, UIO_SYSSPACE, controlp);
1821 	msg->msg_iov = uiov;
1822 	if (error != 0)
1823 		goto bad;
1824 
1825 	/*
1826 	 * Note that kern_recvit() updates msg->msg_namelen.
1827 	 */
1828 	if (msg->msg_name != NULL && msg->msg_namelen > 0) {
1829 		msg->msg_name = PTRIN(l_msghdr.msg_name);
1830 		error = linux_copyout_sockaddr(sa, msg->msg_name,
1831 		    msg->msg_namelen);
1832 		if (error != 0)
1833 			goto bad;
1834 	}
1835 
1836 	error = bsd_to_linux_msghdr(msg, &l_msghdr);
1837 	if (error != 0)
1838 		goto bad;
1839 
1840 	skiped = outlen = 0;
1841 	maxlen = l_msghdr.msg_controllen;
1842 	if (control == NULL)
1843 		goto out;
1844 
1845 	lcm = malloc(L_CMSG_HDRSZ, M_LINUX, M_WAITOK | M_ZERO);
1846 	msg->msg_control = mtod(control, struct cmsghdr *);
1847 	msg->msg_controllen = control->m_len;
1848 	outbuf = PTRIN(l_msghdr.msg_control);
1849 	for (m = control; m != NULL; m = m->m_next) {
1850 		cm = mtod(m, struct cmsghdr *);
1851 		lcm->cmsg_type = bsd_to_linux_cmsg_type(p, cm->cmsg_type,
1852 		    cm->cmsg_level);
1853 		lcm->cmsg_level = bsd_to_linux_sockopt_level(cm->cmsg_level);
1854 
1855 		if (lcm->cmsg_type == -1 ||
1856 		    cm->cmsg_level == -1) {
1857 			LINUX_RATELIMIT_MSG_OPT2(
1858 			    "unsupported recvmsg cmsg level %d type %d",
1859 			    cm->cmsg_level, cm->cmsg_type);
1860 			/* Skip unsupported messages */
1861 			skiped++;
1862 			continue;
1863 		}
1864 		data = CMSG_DATA(cm);
1865 		datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
1866 		udata = NULL;
1867 		error = 0;
1868 
1869 		switch (cm->cmsg_level) {
1870 		case IPPROTO_IP:
1871 			error = recvmsg_scm_ipproto_ip(cm->cmsg_type,
1872 			    lcm->cmsg_type, &datalen, &data, &udata);
1873  			break;
1874 		case SOL_SOCKET:
1875 			error = recvmsg_scm_sol_socket(td, cm->cmsg_type,
1876 			    lcm->cmsg_type, flags, &datalen, &data, &udata);
1877  			break;
1878  		}
1879 
1880 		/* The recvmsg_scm_ is responsible to free udata on error. */
1881 		if (error != 0)
1882 			goto bad;
1883 
1884 		if (outlen + LINUX_CMSG_LEN(datalen) > maxlen) {
1885 			if (outlen == 0) {
1886 				error = EMSGSIZE;
1887 				goto err;
1888 			} else {
1889 				l_msghdr.msg_flags |= LINUX_MSG_CTRUNC;
1890 				m_dispose_extcontrolm(control);
1891 				free(udata, M_LINUX);
1892 				goto out;
1893 			}
1894 		}
1895 
1896 		lcm->cmsg_len = LINUX_CMSG_LEN(datalen);
1897 		error = copyout(lcm, outbuf, L_CMSG_HDRSZ);
1898 		if (error == 0) {
1899 			error = copyout(data, LINUX_CMSG_DATA(outbuf), datalen);
1900 			if (error == 0) {
1901 				outbuf += LINUX_CMSG_SPACE(datalen);
1902 				outlen += LINUX_CMSG_SPACE(datalen);
1903 			}
1904 		}
1905 err:
1906 		free(udata, M_LINUX);
1907 		if (error != 0)
1908 			goto bad;
1909 	}
1910 	if (outlen == 0 && skiped > 0) {
1911 		error = EINVAL;
1912 		goto bad;
1913 	}
1914 
1915 out:
1916 	l_msghdr.msg_controllen = outlen;
1917 	error = copyout(&l_msghdr, msghdr, sizeof(l_msghdr));
1918 
1919 bad:
1920 	if (control != NULL) {
1921 		if (error != 0)
1922 			m_dispose_extcontrolm(control);
1923 		m_freem(control);
1924 	}
1925 	free(iov, M_IOV);
1926 	free(lcm, M_LINUX);
1927 	free(sa, M_SONAME);
1928 
1929 	return (error);
1930 }
1931 
1932 int
1933 linux_recvmsg(struct thread *td, struct linux_recvmsg_args *args)
1934 {
1935 	struct msghdr bsd_msg;
1936 	struct file *fp;
1937 	int error;
1938 
1939 	error = getsock(td, args->s, &cap_recv_rights, &fp);
1940 	if (error != 0)
1941 		return (error);
1942 	fdrop(fp, td);
1943 	return (linux_recvmsg_common(td, args->s, PTRIN(args->msg),
1944 	    args->flags, &bsd_msg));
1945 }
1946 
1947 static int
1948 linux_recvmmsg_common(struct thread *td, l_int s, struct l_mmsghdr *msg,
1949     l_uint vlen, l_uint flags, struct timespec *tts)
1950 {
1951 	struct msghdr bsd_msg;
1952 	struct timespec ts;
1953 	struct file *fp;
1954 	l_uint retval;
1955 	int error, datagrams;
1956 
1957 	error = getsock(td, s, &cap_recv_rights, &fp);
1958 	if (error != 0)
1959 		return (error);
1960 	datagrams = 0;
1961 	while (datagrams < vlen) {
1962 		error = linux_recvmsg_common(td, s, &msg->msg_hdr,
1963 		    flags & ~LINUX_MSG_WAITFORONE, &bsd_msg);
1964 		if (error != 0)
1965 			break;
1966 
1967 		retval = td->td_retval[0];
1968 		error = copyout(&retval, &msg->msg_len, sizeof(msg->msg_len));
1969 		if (error != 0)
1970 			break;
1971 		++msg;
1972 		++datagrams;
1973 
1974 		/*
1975 		 * MSG_WAITFORONE turns on MSG_DONTWAIT after one packet.
1976 		 */
1977 		if (flags & LINUX_MSG_WAITFORONE)
1978 			flags |= LINUX_MSG_DONTWAIT;
1979 
1980 		/*
1981 		 * See BUGS section of recvmmsg(2).
1982 		 */
1983 		if (tts) {
1984 			getnanotime(&ts);
1985 			timespecsub(&ts, tts, &ts);
1986 			if (!timespecisset(&ts) || ts.tv_sec > 0)
1987 				break;
1988 		}
1989 		/* Out of band data, return right away. */
1990 		if (bsd_msg.msg_flags & MSG_OOB)
1991 			break;
1992 	}
1993 	if (error == 0)
1994 		td->td_retval[0] = datagrams;
1995 	fdrop(fp, td);
1996 	return (error);
1997 }
1998 
1999 int
2000 linux_recvmmsg(struct thread *td, struct linux_recvmmsg_args *args)
2001 {
2002 	struct timespec ts, tts, *ptts;
2003 	int error;
2004 
2005 	if (args->timeout) {
2006 		error = linux_get_timespec(&ts, args->timeout);
2007 		if (error != 0)
2008 			return (error);
2009 		getnanotime(&tts);
2010 		timespecadd(&tts, &ts, &tts);
2011 		ptts = &tts;
2012 	}
2013 		else ptts = NULL;
2014 
2015 	return (linux_recvmmsg_common(td, args->s, PTRIN(args->msg),
2016 	    args->vlen, args->flags, ptts));
2017 }
2018 
2019 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
2020 int
2021 linux_recvmmsg_time64(struct thread *td, struct linux_recvmmsg_time64_args *args)
2022 {
2023 	struct timespec ts, tts, *ptts;
2024 	int error;
2025 
2026 	if (args->timeout) {
2027 		error = linux_get_timespec64(&ts, args->timeout);
2028 		if (error != 0)
2029 			return (error);
2030 		getnanotime(&tts);
2031 		timespecadd(&tts, &ts, &tts);
2032 		ptts = &tts;
2033 	}
2034 		else ptts = NULL;
2035 
2036 	return (linux_recvmmsg_common(td, args->s, PTRIN(args->msg),
2037 	    args->vlen, args->flags, ptts));
2038 }
2039 #endif
2040 
2041 int
2042 linux_shutdown(struct thread *td, struct linux_shutdown_args *args)
2043 {
2044 
2045 	return (kern_shutdown(td, args->s, args->how));
2046 }
2047 
2048 int
2049 linux_setsockopt(struct thread *td, struct linux_setsockopt_args *args)
2050 {
2051 	struct proc *p = td->td_proc;
2052 	struct linux_pemuldata *pem;
2053 	l_timeval linux_tv;
2054 	struct sockaddr *sa;
2055 	struct timeval tv;
2056 	socklen_t len;
2057 	int error, level, name, val;
2058 
2059 	level = linux_to_bsd_sockopt_level(args->level);
2060 	switch (level) {
2061 	case SOL_SOCKET:
2062 		name = linux_to_bsd_so_sockopt(args->optname);
2063 		switch (name) {
2064 		case LOCAL_CREDS_PERSISTENT:
2065 			level = SOL_LOCAL;
2066 			break;
2067 		case SO_RCVTIMEO:
2068 			/* FALLTHROUGH */
2069 		case SO_SNDTIMEO:
2070 			error = copyin(PTRIN(args->optval), &linux_tv,
2071 			    sizeof(linux_tv));
2072 			if (error != 0)
2073 				return (error);
2074 			tv.tv_sec = linux_tv.tv_sec;
2075 			tv.tv_usec = linux_tv.tv_usec;
2076 			return (kern_setsockopt(td, args->s, level,
2077 			    name, &tv, UIO_SYSSPACE, sizeof(tv)));
2078 			/* NOTREACHED */
2079 		case SO_TIMESTAMP:
2080 			/* overwrite SO_BINTIME */
2081 			val = 0;
2082 			error = kern_setsockopt(td, args->s, level,
2083 			    SO_BINTIME, &val, UIO_SYSSPACE, sizeof(val));
2084 			if (error != 0)
2085 				return (error);
2086 			pem = pem_find(p);
2087 			pem->so_timestamp = args->optname;
2088 			break;
2089 		case SO_BINTIME:
2090 			/* overwrite SO_TIMESTAMP */
2091 			val = 0;
2092 			error = kern_setsockopt(td, args->s, level,
2093 			    SO_TIMESTAMP, &val, UIO_SYSSPACE, sizeof(val));
2094 			if (error != 0)
2095 				return (error);
2096 			pem = pem_find(p);
2097 			pem->so_timestampns = args->optname;
2098 			break;
2099 		default:
2100 			break;
2101 		}
2102 		break;
2103 	case IPPROTO_IP:
2104 		if (args->optname == LINUX_IP_RECVERR &&
2105 		    linux_ignore_ip_recverr) {
2106 			/*
2107 			 * XXX: This is a hack to unbreak DNS resolution
2108 			 *	with glibc 2.30 and above.
2109 			 */
2110 			return (0);
2111 		}
2112 		name = linux_to_bsd_ip_sockopt(args->optname);
2113 		break;
2114 	case IPPROTO_IPV6:
2115 		name = linux_to_bsd_ip6_sockopt(args->optname);
2116 		break;
2117 	case IPPROTO_TCP:
2118 		name = linux_to_bsd_tcp_sockopt(args->optname);
2119 		break;
2120 	case SOL_NETLINK:
2121 		name = args->optname;
2122 		break;
2123 	default:
2124 		name = -1;
2125 		break;
2126 	}
2127 	if (name < 0) {
2128 		if (name == -1)
2129 			linux_msg(curthread,
2130 			    "unsupported setsockopt level %d optname %d",
2131 			    args->level, args->optname);
2132 		return (ENOPROTOOPT);
2133 	}
2134 
2135 	if (name == IPV6_NEXTHOP) {
2136 		len = args->optlen;
2137 		error = linux_to_bsd_sockaddr(PTRIN(args->optval), &sa, &len);
2138 		if (error != 0)
2139 			return (error);
2140 
2141 		error = kern_setsockopt(td, args->s, level,
2142 		    name, sa, UIO_SYSSPACE, len);
2143 		free(sa, M_SONAME);
2144 	} else {
2145 		error = kern_setsockopt(td, args->s, level,
2146 		    name, PTRIN(args->optval), UIO_USERSPACE, args->optlen);
2147 	}
2148 
2149 	return (error);
2150 }
2151 
2152 static int
2153 linux_sockopt_copyout(struct thread *td, void *val, socklen_t len,
2154     struct linux_getsockopt_args *args)
2155 {
2156 	int error;
2157 
2158 	error = copyout(val, PTRIN(args->optval), len);
2159 	if (error == 0)
2160 		error = copyout(&len, PTRIN(args->optlen), sizeof(len));
2161 	return (error);
2162 }
2163 
2164 static int
2165 linux_getsockopt_so_peergroups(struct thread *td,
2166     struct linux_getsockopt_args *args)
2167 {
2168 	struct xucred xu;
2169 	socklen_t xulen, len;
2170 	int error, i;
2171 
2172 	xulen = sizeof(xu);
2173 	error = kern_getsockopt(td, args->s, 0,
2174 	    LOCAL_PEERCRED, &xu, UIO_SYSSPACE, &xulen);
2175 	if (error != 0)
2176 		return (error);
2177 
2178 	len = xu.cr_ngroups * sizeof(l_gid_t);
2179 	if (args->optlen < len) {
2180 		error = copyout(&len, PTRIN(args->optlen), sizeof(len));
2181 		if (error == 0)
2182 			error = ERANGE;
2183 		return (error);
2184 	}
2185 
2186 	/*
2187 	 * "- 1" to skip the primary group.
2188 	 */
2189 	for (i = 0; i < xu.cr_ngroups - 1; i++) {
2190 		error = copyout(xu.cr_groups + i + 1,
2191 		    (void *)(args->optval + i * sizeof(l_gid_t)),
2192 		    sizeof(l_gid_t));
2193 		if (error != 0)
2194 			return (error);
2195 	}
2196 
2197 	error = copyout(&len, PTRIN(args->optlen), sizeof(len));
2198 	return (error);
2199 }
2200 
2201 static int
2202 linux_getsockopt_so_peersec(struct thread *td,
2203     struct linux_getsockopt_args *args)
2204 {
2205 	socklen_t len;
2206 	int error;
2207 
2208 	len = sizeof(SECURITY_CONTEXT_STRING);
2209 	if (args->optlen < len) {
2210 		error = copyout(&len, PTRIN(args->optlen), sizeof(len));
2211 		if (error == 0)
2212 			error = ERANGE;
2213 		return (error);
2214 	}
2215 
2216 	return (linux_sockopt_copyout(td, SECURITY_CONTEXT_STRING,
2217 	    len, args));
2218 }
2219 
2220 static int
2221 linux_getsockopt_so_linger(struct thread *td,
2222     struct linux_getsockopt_args *args)
2223 {
2224 	struct linger ling;
2225 	socklen_t len;
2226 	int error;
2227 
2228 	len = sizeof(ling);
2229 	error = kern_getsockopt(td, args->s, SOL_SOCKET,
2230 	    SO_LINGER, &ling, UIO_SYSSPACE, &len);
2231 	if (error != 0)
2232 		return (error);
2233 	ling.l_onoff = ((ling.l_onoff & SO_LINGER) != 0);
2234 	return (linux_sockopt_copyout(td, &ling, len, args));
2235 }
2236 
2237 int
2238 linux_getsockopt(struct thread *td, struct linux_getsockopt_args *args)
2239 {
2240 	l_timeval linux_tv;
2241 	struct timeval tv;
2242 	socklen_t tv_len, xulen, len;
2243 	struct sockaddr *sa;
2244 	struct xucred xu;
2245 	struct l_ucred lxu;
2246 	int error, level, name, newval;
2247 
2248 	level = linux_to_bsd_sockopt_level(args->level);
2249 	switch (level) {
2250 	case SOL_SOCKET:
2251 		switch (args->optname) {
2252 		case LINUX_SO_PEERGROUPS:
2253 			return (linux_getsockopt_so_peergroups(td, args));
2254 		case LINUX_SO_PEERSEC:
2255 			return (linux_getsockopt_so_peersec(td, args));
2256 		default:
2257 			break;
2258 		}
2259 
2260 		name = linux_to_bsd_so_sockopt(args->optname);
2261 		switch (name) {
2262 		case LOCAL_CREDS_PERSISTENT:
2263 			level = SOL_LOCAL;
2264 			break;
2265 		case SO_RCVTIMEO:
2266 			/* FALLTHROUGH */
2267 		case SO_SNDTIMEO:
2268 			tv_len = sizeof(tv);
2269 			error = kern_getsockopt(td, args->s, level,
2270 			    name, &tv, UIO_SYSSPACE, &tv_len);
2271 			if (error != 0)
2272 				return (error);
2273 			linux_tv.tv_sec = tv.tv_sec;
2274 			linux_tv.tv_usec = tv.tv_usec;
2275 			return (linux_sockopt_copyout(td, &linux_tv,
2276 			    sizeof(linux_tv), args));
2277 			/* NOTREACHED */
2278 		case LOCAL_PEERCRED:
2279 			if (args->optlen < sizeof(lxu))
2280 				return (EINVAL);
2281 			/*
2282 			 * LOCAL_PEERCRED is not served at the SOL_SOCKET level,
2283 			 * but by the Unix socket's level 0.
2284 			 */
2285 			level = 0;
2286 			xulen = sizeof(xu);
2287 			error = kern_getsockopt(td, args->s, level,
2288 			    name, &xu, UIO_SYSSPACE, &xulen);
2289 			if (error != 0)
2290 				return (error);
2291 			lxu.pid = xu.cr_pid;
2292 			lxu.uid = xu.cr_uid;
2293 			lxu.gid = xu.cr_gid;
2294 			return (linux_sockopt_copyout(td, &lxu,
2295 			    sizeof(lxu), args));
2296 			/* NOTREACHED */
2297 		case SO_ERROR:
2298 			len = sizeof(newval);
2299 			error = kern_getsockopt(td, args->s, level,
2300 			    name, &newval, UIO_SYSSPACE, &len);
2301 			if (error != 0)
2302 				return (error);
2303 			newval = -bsd_to_linux_errno(newval);
2304 			return (linux_sockopt_copyout(td, &newval,
2305 			    len, args));
2306 			/* NOTREACHED */
2307 		case SO_DOMAIN:
2308 			len = sizeof(newval);
2309 			error = kern_getsockopt(td, args->s, level,
2310 			    name, &newval, UIO_SYSSPACE, &len);
2311 			if (error != 0)
2312 				return (error);
2313 			newval = bsd_to_linux_domain((sa_family_t)newval);
2314 			if (newval == AF_UNKNOWN)
2315 				return (ENOPROTOOPT);
2316 			return (linux_sockopt_copyout(td, &newval,
2317 			    len, args));
2318 			/* NOTREACHED */
2319 		case SO_LINGER:
2320 			return (linux_getsockopt_so_linger(td, args));
2321 			/* NOTREACHED */
2322 		default:
2323 			break;
2324 		}
2325 		break;
2326 	case IPPROTO_IP:
2327 		name = linux_to_bsd_ip_sockopt(args->optname);
2328 		break;
2329 	case IPPROTO_IPV6:
2330 		name = linux_to_bsd_ip6_sockopt(args->optname);
2331 		break;
2332 	case IPPROTO_TCP:
2333 		name = linux_to_bsd_tcp_sockopt(args->optname);
2334 		break;
2335 	default:
2336 		name = -1;
2337 		break;
2338 	}
2339 	if (name < 0) {
2340 		if (name == -1)
2341 			linux_msg(curthread,
2342 			    "unsupported getsockopt level %d optname %d",
2343 			    args->level, args->optname);
2344 		return (EINVAL);
2345 	}
2346 
2347 	if (name == IPV6_NEXTHOP) {
2348 		error = copyin(PTRIN(args->optlen), &len, sizeof(len));
2349                 if (error != 0)
2350                         return (error);
2351 		sa = malloc(len, M_SONAME, M_WAITOK);
2352 
2353 		error = kern_getsockopt(td, args->s, level,
2354 		    name, sa, UIO_SYSSPACE, &len);
2355 		if (error != 0)
2356 			goto out;
2357 
2358 		error = linux_copyout_sockaddr(sa, PTRIN(args->optval), len);
2359 		if (error == 0)
2360 			error = copyout(&len, PTRIN(args->optlen),
2361 			    sizeof(len));
2362 out:
2363 		free(sa, M_SONAME);
2364 	} else {
2365 		if (args->optval) {
2366 			error = copyin(PTRIN(args->optlen), &len, sizeof(len));
2367 			if (error != 0)
2368 				return (error);
2369 		}
2370 		error = kern_getsockopt(td, args->s, level,
2371 		    name, PTRIN(args->optval), UIO_USERSPACE, &len);
2372 		if (error == 0)
2373 			error = copyout(&len, PTRIN(args->optlen),
2374 			    sizeof(len));
2375 	}
2376 
2377 	return (error);
2378 }
2379 
2380 /*
2381  * Based on sendfile_getsock from kern_sendfile.c
2382  * Determines whether an fd is a stream socket that can be used
2383  * with FreeBSD sendfile.
2384  */
2385 static bool
2386 is_sendfile(struct file *fp, struct file *ofp)
2387 {
2388 	struct socket *so;
2389 
2390 	/*
2391 	 * FreeBSD sendfile() system call sends a regular file or
2392 	 * shared memory object out a stream socket.
2393 	 */
2394 	if ((fp->f_type != DTYPE_SHM && fp->f_type != DTYPE_VNODE) ||
2395 	    (fp->f_type == DTYPE_VNODE &&
2396 	    (fp->f_vnode == NULL || fp->f_vnode->v_type != VREG)))
2397 		return (false);
2398 	/*
2399 	 * The socket must be a stream socket and connected.
2400 	 */
2401 	if (ofp->f_type != DTYPE_SOCKET)
2402 		return (false);
2403 	so = ofp->f_data;
2404 	if (so->so_type != SOCK_STREAM)
2405 		return (false);
2406 	/*
2407 	 * SCTP one-to-one style sockets currently don't work with
2408 	 * sendfile().
2409 	 */
2410 	if (so->so_proto->pr_protocol == IPPROTO_SCTP)
2411 		return (false);
2412 	return (!SOLISTENING(so));
2413 }
2414 
2415 static bool
2416 is_regular_file(struct file *fp)
2417 {
2418 
2419 	return (fp->f_type == DTYPE_VNODE && fp->f_vnode != NULL &&
2420 	    fp->f_vnode->v_type == VREG);
2421 }
2422 
2423 static int
2424 sendfile_fallback(struct thread *td, struct file *fp, l_int out,
2425     off_t *offset, l_size_t count, off_t *sbytes)
2426 {
2427 	off_t current_offset, out_offset, to_send;
2428 	l_size_t bytes_sent, n_read;
2429 	struct file *ofp;
2430 	struct iovec aiov;
2431 	struct uio auio;
2432 	bool seekable;
2433 	size_t bufsz;
2434 	void *buf;
2435 	int flags, error;
2436 
2437 	if (offset == NULL) {
2438 		if ((error = fo_seek(fp, 0, SEEK_CUR, td)) != 0)
2439 			return (error);
2440 		current_offset = td->td_uretoff.tdu_off;
2441 	} else {
2442 		if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0)
2443 			return (ESPIPE);
2444 		current_offset = *offset;
2445 	}
2446 	error = fget_write(td, out, &cap_pwrite_rights, &ofp);
2447 	if (error != 0)
2448 		return (error);
2449 	seekable = (ofp->f_ops->fo_flags & DFLAG_SEEKABLE) != 0;
2450 	if (seekable) {
2451 		if ((error = fo_seek(ofp, 0, SEEK_CUR, td)) != 0)
2452 			goto drop;
2453 		out_offset = td->td_uretoff.tdu_off;
2454 	} else
2455 		out_offset = 0;
2456 
2457 	flags = FOF_OFFSET | FOF_NOUPDATE;
2458 	bufsz = min(count, MAXPHYS);
2459 	buf = malloc(bufsz, M_LINUX, M_WAITOK);
2460 	bytes_sent = 0;
2461 	while (bytes_sent < count) {
2462 		to_send = min(count - bytes_sent, bufsz);
2463 		aiov.iov_base = buf;
2464 		aiov.iov_len = bufsz;
2465 		auio.uio_iov = &aiov;
2466 		auio.uio_iovcnt = 1;
2467 		auio.uio_segflg = UIO_SYSSPACE;
2468 		auio.uio_td = td;
2469 		auio.uio_rw = UIO_READ;
2470 		auio.uio_offset = current_offset;
2471 		auio.uio_resid = to_send;
2472 		error = fo_read(fp, &auio, fp->f_cred, flags, td);
2473 		if (error != 0)
2474 			break;
2475 		n_read = to_send - auio.uio_resid;
2476 		if (n_read == 0)
2477 			break;
2478 		aiov.iov_base = buf;
2479 		aiov.iov_len = bufsz;
2480 		auio.uio_iov = &aiov;
2481 		auio.uio_iovcnt = 1;
2482 		auio.uio_segflg = UIO_SYSSPACE;
2483 		auio.uio_td = td;
2484 		auio.uio_rw = UIO_WRITE;
2485 		auio.uio_offset = (seekable) ? out_offset : 0;
2486 		auio.uio_resid = n_read;
2487 		error = fo_write(ofp, &auio, ofp->f_cred, flags, td);
2488 		if (error != 0)
2489 			break;
2490 		bytes_sent += n_read;
2491 		current_offset += n_read;
2492 		out_offset += n_read;
2493 	}
2494 	free(buf, M_LINUX);
2495 
2496 	if (error == 0) {
2497 		*sbytes = bytes_sent;
2498 		if (offset != NULL)
2499 			*offset = current_offset;
2500 		else
2501 			error = fo_seek(fp, current_offset, SEEK_SET, td);
2502 	}
2503 	if (error == 0 && seekable)
2504 		error = fo_seek(ofp, out_offset, SEEK_SET, td);
2505 
2506 drop:
2507 	fdrop(ofp, td);
2508 	return (error);
2509 }
2510 
2511 static int
2512 sendfile_sendfile(struct thread *td, struct file *fp, l_int out,
2513     off_t *offset, l_size_t count, off_t *sbytes)
2514 {
2515 	off_t current_offset;
2516 	int error;
2517 
2518 	if (offset == NULL) {
2519 		if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0)
2520 			return (ESPIPE);
2521 		if ((error = fo_seek(fp, 0, SEEK_CUR, td)) != 0)
2522 			return (error);
2523 		current_offset = td->td_uretoff.tdu_off;
2524 	} else
2525 		current_offset = *offset;
2526 	error = fo_sendfile(fp, out, NULL, NULL, current_offset, count,
2527 	    sbytes, 0, td);
2528 	if (error == 0) {
2529 		current_offset += *sbytes;
2530 		if (offset != NULL)
2531 			*offset = current_offset;
2532 		else
2533 			error = fo_seek(fp, current_offset, SEEK_SET, td);
2534 	}
2535 	return (error);
2536 }
2537 
2538 static int
2539 linux_sendfile_common(struct thread *td, l_int out, l_int in,
2540     off_t *offset, l_size_t count)
2541 {
2542 	struct file *fp, *ofp;
2543 	off_t sbytes;
2544 	int error;
2545 
2546 	/* Linux cannot have 0 count. */
2547 	if (count <= 0 || (offset != NULL && *offset < 0))
2548 		return (EINVAL);
2549 
2550 	AUDIT_ARG_FD(in);
2551 	error = fget_read(td, in, &cap_pread_rights, &fp);
2552 	if (error != 0)
2553 		return (error);
2554 	if ((fp->f_type != DTYPE_SHM && fp->f_type != DTYPE_VNODE) ||
2555 	    (fp->f_type == DTYPE_VNODE &&
2556 	    (fp->f_vnode == NULL || fp->f_vnode->v_type != VREG))) {
2557 		error = EINVAL;
2558 		goto drop;
2559 	}
2560 	error = fget_unlocked(td, out, &cap_no_rights, &ofp);
2561 	if (error != 0)
2562 		goto drop;
2563 
2564 	if (is_regular_file(fp) && is_regular_file(ofp)) {
2565 		error = kern_copy_file_range(td, in, offset, out, NULL, count,
2566 		    0);
2567 	} else {
2568 		sbytes = 0;
2569 		if (is_sendfile(fp, ofp))
2570 			error = sendfile_sendfile(td, fp, out, offset, count,
2571 			    &sbytes);
2572 		else
2573 			error = sendfile_fallback(td, fp, out, offset, count,
2574 			    &sbytes);
2575 		if (error == ENOBUFS && (ofp->f_flag & FNONBLOCK) != 0)
2576 			error = EAGAIN;
2577 		if (error == 0)
2578 			td->td_retval[0] = sbytes;
2579 	}
2580 	fdrop(ofp, td);
2581 
2582 drop:
2583 	fdrop(fp, td);
2584 	return (error);
2585 }
2586 
2587 int
2588 linux_sendfile(struct thread *td, struct linux_sendfile_args *arg)
2589 {
2590 	/*
2591 	 * Differences between FreeBSD and Linux sendfile:
2592 	 * - Linux doesn't send anything when count is 0 (FreeBSD uses 0 to
2593 	 *   mean send the whole file).
2594 	 * - Linux can send to any fd whereas FreeBSD only supports sockets.
2595 	 *   We therefore use FreeBSD sendfile where possible for performance,
2596 	 *   but fall back on a manual copy (sendfile_fallback).
2597 	 * - Linux doesn't have an equivalent for FreeBSD's flags and sf_hdtr.
2598 	 * - Linux takes an offset pointer and updates it to the read location.
2599 	 *   FreeBSD takes in an offset and a 'bytes read' parameter which is
2600 	 *   only filled if it isn't NULL.  We use this parameter to update the
2601 	 *   offset pointer if it exists.
2602 	 * - Linux sendfile returns bytes read on success while FreeBSD
2603 	 *   returns 0.  We use the 'bytes read' parameter to get this value.
2604 	 */
2605 
2606 	off_t offset64;
2607 	l_off_t offset;
2608 	int error;
2609 
2610 	if (arg->offset != NULL) {
2611 		error = copyin(arg->offset, &offset, sizeof(offset));
2612 		if (error != 0)
2613 			return (error);
2614 		offset64 = offset;
2615 	}
2616 
2617 	error = linux_sendfile_common(td, arg->out, arg->in,
2618 	    arg->offset != NULL ? &offset64 : NULL, arg->count);
2619 
2620 	if (error == 0 && arg->offset != NULL) {
2621 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
2622 		if (offset64 > INT32_MAX)
2623 			return (EOVERFLOW);
2624 #endif
2625 		offset = (l_off_t)offset64;
2626 		error = copyout(&offset, arg->offset, sizeof(offset));
2627 	}
2628 
2629 	return (error);
2630 }
2631 
2632 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
2633 int
2634 linux_sendfile64(struct thread *td, struct linux_sendfile64_args *arg)
2635 {
2636 	off_t offset;
2637 	int error;
2638 
2639 	if (arg->offset != NULL) {
2640 		error = copyin(arg->offset, &offset, sizeof(offset));
2641 		if (error != 0)
2642 			return (error);
2643 	}
2644 
2645 	error = linux_sendfile_common(td, arg->out, arg->in,
2646 		arg->offset != NULL ? &offset : NULL, arg->count);
2647 
2648 	if (error == 0 && arg->offset != NULL)
2649 		error = copyout(&offset, arg->offset, sizeof(offset));
2650 
2651 	return (error);
2652 }
2653 
2654 /* Argument list sizes for linux_socketcall */
2655 static const unsigned char lxs_args_cnt[] = {
2656 	0 /* unused*/,		3 /* socket */,
2657 	3 /* bind */,		3 /* connect */,
2658 	2 /* listen */,		3 /* accept */,
2659 	3 /* getsockname */,	3 /* getpeername */,
2660 	4 /* socketpair */,	4 /* send */,
2661 	4 /* recv */,		6 /* sendto */,
2662 	6 /* recvfrom */,	2 /* shutdown */,
2663 	5 /* setsockopt */,	5 /* getsockopt */,
2664 	3 /* sendmsg */,	3 /* recvmsg */,
2665 	4 /* accept4 */,	5 /* recvmmsg */,
2666 	4 /* sendmmsg */,	4 /* sendfile */
2667 };
2668 #define	LINUX_ARGS_CNT		(nitems(lxs_args_cnt) - 1)
2669 #define	LINUX_ARG_SIZE(x)	(lxs_args_cnt[x] * sizeof(l_ulong))
2670 
2671 int
2672 linux_socketcall(struct thread *td, struct linux_socketcall_args *args)
2673 {
2674 	l_ulong a[6];
2675 #if defined(__amd64__) && defined(COMPAT_LINUX32)
2676 	register_t l_args[6];
2677 #endif
2678 	void *arg;
2679 	int error;
2680 
2681 	if (args->what < LINUX_SOCKET || args->what > LINUX_ARGS_CNT)
2682 		return (EINVAL);
2683 	error = copyin(PTRIN(args->args), a, LINUX_ARG_SIZE(args->what));
2684 	if (error != 0)
2685 		return (error);
2686 
2687 #if defined(__amd64__) && defined(COMPAT_LINUX32)
2688 	for (int i = 0; i < lxs_args_cnt[args->what]; ++i)
2689 		l_args[i] = a[i];
2690 	arg = l_args;
2691 #else
2692 	arg = a;
2693 #endif
2694 	switch (args->what) {
2695 	case LINUX_SOCKET:
2696 		return (linux_socket(td, arg));
2697 	case LINUX_BIND:
2698 		return (linux_bind(td, arg));
2699 	case LINUX_CONNECT:
2700 		return (linux_connect(td, arg));
2701 	case LINUX_LISTEN:
2702 		return (linux_listen(td, arg));
2703 	case LINUX_ACCEPT:
2704 		return (linux_accept(td, arg));
2705 	case LINUX_GETSOCKNAME:
2706 		return (linux_getsockname(td, arg));
2707 	case LINUX_GETPEERNAME:
2708 		return (linux_getpeername(td, arg));
2709 	case LINUX_SOCKETPAIR:
2710 		return (linux_socketpair(td, arg));
2711 	case LINUX_SEND:
2712 		return (linux_send(td, arg));
2713 	case LINUX_RECV:
2714 		return (linux_recv(td, arg));
2715 	case LINUX_SENDTO:
2716 		return (linux_sendto(td, arg));
2717 	case LINUX_RECVFROM:
2718 		return (linux_recvfrom(td, arg));
2719 	case LINUX_SHUTDOWN:
2720 		return (linux_shutdown(td, arg));
2721 	case LINUX_SETSOCKOPT:
2722 		return (linux_setsockopt(td, arg));
2723 	case LINUX_GETSOCKOPT:
2724 		return (linux_getsockopt(td, arg));
2725 	case LINUX_SENDMSG:
2726 		return (linux_sendmsg(td, arg));
2727 	case LINUX_RECVMSG:
2728 		return (linux_recvmsg(td, arg));
2729 	case LINUX_ACCEPT4:
2730 		return (linux_accept4(td, arg));
2731 	case LINUX_RECVMMSG:
2732 		return (linux_recvmmsg(td, arg));
2733 	case LINUX_SENDMMSG:
2734 		return (linux_sendmmsg(td, arg));
2735 	case LINUX_SENDFILE:
2736 		return (linux_sendfile(td, arg));
2737 	}
2738 
2739 	linux_msg(td, "socket type %d not implemented", args->what);
2740 	return (ENOSYS);
2741 }
2742 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
2743