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