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