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