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