xref: /freebsd/sys/compat/linux/linux_socket.c (revision d5be41beb7c44119730791d92782d8e77174d312)
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_MD5SIG:
578 		return (TCP_MD5SIG);
579 	}
580 	return (-1);
581 }
582 
583 static int
584 linux_to_bsd_msg_flags(int flags)
585 {
586 	int ret_flags = 0;
587 
588 	if (flags & LINUX_MSG_OOB)
589 		ret_flags |= MSG_OOB;
590 	if (flags & LINUX_MSG_PEEK)
591 		ret_flags |= MSG_PEEK;
592 	if (flags & LINUX_MSG_DONTROUTE)
593 		ret_flags |= MSG_DONTROUTE;
594 	if (flags & LINUX_MSG_CTRUNC)
595 		ret_flags |= MSG_CTRUNC;
596 	if (flags & LINUX_MSG_TRUNC)
597 		ret_flags |= MSG_TRUNC;
598 	if (flags & LINUX_MSG_DONTWAIT)
599 		ret_flags |= MSG_DONTWAIT;
600 	if (flags & LINUX_MSG_EOR)
601 		ret_flags |= MSG_EOR;
602 	if (flags & LINUX_MSG_WAITALL)
603 		ret_flags |= MSG_WAITALL;
604 	if (flags & LINUX_MSG_NOSIGNAL)
605 		ret_flags |= MSG_NOSIGNAL;
606 	if (flags & LINUX_MSG_PROXY)
607 		LINUX_RATELIMIT_MSG_OPT1("socket message flag MSG_PROXY (%d) not handled",
608 		    LINUX_MSG_PROXY);
609 	if (flags & LINUX_MSG_FIN)
610 		LINUX_RATELIMIT_MSG_OPT1("socket message flag MSG_FIN (%d) not handled",
611 		    LINUX_MSG_FIN);
612 	if (flags & LINUX_MSG_SYN)
613 		LINUX_RATELIMIT_MSG_OPT1("socket message flag MSG_SYN (%d) not handled",
614 		    LINUX_MSG_SYN);
615 	if (flags & LINUX_MSG_CONFIRM)
616 		LINUX_RATELIMIT_MSG_OPT1("socket message flag MSG_CONFIRM (%d) not handled",
617 		    LINUX_MSG_CONFIRM);
618 	if (flags & LINUX_MSG_RST)
619 		LINUX_RATELIMIT_MSG_OPT1("socket message flag MSG_RST (%d) not handled",
620 		    LINUX_MSG_RST);
621 	if (flags & LINUX_MSG_ERRQUEUE)
622 		LINUX_RATELIMIT_MSG_OPT1("socket message flag MSG_ERRQUEUE (%d) not handled",
623 		    LINUX_MSG_ERRQUEUE);
624 	return (ret_flags);
625 }
626 
627 static int
628 linux_to_bsd_cmsg_type(int cmsg_type)
629 {
630 
631 	switch (cmsg_type) {
632 	case LINUX_SCM_RIGHTS:
633 		return (SCM_RIGHTS);
634 	case LINUX_SCM_CREDENTIALS:
635 		return (SCM_CREDS);
636 	}
637 	return (-1);
638 }
639 
640 static int
641 bsd_to_linux_cmsg_type(int cmsg_type)
642 {
643 
644 	switch (cmsg_type) {
645 	case SCM_RIGHTS:
646 		return (LINUX_SCM_RIGHTS);
647 	case SCM_CREDS:
648 		return (LINUX_SCM_CREDENTIALS);
649 	case SCM_CREDS2:
650 		return (LINUX_SCM_CREDENTIALS);
651 	case SCM_TIMESTAMP:
652 		return (LINUX_SCM_TIMESTAMP);
653 	}
654 	return (-1);
655 }
656 
657 static int
658 linux_to_bsd_msghdr(struct msghdr *bhdr, const struct l_msghdr *lhdr)
659 {
660 	if (lhdr->msg_controllen > INT_MAX)
661 		return (ENOBUFS);
662 
663 	bhdr->msg_name		= PTRIN(lhdr->msg_name);
664 	bhdr->msg_namelen	= lhdr->msg_namelen;
665 	bhdr->msg_iov		= PTRIN(lhdr->msg_iov);
666 	bhdr->msg_iovlen	= lhdr->msg_iovlen;
667 	bhdr->msg_control	= PTRIN(lhdr->msg_control);
668 
669 	/*
670 	 * msg_controllen is skipped since BSD and LINUX control messages
671 	 * are potentially different sizes (e.g. the cred structure used
672 	 * by SCM_CREDS is different between the two operating system).
673 	 *
674 	 * The caller can set it (if necessary) after converting all the
675 	 * control messages.
676 	 */
677 
678 	bhdr->msg_flags		= linux_to_bsd_msg_flags(lhdr->msg_flags);
679 	return (0);
680 }
681 
682 static int
683 bsd_to_linux_msghdr(const struct msghdr *bhdr, struct l_msghdr *lhdr)
684 {
685 	lhdr->msg_name		= PTROUT(bhdr->msg_name);
686 	lhdr->msg_namelen	= bhdr->msg_namelen;
687 	lhdr->msg_iov		= PTROUT(bhdr->msg_iov);
688 	lhdr->msg_iovlen	= bhdr->msg_iovlen;
689 	lhdr->msg_control	= PTROUT(bhdr->msg_control);
690 
691 	/*
692 	 * msg_controllen is skipped since BSD and LINUX control messages
693 	 * are potentially different sizes (e.g. the cred structure used
694 	 * by SCM_CREDS is different between the two operating system).
695 	 *
696 	 * The caller can set it (if necessary) after converting all the
697 	 * control messages.
698 	 */
699 
700 	/* msg_flags skipped */
701 	return (0);
702 }
703 
704 static int
705 linux_set_socket_flags(int lflags, int *flags)
706 {
707 
708 	if (lflags & ~(LINUX_SOCK_CLOEXEC | LINUX_SOCK_NONBLOCK))
709 		return (EINVAL);
710 	if (lflags & LINUX_SOCK_NONBLOCK)
711 		*flags |= SOCK_NONBLOCK;
712 	if (lflags & LINUX_SOCK_CLOEXEC)
713 		*flags |= SOCK_CLOEXEC;
714 	return (0);
715 }
716 
717 static int
718 linux_copyout_sockaddr(const struct sockaddr *sa, void *uaddr, size_t len)
719 {
720 	struct l_sockaddr *lsa;
721 	int error;
722 
723 	error = bsd_to_linux_sockaddr(sa, &lsa, len);
724 	if (error != 0)
725 		return (error);
726 
727 	error = copyout(lsa, uaddr, len);
728 	free(lsa, M_SONAME);
729 
730 	return (error);
731 }
732 
733 static int
734 linux_sendit(struct thread *td, int s, struct msghdr *mp, int flags,
735     struct mbuf *control, enum uio_seg segflg)
736 {
737 	struct sockaddr *to;
738 	int error, len;
739 
740 	if (mp->msg_name != NULL) {
741 		len = mp->msg_namelen;
742 		error = linux_to_bsd_sockaddr(mp->msg_name, &to, &len);
743 		if (error != 0)
744 			return (error);
745 		mp->msg_name = to;
746 	} else
747 		to = NULL;
748 
749 	error = kern_sendit(td, s, mp, linux_to_bsd_msg_flags(flags), control,
750 	    segflg);
751 
752 	if (to)
753 		free(to, M_SONAME);
754 	return (error);
755 }
756 
757 /* Return 0 if IP_HDRINCL is set for the given socket. */
758 static int
759 linux_check_hdrincl(struct thread *td, int s)
760 {
761 	int error, optval;
762 	socklen_t size_val;
763 
764 	size_val = sizeof(optval);
765 	error = kern_getsockopt(td, s, IPPROTO_IP, IP_HDRINCL,
766 	    &optval, UIO_SYSSPACE, &size_val);
767 	if (error != 0)
768 		return (error);
769 
770 	return (optval == 0);
771 }
772 
773 /*
774  * Updated sendto() when IP_HDRINCL is set:
775  * tweak endian-dependent fields in the IP packet.
776  */
777 static int
778 linux_sendto_hdrincl(struct thread *td, struct linux_sendto_args *linux_args)
779 {
780 /*
781  * linux_ip_copysize defines how many bytes we should copy
782  * from the beginning of the IP packet before we customize it for BSD.
783  * It should include all the fields we modify (ip_len and ip_off).
784  */
785 #define linux_ip_copysize	8
786 
787 	struct ip *packet;
788 	struct msghdr msg;
789 	struct iovec aiov[1];
790 	int error;
791 
792 	/* Check that the packet isn't too big or too small. */
793 	if (linux_args->len < linux_ip_copysize ||
794 	    linux_args->len > IP_MAXPACKET)
795 		return (EINVAL);
796 
797 	packet = (struct ip *)malloc(linux_args->len, M_LINUX, M_WAITOK);
798 
799 	/* Make kernel copy of the packet to be sent */
800 	if ((error = copyin(PTRIN(linux_args->msg), packet,
801 	    linux_args->len)))
802 		goto goout;
803 
804 	/* Convert fields from Linux to BSD raw IP socket format */
805 	packet->ip_len = linux_args->len;
806 	packet->ip_off = ntohs(packet->ip_off);
807 
808 	/* Prepare the msghdr and iovec structures describing the new packet */
809 	msg.msg_name = PTRIN(linux_args->to);
810 	msg.msg_namelen = linux_args->tolen;
811 	msg.msg_iov = aiov;
812 	msg.msg_iovlen = 1;
813 	msg.msg_control = NULL;
814 	msg.msg_flags = 0;
815 	aiov[0].iov_base = (char *)packet;
816 	aiov[0].iov_len = linux_args->len;
817 	error = linux_sendit(td, linux_args->s, &msg, linux_args->flags,
818 	    NULL, UIO_SYSSPACE);
819 goout:
820 	free(packet, M_LINUX);
821 	return (error);
822 }
823 
824 static const char *linux_netlink_names[] = {
825 	[LINUX_NETLINK_ROUTE] = "ROUTE",
826 	[LINUX_NETLINK_SOCK_DIAG] = "SOCK_DIAG",
827 	[LINUX_NETLINK_NFLOG] = "NFLOG",
828 	[LINUX_NETLINK_SELINUX] = "SELINUX",
829 	[LINUX_NETLINK_AUDIT] = "AUDIT",
830 	[LINUX_NETLINK_FIB_LOOKUP] = "FIB_LOOKUP",
831 	[LINUX_NETLINK_NETFILTER] = "NETFILTER",
832 	[LINUX_NETLINK_KOBJECT_UEVENT] = "KOBJECT_UEVENT",
833 };
834 
835 int
836 linux_socket(struct thread *td, struct linux_socket_args *args)
837 {
838 	int domain, retval_socket, type;
839 
840 	type = args->type & LINUX_SOCK_TYPE_MASK;
841 	if (type < 0 || type > LINUX_SOCK_MAX)
842 		return (EINVAL);
843 	retval_socket = linux_set_socket_flags(args->type & ~LINUX_SOCK_TYPE_MASK,
844 		&type);
845 	if (retval_socket != 0)
846 		return (retval_socket);
847 	domain = linux_to_bsd_domain(args->domain);
848 	if (domain == -1) {
849 		/* Mask off SOCK_NONBLOCK / CLOEXEC for error messages. */
850 		type = args->type & LINUX_SOCK_TYPE_MASK;
851 		if (args->domain == LINUX_AF_NETLINK &&
852 		    args->protocol == LINUX_NETLINK_AUDIT) {
853 			; /* Do nothing, quietly. */
854 		} else if (args->domain == LINUX_AF_NETLINK) {
855 			const char *nl_name;
856 
857 			if (args->protocol >= 0 &&
858 			    args->protocol < nitems(linux_netlink_names))
859 				nl_name = linux_netlink_names[args->protocol];
860 			else
861 				nl_name = NULL;
862 			if (nl_name != NULL)
863 				linux_msg(curthread,
864 				    "unsupported socket(AF_NETLINK, %d, "
865 				    "NETLINK_%s)", type, nl_name);
866 			else
867 				linux_msg(curthread,
868 				    "unsupported socket(AF_NETLINK, %d, %d)",
869 				    type, args->protocol);
870 		} else {
871 			linux_msg(curthread, "unsupported socket domain %d, "
872 			    "type %d, protocol %d", args->domain, type,
873 			    args->protocol);
874 		}
875 		return (EAFNOSUPPORT);
876 	}
877 
878 	retval_socket = kern_socket(td, domain, type, args->protocol);
879 	if (retval_socket)
880 		return (retval_socket);
881 
882 	if (type == SOCK_RAW
883 	    && (args->protocol == IPPROTO_RAW || args->protocol == 0)
884 	    && domain == PF_INET) {
885 		/* It's a raw IP socket: set the IP_HDRINCL option. */
886 		int hdrincl;
887 
888 		hdrincl = 1;
889 		/* We ignore any error returned by kern_setsockopt() */
890 		kern_setsockopt(td, td->td_retval[0], IPPROTO_IP, IP_HDRINCL,
891 		    &hdrincl, UIO_SYSSPACE, sizeof(hdrincl));
892 	}
893 #ifdef INET6
894 	/*
895 	 * Linux AF_INET6 socket has IPV6_V6ONLY setsockopt set to 0 by default
896 	 * and some apps depend on this. So, set V6ONLY to 0 for Linux apps.
897 	 * For simplicity we do this unconditionally of the net.inet6.ip6.v6only
898 	 * sysctl value.
899 	 */
900 	if (domain == PF_INET6) {
901 		int v6only;
902 
903 		v6only = 0;
904 		/* We ignore any error returned by setsockopt() */
905 		kern_setsockopt(td, td->td_retval[0], IPPROTO_IPV6, IPV6_V6ONLY,
906 		    &v6only, UIO_SYSSPACE, sizeof(v6only));
907 	}
908 #endif
909 
910 	return (retval_socket);
911 }
912 
913 int
914 linux_bind(struct thread *td, struct linux_bind_args *args)
915 {
916 	struct sockaddr *sa;
917 	int error;
918 
919 	error = linux_to_bsd_sockaddr(PTRIN(args->name), &sa,
920 	    &args->namelen);
921 	if (error != 0)
922 		return (error);
923 
924 	error = kern_bindat(td, AT_FDCWD, args->s, sa);
925 	free(sa, M_SONAME);
926 
927 	/* XXX */
928 	if (error == EADDRNOTAVAIL && args->namelen != sizeof(struct sockaddr_in))
929 		return (EINVAL);
930 	return (error);
931 }
932 
933 int
934 linux_connect(struct thread *td, struct linux_connect_args *args)
935 {
936 	struct socket *so;
937 	struct sockaddr *sa;
938 	struct file *fp;
939 	u_int fflag;
940 	int error;
941 
942 	error = linux_to_bsd_sockaddr(PTRIN(args->name), &sa,
943 	    &args->namelen);
944 	if (error != 0)
945 		return (error);
946 
947 	error = kern_connectat(td, AT_FDCWD, args->s, sa);
948 	free(sa, M_SONAME);
949 	if (error != EISCONN)
950 		return (error);
951 
952 	/*
953 	 * Linux doesn't return EISCONN the first time it occurs,
954 	 * when on a non-blocking socket. Instead it returns the
955 	 * error getsockopt(SOL_SOCKET, SO_ERROR) would return on BSD.
956 	 */
957 	error = getsock_cap(td, args->s, &cap_connect_rights,
958 	    &fp, &fflag, NULL);
959 	if (error != 0)
960 		return (error);
961 
962 	error = EISCONN;
963 	so = fp->f_data;
964 	if (fflag & FNONBLOCK) {
965 		SOCK_LOCK(so);
966 		if (so->so_emuldata == 0)
967 			error = so->so_error;
968 		so->so_emuldata = (void *)1;
969 		SOCK_UNLOCK(so);
970 	}
971 	fdrop(fp, td);
972 
973 	return (error);
974 }
975 
976 int
977 linux_listen(struct thread *td, struct linux_listen_args *args)
978 {
979 
980 	return (kern_listen(td, args->s, args->backlog));
981 }
982 
983 static int
984 linux_accept_common(struct thread *td, int s, l_uintptr_t addr,
985     l_uintptr_t namelen, int flags)
986 {
987 	struct sockaddr *sa;
988 	struct file *fp, *fp1;
989 	int bflags, len;
990 	struct socket *so;
991 	int error, error1;
992 
993 	bflags = 0;
994 	fp = NULL;
995 	sa = NULL;
996 
997 	error = linux_set_socket_flags(flags, &bflags);
998 	if (error != 0)
999 		return (error);
1000 
1001 	if (PTRIN(addr) == NULL) {
1002 		len = 0;
1003 		error = kern_accept4(td, s, NULL, NULL, bflags, NULL);
1004 	} else {
1005 		error = copyin(PTRIN(namelen), &len, sizeof(len));
1006 		if (error != 0)
1007 			return (error);
1008 		if (len < 0)
1009 			return (EINVAL);
1010 		error = kern_accept4(td, s, &sa, &len, bflags, &fp);
1011 	}
1012 
1013 	/*
1014 	 * Translate errno values into ones used by Linux.
1015 	 */
1016 	if (error != 0) {
1017 		/*
1018 		 * XXX. This is wrong, different sockaddr structures
1019 		 * have different sizes.
1020 		 */
1021 		switch (error) {
1022 		case EFAULT:
1023 			if (namelen != sizeof(struct sockaddr_in))
1024 				error = EINVAL;
1025 			break;
1026 		case EINVAL:
1027 			error1 = getsock_cap(td, s, &cap_accept_rights, &fp1, NULL, NULL);
1028 			if (error1 != 0) {
1029 				error = error1;
1030 				break;
1031 			}
1032 			so = fp1->f_data;
1033 			if (so->so_type == SOCK_DGRAM)
1034 				error = EOPNOTSUPP;
1035 			fdrop(fp1, td);
1036 			break;
1037 		}
1038 		return (error);
1039 	}
1040 
1041 	if (len != 0) {
1042 		error = linux_copyout_sockaddr(sa, PTRIN(addr), len);
1043 
1044 		/*
1045 		 * XXX: We should also copyout the len, shouldn't we?
1046 		 */
1047 
1048 		if (error != 0) {
1049 			fdclose(td, fp, td->td_retval[0]);
1050 			td->td_retval[0] = 0;
1051 		}
1052 	}
1053 	if (fp != NULL)
1054 		fdrop(fp, td);
1055 	free(sa, M_SONAME);
1056 	return (error);
1057 }
1058 
1059 int
1060 linux_accept(struct thread *td, struct linux_accept_args *args)
1061 {
1062 
1063 	return (linux_accept_common(td, args->s, args->addr,
1064 	    args->namelen, 0));
1065 }
1066 
1067 int
1068 linux_accept4(struct thread *td, struct linux_accept4_args *args)
1069 {
1070 
1071 	return (linux_accept_common(td, args->s, args->addr,
1072 	    args->namelen, args->flags));
1073 }
1074 
1075 int
1076 linux_getsockname(struct thread *td, struct linux_getsockname_args *args)
1077 {
1078 	struct sockaddr *sa;
1079 	int len, error;
1080 
1081 	error = copyin(PTRIN(args->namelen), &len, sizeof(len));
1082 	if (error != 0)
1083 		return (error);
1084 
1085 	error = kern_getsockname(td, args->s, &sa, &len);
1086 	if (error != 0)
1087 		return (error);
1088 
1089 	if (len != 0)
1090 		error = linux_copyout_sockaddr(sa, PTRIN(args->addr), len);
1091 
1092 	free(sa, M_SONAME);
1093 	if (error == 0)
1094 		error = copyout(&len, PTRIN(args->namelen), sizeof(len));
1095 	return (error);
1096 }
1097 
1098 int
1099 linux_getpeername(struct thread *td, struct linux_getpeername_args *args)
1100 {
1101 	struct sockaddr *sa;
1102 	int len, error;
1103 
1104 	error = copyin(PTRIN(args->namelen), &len, sizeof(len));
1105 	if (error != 0)
1106 		return (error);
1107 	if (len < 0)
1108 		return (EINVAL);
1109 
1110 	error = kern_getpeername(td, args->s, &sa, &len);
1111 	if (error != 0)
1112 		return (error);
1113 
1114 	if (len != 0)
1115 		error = linux_copyout_sockaddr(sa, PTRIN(args->addr), len);
1116 
1117 	free(sa, M_SONAME);
1118 	if (error == 0)
1119 		error = copyout(&len, PTRIN(args->namelen), sizeof(len));
1120 	return (error);
1121 }
1122 
1123 int
1124 linux_socketpair(struct thread *td, struct linux_socketpair_args *args)
1125 {
1126 	int domain, error, sv[2], type;
1127 
1128 	domain = linux_to_bsd_domain(args->domain);
1129 	if (domain != PF_LOCAL)
1130 		return (EAFNOSUPPORT);
1131 	type = args->type & LINUX_SOCK_TYPE_MASK;
1132 	if (type < 0 || type > LINUX_SOCK_MAX)
1133 		return (EINVAL);
1134 	error = linux_set_socket_flags(args->type & ~LINUX_SOCK_TYPE_MASK,
1135 	    &type);
1136 	if (error != 0)
1137 		return (error);
1138 	if (args->protocol != 0 && args->protocol != PF_UNIX) {
1139 		/*
1140 		 * Use of PF_UNIX as protocol argument is not right,
1141 		 * but Linux does it.
1142 		 * Do not map PF_UNIX as its Linux value is identical
1143 		 * to FreeBSD one.
1144 		 */
1145 		return (EPROTONOSUPPORT);
1146 	}
1147 	error = kern_socketpair(td, domain, type, 0, sv);
1148 	if (error != 0)
1149                 return (error);
1150         error = copyout(sv, PTRIN(args->rsv), 2 * sizeof(int));
1151         if (error != 0) {
1152                 (void)kern_close(td, sv[0]);
1153                 (void)kern_close(td, sv[1]);
1154         }
1155 	return (error);
1156 }
1157 
1158 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1159 struct linux_send_args {
1160 	register_t s;
1161 	register_t msg;
1162 	register_t len;
1163 	register_t flags;
1164 };
1165 
1166 static int
1167 linux_send(struct thread *td, struct linux_send_args *args)
1168 {
1169 	struct sendto_args /* {
1170 		int s;
1171 		caddr_t buf;
1172 		int len;
1173 		int flags;
1174 		caddr_t to;
1175 		int tolen;
1176 	} */ bsd_args;
1177 	struct file *fp;
1178 	int error, fflag;
1179 
1180 	bsd_args.s = args->s;
1181 	bsd_args.buf = (caddr_t)PTRIN(args->msg);
1182 	bsd_args.len = args->len;
1183 	bsd_args.flags = linux_to_bsd_msg_flags(args->flags);
1184 	bsd_args.to = NULL;
1185 	bsd_args.tolen = 0;
1186 	error = sys_sendto(td, &bsd_args);
1187 	if (error == ENOTCONN) {
1188 		/*
1189 		 * Linux doesn't return ENOTCONN for non-blocking sockets.
1190 		 * Instead it returns the EAGAIN.
1191 		 */
1192 		error = getsock_cap(td, args->s, &cap_send_rights, &fp,
1193 		    &fflag, NULL);
1194 		if (error == 0) {
1195 			if (fflag & FNONBLOCK)
1196 				error = EAGAIN;
1197 			fdrop(fp, td);
1198 		}
1199 	}
1200 	return (error);
1201 }
1202 
1203 struct linux_recv_args {
1204 	register_t s;
1205 	register_t msg;
1206 	register_t len;
1207 	register_t flags;
1208 };
1209 
1210 static int
1211 linux_recv(struct thread *td, struct linux_recv_args *args)
1212 {
1213 	struct recvfrom_args /* {
1214 		int s;
1215 		caddr_t buf;
1216 		int len;
1217 		int flags;
1218 		struct sockaddr *from;
1219 		socklen_t fromlenaddr;
1220 	} */ bsd_args;
1221 
1222 	bsd_args.s = args->s;
1223 	bsd_args.buf = (caddr_t)PTRIN(args->msg);
1224 	bsd_args.len = args->len;
1225 	bsd_args.flags = linux_to_bsd_msg_flags(args->flags);
1226 	bsd_args.from = NULL;
1227 	bsd_args.fromlenaddr = 0;
1228 	return (sys_recvfrom(td, &bsd_args));
1229 }
1230 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1231 
1232 int
1233 linux_sendto(struct thread *td, struct linux_sendto_args *args)
1234 {
1235 	struct msghdr msg;
1236 	struct iovec aiov;
1237 
1238 	if (linux_check_hdrincl(td, args->s) == 0)
1239 		/* IP_HDRINCL set, tweak the packet before sending */
1240 		return (linux_sendto_hdrincl(td, args));
1241 
1242 	msg.msg_name = PTRIN(args->to);
1243 	msg.msg_namelen = args->tolen;
1244 	msg.msg_iov = &aiov;
1245 	msg.msg_iovlen = 1;
1246 	msg.msg_control = NULL;
1247 	msg.msg_flags = 0;
1248 	aiov.iov_base = PTRIN(args->msg);
1249 	aiov.iov_len = args->len;
1250 	return (linux_sendit(td, args->s, &msg, args->flags, NULL,
1251 	    UIO_USERSPACE));
1252 }
1253 
1254 int
1255 linux_recvfrom(struct thread *td, struct linux_recvfrom_args *args)
1256 {
1257 	struct sockaddr *sa;
1258 	struct msghdr msg;
1259 	struct iovec aiov;
1260 	int error, fromlen;
1261 
1262 	if (PTRIN(args->fromlen) != NULL) {
1263 		error = copyin(PTRIN(args->fromlen), &fromlen,
1264 		    sizeof(fromlen));
1265 		if (error != 0)
1266 			return (error);
1267 		if (fromlen < 0)
1268 			return (EINVAL);
1269 		sa = malloc(fromlen, M_SONAME, M_WAITOK);
1270 	} else {
1271 		fromlen = 0;
1272 		sa = NULL;
1273 	}
1274 
1275 	msg.msg_name = sa;
1276 	msg.msg_namelen = fromlen;
1277 	msg.msg_iov = &aiov;
1278 	msg.msg_iovlen = 1;
1279 	aiov.iov_base = PTRIN(args->buf);
1280 	aiov.iov_len = args->len;
1281 	msg.msg_control = 0;
1282 	msg.msg_flags = linux_to_bsd_msg_flags(args->flags);
1283 
1284 	error = kern_recvit(td, args->s, &msg, UIO_SYSSPACE, NULL);
1285 	if (error != 0)
1286 		goto out;
1287 
1288 	if (PTRIN(args->from) != NULL)
1289 		error = linux_copyout_sockaddr(sa, PTRIN(args->from), msg.msg_namelen);
1290 
1291 	if (error == 0 && PTRIN(args->fromlen) != NULL)
1292 		error = copyout(&msg.msg_namelen, PTRIN(args->fromlen),
1293 		    sizeof(msg.msg_namelen));
1294 out:
1295 	free(sa, M_SONAME);
1296 	return (error);
1297 }
1298 
1299 static int
1300 linux_sendmsg_common(struct thread *td, l_int s, struct l_msghdr *msghdr,
1301     l_uint flags)
1302 {
1303 	struct cmsghdr *cmsg;
1304 	struct mbuf *control;
1305 	struct msghdr msg;
1306 	struct l_cmsghdr linux_cmsg;
1307 	struct l_cmsghdr *ptr_cmsg;
1308 	struct l_msghdr linux_msghdr;
1309 	struct iovec *iov;
1310 	socklen_t datalen;
1311 	struct sockaddr *sa;
1312 	struct socket *so;
1313 	sa_family_t sa_family;
1314 	struct file *fp;
1315 	void *data;
1316 	l_size_t len;
1317 	l_size_t clen;
1318 	int error, fflag;
1319 
1320 	error = copyin(msghdr, &linux_msghdr, sizeof(linux_msghdr));
1321 	if (error != 0)
1322 		return (error);
1323 
1324 	/*
1325 	 * Some Linux applications (ping) define a non-NULL control data
1326 	 * pointer, but a msg_controllen of 0, which is not allowed in the
1327 	 * FreeBSD system call interface.  NULL the msg_control pointer in
1328 	 * order to handle this case.  This should be checked, but allows the
1329 	 * Linux ping to work.
1330 	 */
1331 	if (PTRIN(linux_msghdr.msg_control) != NULL &&
1332 	    linux_msghdr.msg_controllen == 0)
1333 		linux_msghdr.msg_control = PTROUT(NULL);
1334 
1335 	error = linux_to_bsd_msghdr(&msg, &linux_msghdr);
1336 	if (error != 0)
1337 		return (error);
1338 
1339 #ifdef COMPAT_LINUX32
1340 	error = linux32_copyiniov(PTRIN(msg.msg_iov), msg.msg_iovlen,
1341 	    &iov, EMSGSIZE);
1342 #else
1343 	error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
1344 #endif
1345 	if (error != 0)
1346 		return (error);
1347 
1348 	control = NULL;
1349 
1350 	error = kern_getsockname(td, s, &sa, &datalen);
1351 	if (error != 0)
1352 		goto bad;
1353 	sa_family = sa->sa_family;
1354 	free(sa, M_SONAME);
1355 
1356 	if (flags & LINUX_MSG_OOB) {
1357 		error = EOPNOTSUPP;
1358 		if (sa_family == AF_UNIX)
1359 			goto bad;
1360 
1361 		error = getsock_cap(td, s, &cap_send_rights, &fp,
1362 		    &fflag, NULL);
1363 		if (error != 0)
1364 			goto bad;
1365 		so = fp->f_data;
1366 		if (so->so_type != SOCK_STREAM)
1367 			error = EOPNOTSUPP;
1368 		fdrop(fp, td);
1369 		if (error != 0)
1370 			goto bad;
1371 	}
1372 
1373 	if (linux_msghdr.msg_controllen >= sizeof(struct l_cmsghdr)) {
1374 		error = ENOBUFS;
1375 		control = m_get(M_WAITOK, MT_CONTROL);
1376 		MCLGET(control, M_WAITOK);
1377 		data = mtod(control, void *);
1378 		datalen = 0;
1379 
1380 		ptr_cmsg = PTRIN(linux_msghdr.msg_control);
1381 		clen = linux_msghdr.msg_controllen;
1382 		do {
1383 			error = copyin(ptr_cmsg, &linux_cmsg,
1384 			    sizeof(struct l_cmsghdr));
1385 			if (error != 0)
1386 				goto bad;
1387 
1388 			error = EINVAL;
1389 			if (linux_cmsg.cmsg_len < sizeof(struct l_cmsghdr) ||
1390 			    linux_cmsg.cmsg_len > clen)
1391 				goto bad;
1392 
1393 			if (datalen + CMSG_HDRSZ > MCLBYTES)
1394 				goto bad;
1395 
1396 			/*
1397 			 * Now we support only SCM_RIGHTS and SCM_CRED,
1398 			 * so return EINVAL in any other cmsg_type
1399 			 */
1400 			cmsg = data;
1401 			cmsg->cmsg_type =
1402 			    linux_to_bsd_cmsg_type(linux_cmsg.cmsg_type);
1403 			cmsg->cmsg_level =
1404 			    linux_to_bsd_sockopt_level(linux_cmsg.cmsg_level);
1405 			if (cmsg->cmsg_type == -1
1406 			    || cmsg->cmsg_level != SOL_SOCKET) {
1407 				linux_msg(curthread,
1408 				    "unsupported sendmsg cmsg level %d type %d",
1409 				    linux_cmsg.cmsg_level, linux_cmsg.cmsg_type);
1410 				goto bad;
1411 			}
1412 
1413 			/*
1414 			 * Some applications (e.g. pulseaudio) attempt to
1415 			 * send ancillary data even if the underlying protocol
1416 			 * doesn't support it which is not allowed in the
1417 			 * FreeBSD system call interface.
1418 			 */
1419 			if (sa_family != AF_UNIX)
1420 				goto next;
1421 
1422 			if (cmsg->cmsg_type == SCM_CREDS) {
1423 				len = sizeof(struct cmsgcred);
1424 				if (datalen + CMSG_SPACE(len) > MCLBYTES)
1425 					goto bad;
1426 
1427 				/*
1428 				 * The lower levels will fill in the structure
1429 				 */
1430 				memset(CMSG_DATA(data), 0, len);
1431 			} else {
1432 				len = linux_cmsg.cmsg_len - L_CMSG_HDRSZ;
1433 				if (datalen + CMSG_SPACE(len) < datalen ||
1434 				    datalen + CMSG_SPACE(len) > MCLBYTES)
1435 					goto bad;
1436 
1437 				error = copyin(LINUX_CMSG_DATA(ptr_cmsg),
1438 				    CMSG_DATA(data), len);
1439 				if (error != 0)
1440 					goto bad;
1441 			}
1442 
1443 			cmsg->cmsg_len = CMSG_LEN(len);
1444 			data = (char *)data + CMSG_SPACE(len);
1445 			datalen += CMSG_SPACE(len);
1446 
1447 next:
1448 			if (clen <= LINUX_CMSG_ALIGN(linux_cmsg.cmsg_len))
1449 				break;
1450 
1451 			clen -= LINUX_CMSG_ALIGN(linux_cmsg.cmsg_len);
1452 			ptr_cmsg = (struct l_cmsghdr *)((char *)ptr_cmsg +
1453 			    LINUX_CMSG_ALIGN(linux_cmsg.cmsg_len));
1454 		} while(clen >= sizeof(struct l_cmsghdr));
1455 
1456 		control->m_len = datalen;
1457 		if (datalen == 0) {
1458 			m_freem(control);
1459 			control = NULL;
1460 		}
1461 	}
1462 
1463 	msg.msg_iov = iov;
1464 	msg.msg_flags = 0;
1465 	error = linux_sendit(td, s, &msg, flags, control, UIO_USERSPACE);
1466 	control = NULL;
1467 
1468 bad:
1469 	m_freem(control);
1470 	free(iov, M_IOV);
1471 	return (error);
1472 }
1473 
1474 int
1475 linux_sendmsg(struct thread *td, struct linux_sendmsg_args *args)
1476 {
1477 
1478 	return (linux_sendmsg_common(td, args->s, PTRIN(args->msg),
1479 	    args->flags));
1480 }
1481 
1482 int
1483 linux_sendmmsg(struct thread *td, struct linux_sendmmsg_args *args)
1484 {
1485 	struct l_mmsghdr *msg;
1486 	l_uint retval;
1487 	int error, datagrams;
1488 
1489 	if (args->vlen > UIO_MAXIOV)
1490 		args->vlen = UIO_MAXIOV;
1491 
1492 	msg = PTRIN(args->msg);
1493 	datagrams = 0;
1494 	while (datagrams < args->vlen) {
1495 		error = linux_sendmsg_common(td, args->s, &msg->msg_hdr,
1496 		    args->flags);
1497 		if (error != 0)
1498 			break;
1499 
1500 		retval = td->td_retval[0];
1501 		error = copyout(&retval, &msg->msg_len, sizeof(msg->msg_len));
1502 		if (error != 0)
1503 			break;
1504 		++msg;
1505 		++datagrams;
1506 	}
1507 	if (error == 0)
1508 		td->td_retval[0] = datagrams;
1509 	return (error);
1510 }
1511 
1512 static int
1513 linux_recvmsg_common(struct thread *td, l_int s, struct l_msghdr *msghdr,
1514     l_uint flags, struct msghdr *msg)
1515 {
1516 	struct cmsghdr *cm;
1517 	struct cmsgcred *cmcred;
1518 	struct sockcred2 *scred;
1519 	struct l_cmsghdr *linux_cmsg = NULL;
1520 	struct l_ucred linux_ucred;
1521 	socklen_t datalen, maxlen, outlen;
1522 	struct l_msghdr linux_msghdr;
1523 	struct iovec *iov, *uiov;
1524 	struct mbuf *control = NULL;
1525 	struct mbuf **controlp;
1526 	struct timeval *ftmvl;
1527 	struct sockaddr *sa;
1528 	l_timeval ltmvl;
1529 	caddr_t outbuf;
1530 	void *data;
1531 	int error, i, fd, fds, *fdp;
1532 
1533 	error = copyin(msghdr, &linux_msghdr, sizeof(linux_msghdr));
1534 	if (error != 0)
1535 		return (error);
1536 
1537 	error = linux_to_bsd_msghdr(msg, &linux_msghdr);
1538 	if (error != 0)
1539 		return (error);
1540 
1541 #ifdef COMPAT_LINUX32
1542 	error = linux32_copyiniov(PTRIN(msg->msg_iov), msg->msg_iovlen,
1543 	    &iov, EMSGSIZE);
1544 #else
1545 	error = copyiniov(msg->msg_iov, msg->msg_iovlen, &iov, EMSGSIZE);
1546 #endif
1547 	if (error != 0)
1548 		return (error);
1549 
1550 	if (msg->msg_name != NULL && msg->msg_namelen > 0) {
1551 		msg->msg_namelen = min(msg->msg_namelen, SOCK_MAXADDRLEN);
1552 		sa = malloc(msg->msg_namelen, M_SONAME, M_WAITOK);
1553 		msg->msg_name = sa;
1554 	} else {
1555 		sa = NULL;
1556 		msg->msg_name = NULL;
1557 	}
1558 
1559 	uiov = msg->msg_iov;
1560 	msg->msg_iov = iov;
1561 	controlp = (msg->msg_control != NULL) ? &control : NULL;
1562 	error = kern_recvit(td, s, msg, UIO_SYSSPACE, controlp);
1563 	msg->msg_iov = uiov;
1564 	if (error != 0)
1565 		goto bad;
1566 
1567 	/*
1568 	 * Note that kern_recvit() updates msg->msg_namelen.
1569 	 */
1570 	if (msg->msg_name != NULL && msg->msg_namelen > 0) {
1571 		msg->msg_name = PTRIN(linux_msghdr.msg_name);
1572 		error = linux_copyout_sockaddr(sa,
1573 		    PTRIN(msg->msg_name), msg->msg_namelen);
1574 		if (error != 0)
1575 			goto bad;
1576 	}
1577 
1578 	error = bsd_to_linux_msghdr(msg, &linux_msghdr);
1579 	if (error != 0)
1580 		goto bad;
1581 
1582 	maxlen = linux_msghdr.msg_controllen;
1583 	linux_msghdr.msg_controllen = 0;
1584 	if (control) {
1585 		linux_cmsg = malloc(L_CMSG_HDRSZ, M_LINUX, M_WAITOK | M_ZERO);
1586 
1587 		msg->msg_control = mtod(control, struct cmsghdr *);
1588 		msg->msg_controllen = control->m_len;
1589 
1590 		cm = CMSG_FIRSTHDR(msg);
1591 		outbuf = PTRIN(linux_msghdr.msg_control);
1592 		outlen = 0;
1593 		while (cm != NULL) {
1594 			linux_cmsg->cmsg_type =
1595 			    bsd_to_linux_cmsg_type(cm->cmsg_type);
1596 			linux_cmsg->cmsg_level =
1597 			    bsd_to_linux_sockopt_level(cm->cmsg_level);
1598 			if (linux_cmsg->cmsg_type == -1 ||
1599 			    cm->cmsg_level != SOL_SOCKET) {
1600 				linux_msg(curthread,
1601 				    "unsupported recvmsg cmsg level %d type %d",
1602 				    cm->cmsg_level, cm->cmsg_type);
1603 				error = EINVAL;
1604 				goto bad;
1605 			}
1606 
1607 			data = CMSG_DATA(cm);
1608 			datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
1609 
1610 			switch (cm->cmsg_type) {
1611 			case SCM_RIGHTS:
1612 				if (flags & LINUX_MSG_CMSG_CLOEXEC) {
1613 					fds = datalen / sizeof(int);
1614 					fdp = data;
1615 					for (i = 0; i < fds; i++) {
1616 						fd = *fdp++;
1617 						(void)kern_fcntl(td, fd,
1618 						    F_SETFD, FD_CLOEXEC);
1619 					}
1620 				}
1621 				break;
1622 
1623 			case SCM_CREDS:
1624 				/*
1625 				 * Currently LOCAL_CREDS is never in
1626 				 * effect for Linux so no need to worry
1627 				 * about sockcred
1628 				 */
1629 				if (datalen != sizeof(*cmcred)) {
1630 					error = EMSGSIZE;
1631 					goto bad;
1632 				}
1633 				cmcred = (struct cmsgcred *)data;
1634 				bzero(&linux_ucred, sizeof(linux_ucred));
1635 				linux_ucred.pid = cmcred->cmcred_pid;
1636 				linux_ucred.uid = cmcred->cmcred_uid;
1637 				linux_ucred.gid = cmcred->cmcred_gid;
1638 				data = &linux_ucred;
1639 				datalen = sizeof(linux_ucred);
1640 				break;
1641 
1642 			case SCM_CREDS2:
1643 				scred = data;
1644 				bzero(&linux_ucred, sizeof(linux_ucred));
1645 				linux_ucred.pid = scred->sc_pid;
1646 				linux_ucred.uid = scred->sc_uid;
1647 				linux_ucred.gid = scred->sc_gid;
1648 				data = &linux_ucred;
1649 				datalen = sizeof(linux_ucred);
1650 				break;
1651 
1652 			case SCM_TIMESTAMP:
1653 				if (datalen != sizeof(struct timeval)) {
1654 					error = EMSGSIZE;
1655 					goto bad;
1656 				}
1657 				ftmvl = (struct timeval *)data;
1658 				ltmvl.tv_sec = ftmvl->tv_sec;
1659 				ltmvl.tv_usec = ftmvl->tv_usec;
1660 				data = &ltmvl;
1661 				datalen = sizeof(ltmvl);
1662 				break;
1663 			}
1664 
1665 			if (outlen + LINUX_CMSG_LEN(datalen) > maxlen) {
1666 				if (outlen == 0) {
1667 					error = EMSGSIZE;
1668 					goto bad;
1669 				} else {
1670 					linux_msghdr.msg_flags |= LINUX_MSG_CTRUNC;
1671 					m_dispose_extcontrolm(control);
1672 					goto out;
1673 				}
1674 			}
1675 
1676 			linux_cmsg->cmsg_len = LINUX_CMSG_LEN(datalen);
1677 
1678 			error = copyout(linux_cmsg, outbuf, L_CMSG_HDRSZ);
1679 			if (error != 0)
1680 				goto bad;
1681 			outbuf += L_CMSG_HDRSZ;
1682 
1683 			error = copyout(data, outbuf, datalen);
1684 			if (error != 0)
1685 				goto bad;
1686 
1687 			outbuf += LINUX_CMSG_ALIGN(datalen);
1688 			outlen += LINUX_CMSG_LEN(datalen);
1689 
1690 			cm = CMSG_NXTHDR(msg, cm);
1691 		}
1692 		linux_msghdr.msg_controllen = outlen;
1693 	}
1694 
1695 out:
1696 	error = copyout(&linux_msghdr, msghdr, sizeof(linux_msghdr));
1697 
1698 bad:
1699 	if (control != NULL) {
1700 		if (error != 0)
1701 			m_dispose_extcontrolm(control);
1702 		m_freem(control);
1703 	}
1704 	free(iov, M_IOV);
1705 	free(linux_cmsg, M_LINUX);
1706 	free(sa, M_SONAME);
1707 
1708 	return (error);
1709 }
1710 
1711 int
1712 linux_recvmsg(struct thread *td, struct linux_recvmsg_args *args)
1713 {
1714 	struct msghdr bsd_msg;
1715 
1716 	return (linux_recvmsg_common(td, args->s, PTRIN(args->msg),
1717 	    args->flags, &bsd_msg));
1718 }
1719 
1720 int
1721 linux_recvmmsg(struct thread *td, struct linux_recvmmsg_args *args)
1722 {
1723 	struct l_mmsghdr *msg;
1724 	struct msghdr bsd_msg;
1725 	struct l_timespec lts;
1726 	struct timespec ts, tts;
1727 	l_uint retval;
1728 	int error, datagrams;
1729 
1730 	if (args->timeout) {
1731 		error = copyin(args->timeout, &lts, sizeof(struct l_timespec));
1732 		if (error != 0)
1733 			return (error);
1734 		error = linux_to_native_timespec(&ts, &lts);
1735 		if (error != 0)
1736 			return (error);
1737 		getnanotime(&tts);
1738 		timespecadd(&tts, &ts, &tts);
1739 	}
1740 
1741 	msg = PTRIN(args->msg);
1742 	datagrams = 0;
1743 	while (datagrams < args->vlen) {
1744 		error = linux_recvmsg_common(td, args->s, &msg->msg_hdr,
1745 		    args->flags & ~LINUX_MSG_WAITFORONE, &bsd_msg);
1746 		if (error != 0)
1747 			break;
1748 
1749 		retval = td->td_retval[0];
1750 		error = copyout(&retval, &msg->msg_len, sizeof(msg->msg_len));
1751 		if (error != 0)
1752 			break;
1753 		++msg;
1754 		++datagrams;
1755 
1756 		/*
1757 		 * MSG_WAITFORONE turns on MSG_DONTWAIT after one packet.
1758 		 */
1759 		if (args->flags & LINUX_MSG_WAITFORONE)
1760 			args->flags |= LINUX_MSG_DONTWAIT;
1761 
1762 		/*
1763 		 * See BUGS section of recvmmsg(2).
1764 		 */
1765 		if (args->timeout) {
1766 			getnanotime(&ts);
1767 			timespecsub(&ts, &tts, &ts);
1768 			if (!timespecisset(&ts) || ts.tv_sec > 0)
1769 				break;
1770 		}
1771 		/* Out of band data, return right away. */
1772 		if (bsd_msg.msg_flags & MSG_OOB)
1773 			break;
1774 	}
1775 	if (error == 0)
1776 		td->td_retval[0] = datagrams;
1777 	return (error);
1778 }
1779 
1780 int
1781 linux_shutdown(struct thread *td, struct linux_shutdown_args *args)
1782 {
1783 
1784 	return (kern_shutdown(td, args->s, args->how));
1785 }
1786 
1787 int
1788 linux_setsockopt(struct thread *td, struct linux_setsockopt_args *args)
1789 {
1790 	l_timeval linux_tv;
1791 	struct sockaddr *sa;
1792 	struct timeval tv;
1793 	socklen_t len;
1794 	int error, level, name;
1795 
1796 	level = linux_to_bsd_sockopt_level(args->level);
1797 	switch (level) {
1798 	case SOL_SOCKET:
1799 		name = linux_to_bsd_so_sockopt(args->optname);
1800 		switch (name) {
1801 		case LOCAL_CREDS_PERSISTENT:
1802 			level = SOL_LOCAL;
1803 			break;
1804 		case SO_RCVTIMEO:
1805 			/* FALLTHROUGH */
1806 		case SO_SNDTIMEO:
1807 			error = copyin(PTRIN(args->optval), &linux_tv,
1808 			    sizeof(linux_tv));
1809 			if (error != 0)
1810 				return (error);
1811 			tv.tv_sec = linux_tv.tv_sec;
1812 			tv.tv_usec = linux_tv.tv_usec;
1813 			return (kern_setsockopt(td, args->s, level,
1814 			    name, &tv, UIO_SYSSPACE, sizeof(tv)));
1815 			/* NOTREACHED */
1816 		default:
1817 			break;
1818 		}
1819 		break;
1820 	case IPPROTO_IP:
1821 		if (args->optname == LINUX_IP_RECVERR &&
1822 		    linux_ignore_ip_recverr) {
1823 			/*
1824 			 * XXX: This is a hack to unbreak DNS resolution
1825 			 *	with glibc 2.30 and above.
1826 			 */
1827 			return (0);
1828 		}
1829 		name = linux_to_bsd_ip_sockopt(args->optname);
1830 		break;
1831 	case IPPROTO_IPV6:
1832 		name = linux_to_bsd_ip6_sockopt(args->optname);
1833 		break;
1834 	case IPPROTO_TCP:
1835 		name = linux_to_bsd_tcp_sockopt(args->optname);
1836 		break;
1837 	default:
1838 		name = -1;
1839 		break;
1840 	}
1841 	if (name < 0) {
1842 		if (name == -1)
1843 			linux_msg(curthread,
1844 			    "unsupported setsockopt level %d optname %d",
1845 			    args->level, args->optname);
1846 		return (ENOPROTOOPT);
1847 	}
1848 
1849 	if (name == IPV6_NEXTHOP) {
1850 		len = args->optlen;
1851 		error = linux_to_bsd_sockaddr(PTRIN(args->optval), &sa, &len);
1852 		if (error != 0)
1853 			return (error);
1854 
1855 		error = kern_setsockopt(td, args->s, level,
1856 		    name, sa, UIO_SYSSPACE, len);
1857 		free(sa, M_SONAME);
1858 	} else {
1859 		error = kern_setsockopt(td, args->s, level,
1860 		    name, PTRIN(args->optval), UIO_USERSPACE, args->optlen);
1861 	}
1862 
1863 	return (error);
1864 }
1865 
1866 static int
1867 linux_getsockopt_so_peergroups(struct thread *td,
1868     struct linux_getsockopt_args *args)
1869 {
1870 	struct xucred xu;
1871 	socklen_t xulen, len;
1872 	int error, i;
1873 
1874 	xulen = sizeof(xu);
1875 	error = kern_getsockopt(td, args->s, 0,
1876 	    LOCAL_PEERCRED, &xu, UIO_SYSSPACE, &xulen);
1877 	if (error != 0)
1878 		return (error);
1879 
1880 	len = xu.cr_ngroups * sizeof(l_gid_t);
1881 	if (args->optlen < len) {
1882 		error = copyout(&len, PTRIN(args->optlen), sizeof(len));
1883 		if (error == 0)
1884 			error = ERANGE;
1885 		return (error);
1886 	}
1887 
1888 	/*
1889 	 * "- 1" to skip the primary group.
1890 	 */
1891 	for (i = 0; i < xu.cr_ngroups - 1; i++) {
1892 		error = copyout(xu.cr_groups + i + 1,
1893 		    (void *)(args->optval + i * sizeof(l_gid_t)),
1894 		    sizeof(l_gid_t));
1895 		if (error != 0)
1896 			return (error);
1897 	}
1898 
1899 	error = copyout(&len, PTRIN(args->optlen), sizeof(len));
1900 	return (error);
1901 }
1902 
1903 static int
1904 linux_getsockopt_so_peersec(struct thread *td,
1905     struct linux_getsockopt_args *args)
1906 {
1907 	socklen_t len;
1908 	int error;
1909 
1910 	len = sizeof(SECURITY_CONTEXT_STRING);
1911 	if (args->optlen < len) {
1912 		error = copyout(&len, PTRIN(args->optlen), sizeof(len));
1913 		if (error == 0)
1914 			error = ERANGE;
1915 		return (error);
1916 	}
1917 
1918 	error = copyout(SECURITY_CONTEXT_STRING,
1919 	    PTRIN(args->optval), sizeof(SECURITY_CONTEXT_STRING));
1920 	if (error == 0)
1921 		error = copyout(&len, PTRIN(args->optlen), sizeof(len));
1922 	return (error);
1923 }
1924 
1925 int
1926 linux_getsockopt(struct thread *td, struct linux_getsockopt_args *args)
1927 {
1928 	l_timeval linux_tv;
1929 	struct timeval tv;
1930 	socklen_t tv_len, xulen, len;
1931 	struct sockaddr *sa;
1932 	struct xucred xu;
1933 	struct l_ucred lxu;
1934 	int error, level, name, newval;
1935 
1936 	level = linux_to_bsd_sockopt_level(args->level);
1937 	switch (level) {
1938 	case SOL_SOCKET:
1939 		switch (args->optname) {
1940 		case LINUX_SO_PEERGROUPS:
1941 			return (linux_getsockopt_so_peergroups(td, args));
1942 		case LINUX_SO_PEERSEC:
1943 			return (linux_getsockopt_so_peersec(td, args));
1944 		default:
1945 			break;
1946 		}
1947 
1948 		name = linux_to_bsd_so_sockopt(args->optname);
1949 		switch (name) {
1950 		case LOCAL_CREDS_PERSISTENT:
1951 			level = SOL_LOCAL;
1952 			break;
1953 		case SO_RCVTIMEO:
1954 			/* FALLTHROUGH */
1955 		case SO_SNDTIMEO:
1956 			tv_len = sizeof(tv);
1957 			error = kern_getsockopt(td, args->s, level,
1958 			    name, &tv, UIO_SYSSPACE, &tv_len);
1959 			if (error != 0)
1960 				return (error);
1961 			linux_tv.tv_sec = tv.tv_sec;
1962 			linux_tv.tv_usec = tv.tv_usec;
1963 			return (copyout(&linux_tv, PTRIN(args->optval),
1964 			    sizeof(linux_tv)));
1965 			/* NOTREACHED */
1966 		case LOCAL_PEERCRED:
1967 			if (args->optlen < sizeof(lxu))
1968 				return (EINVAL);
1969 			/*
1970 			 * LOCAL_PEERCRED is not served at the SOL_SOCKET level,
1971 			 * but by the Unix socket's level 0.
1972 			 */
1973 			level = 0;
1974 			xulen = sizeof(xu);
1975 			error = kern_getsockopt(td, args->s, level,
1976 			    name, &xu, UIO_SYSSPACE, &xulen);
1977 			if (error != 0)
1978 				return (error);
1979 			lxu.pid = xu.cr_pid;
1980 			lxu.uid = xu.cr_uid;
1981 			lxu.gid = xu.cr_gid;
1982 			return (copyout(&lxu, PTRIN(args->optval), sizeof(lxu)));
1983 			/* NOTREACHED */
1984 		case SO_ERROR:
1985 			len = sizeof(newval);
1986 			error = kern_getsockopt(td, args->s, level,
1987 			    name, &newval, UIO_SYSSPACE, &len);
1988 			if (error != 0)
1989 				return (error);
1990 			newval = -bsd_to_linux_errno(newval);
1991 			return (copyout(&newval, PTRIN(args->optval), len));
1992 			/* NOTREACHED */
1993 		default:
1994 			break;
1995 		}
1996 		break;
1997 	case IPPROTO_IP:
1998 		name = linux_to_bsd_ip_sockopt(args->optname);
1999 		break;
2000 	case IPPROTO_IPV6:
2001 		name = linux_to_bsd_ip6_sockopt(args->optname);
2002 		break;
2003 	case IPPROTO_TCP:
2004 		name = linux_to_bsd_tcp_sockopt(args->optname);
2005 		break;
2006 	default:
2007 		name = -1;
2008 		break;
2009 	}
2010 	if (name < 0) {
2011 		if (name == -1)
2012 			linux_msg(curthread,
2013 			    "unsupported getsockopt level %d optname %d",
2014 			    args->level, args->optname);
2015 		return (EINVAL);
2016 	}
2017 
2018 	if (name == IPV6_NEXTHOP) {
2019 		error = copyin(PTRIN(args->optlen), &len, sizeof(len));
2020                 if (error != 0)
2021                         return (error);
2022 		sa = malloc(len, M_SONAME, M_WAITOK);
2023 
2024 		error = kern_getsockopt(td, args->s, level,
2025 		    name, sa, UIO_SYSSPACE, &len);
2026 		if (error != 0)
2027 			goto out;
2028 
2029 		error = linux_copyout_sockaddr(sa, PTRIN(args->optval), len);
2030 		if (error == 0)
2031 			error = copyout(&len, PTRIN(args->optlen),
2032 			    sizeof(len));
2033 out:
2034 		free(sa, M_SONAME);
2035 	} else {
2036 		if (args->optval) {
2037 			error = copyin(PTRIN(args->optlen), &len, sizeof(len));
2038 			if (error != 0)
2039 				return (error);
2040 		}
2041 		error = kern_getsockopt(td, args->s, level,
2042 		    name, PTRIN(args->optval), UIO_USERSPACE, &len);
2043 		if (error == 0)
2044 			error = copyout(&len, PTRIN(args->optlen),
2045 			    sizeof(len));
2046 	}
2047 
2048 	return (error);
2049 }
2050 
2051 static int
2052 linux_sendfile_common(struct thread *td, l_int out, l_int in,
2053     l_loff_t *offset, l_size_t count)
2054 {
2055 	off_t bytes_read;
2056 	int error;
2057 	l_loff_t current_offset;
2058 	struct file *fp;
2059 
2060 	AUDIT_ARG_FD(in);
2061 	error = fget_read(td, in, &cap_pread_rights, &fp);
2062 	if (error != 0)
2063 		return (error);
2064 
2065 	if (offset != NULL) {
2066 		current_offset = *offset;
2067 	} else {
2068 		error = (fp->f_ops->fo_flags & DFLAG_SEEKABLE) != 0 ?
2069 		    fo_seek(fp, 0, SEEK_CUR, td) : ESPIPE;
2070 		if (error != 0)
2071 			goto drop;
2072 		current_offset = td->td_uretoff.tdu_off;
2073 	}
2074 
2075 	bytes_read = 0;
2076 
2077 	/* Linux cannot have 0 count. */
2078 	if (count <= 0 || current_offset < 0) {
2079 		error = EINVAL;
2080 		goto drop;
2081 	}
2082 
2083 	error = fo_sendfile(fp, out, NULL, NULL, current_offset, count,
2084 	    &bytes_read, 0, td);
2085 	if (error != 0)
2086 		goto drop;
2087 	current_offset += bytes_read;
2088 
2089 	if (offset != NULL) {
2090 		*offset = current_offset;
2091 	} else {
2092 		error = fo_seek(fp, current_offset, SEEK_SET, td);
2093 		if (error != 0)
2094 			goto drop;
2095 	}
2096 
2097 	td->td_retval[0] = (ssize_t)bytes_read;
2098 drop:
2099 	fdrop(fp, td);
2100 	return (error);
2101 }
2102 
2103 int
2104 linux_sendfile(struct thread *td, struct linux_sendfile_args *arg)
2105 {
2106 	/*
2107 	 * Differences between FreeBSD and Linux sendfile:
2108 	 * - Linux doesn't send anything when count is 0 (FreeBSD uses 0 to
2109 	 *   mean send the whole file.)  In linux_sendfile given fds are still
2110 	 *   checked for validity when the count is 0.
2111 	 * - Linux can send to any fd whereas FreeBSD only supports sockets.
2112 	 *   The same restriction follows for linux_sendfile.
2113 	 * - Linux doesn't have an equivalent for FreeBSD's flags and sf_hdtr.
2114 	 * - Linux takes an offset pointer and updates it to the read location.
2115 	 *   FreeBSD takes in an offset and a 'bytes read' parameter which is
2116 	 *   only filled if it isn't NULL.  We use this parameter to update the
2117 	 *   offset pointer if it exists.
2118 	 * - Linux sendfile returns bytes read on success while FreeBSD
2119 	 *   returns 0.  We use the 'bytes read' parameter to get this value.
2120 	 */
2121 
2122 	l_loff_t offset64;
2123 	l_long offset;
2124 	int ret;
2125 	int error;
2126 
2127 	if (arg->offset != NULL) {
2128 		error = copyin(arg->offset, &offset, sizeof(offset));
2129 		if (error != 0)
2130 			return (error);
2131 		offset64 = (l_loff_t)offset;
2132 	}
2133 
2134 	ret = linux_sendfile_common(td, arg->out, arg->in,
2135 	    arg->offset != NULL ? &offset64 : NULL, arg->count);
2136 
2137 	if (arg->offset != NULL) {
2138 #if defined(__i386__) || defined(__arm__) || \
2139     (defined(__amd64__) && defined(COMPAT_LINUX32))
2140 		if (offset64 > INT32_MAX)
2141 			return (EOVERFLOW);
2142 #endif
2143 		offset = (l_long)offset64;
2144 		error = copyout(&offset, arg->offset, sizeof(offset));
2145 		if (error != 0)
2146 			return (error);
2147 	}
2148 
2149 	return (ret);
2150 }
2151 
2152 #if defined(__i386__) || defined(__arm__) || \
2153     (defined(__amd64__) && defined(COMPAT_LINUX32))
2154 
2155 int
2156 linux_sendfile64(struct thread *td, struct linux_sendfile64_args *arg)
2157 {
2158 	l_loff_t offset;
2159 	int ret;
2160 	int error;
2161 
2162 	if (arg->offset != NULL) {
2163 		error = copyin(arg->offset, &offset, sizeof(offset));
2164 		if (error != 0)
2165 			return (error);
2166 	}
2167 
2168 	ret = linux_sendfile_common(td, arg->out, arg->in,
2169 		arg->offset != NULL ? &offset : NULL, arg->count);
2170 
2171 	if (arg->offset != NULL) {
2172 		error = copyout(&offset, arg->offset, sizeof(offset));
2173 		if (error != 0)
2174 			return (error);
2175 	}
2176 
2177 	return (ret);
2178 }
2179 
2180 /* Argument list sizes for linux_socketcall */
2181 static const unsigned char lxs_args_cnt[] = {
2182 	0 /* unused*/,		3 /* socket */,
2183 	3 /* bind */,		3 /* connect */,
2184 	2 /* listen */,		3 /* accept */,
2185 	3 /* getsockname */,	3 /* getpeername */,
2186 	4 /* socketpair */,	4 /* send */,
2187 	4 /* recv */,		6 /* sendto */,
2188 	6 /* recvfrom */,	2 /* shutdown */,
2189 	5 /* setsockopt */,	5 /* getsockopt */,
2190 	3 /* sendmsg */,	3 /* recvmsg */,
2191 	4 /* accept4 */,	5 /* recvmmsg */,
2192 	4 /* sendmmsg */,	4 /* sendfile */
2193 };
2194 #define	LINUX_ARGS_CNT		(nitems(lxs_args_cnt) - 1)
2195 #define	LINUX_ARG_SIZE(x)	(lxs_args_cnt[x] * sizeof(l_ulong))
2196 
2197 int
2198 linux_socketcall(struct thread *td, struct linux_socketcall_args *args)
2199 {
2200 	l_ulong a[6];
2201 #if defined(__amd64__) && defined(COMPAT_LINUX32)
2202 	register_t l_args[6];
2203 #endif
2204 	void *arg;
2205 	int error;
2206 
2207 	if (args->what < LINUX_SOCKET || args->what > LINUX_ARGS_CNT)
2208 		return (EINVAL);
2209 	error = copyin(PTRIN(args->args), a, LINUX_ARG_SIZE(args->what));
2210 	if (error != 0)
2211 		return (error);
2212 
2213 #if defined(__amd64__) && defined(COMPAT_LINUX32)
2214 	for (int i = 0; i < lxs_args_cnt[args->what]; ++i)
2215 		l_args[i] = a[i];
2216 	arg = l_args;
2217 #else
2218 	arg = a;
2219 #endif
2220 	switch (args->what) {
2221 	case LINUX_SOCKET:
2222 		return (linux_socket(td, arg));
2223 	case LINUX_BIND:
2224 		return (linux_bind(td, arg));
2225 	case LINUX_CONNECT:
2226 		return (linux_connect(td, arg));
2227 	case LINUX_LISTEN:
2228 		return (linux_listen(td, arg));
2229 	case LINUX_ACCEPT:
2230 		return (linux_accept(td, arg));
2231 	case LINUX_GETSOCKNAME:
2232 		return (linux_getsockname(td, arg));
2233 	case LINUX_GETPEERNAME:
2234 		return (linux_getpeername(td, arg));
2235 	case LINUX_SOCKETPAIR:
2236 		return (linux_socketpair(td, arg));
2237 	case LINUX_SEND:
2238 		return (linux_send(td, arg));
2239 	case LINUX_RECV:
2240 		return (linux_recv(td, arg));
2241 	case LINUX_SENDTO:
2242 		return (linux_sendto(td, arg));
2243 	case LINUX_RECVFROM:
2244 		return (linux_recvfrom(td, arg));
2245 	case LINUX_SHUTDOWN:
2246 		return (linux_shutdown(td, arg));
2247 	case LINUX_SETSOCKOPT:
2248 		return (linux_setsockopt(td, arg));
2249 	case LINUX_GETSOCKOPT:
2250 		return (linux_getsockopt(td, arg));
2251 	case LINUX_SENDMSG:
2252 		return (linux_sendmsg(td, arg));
2253 	case LINUX_RECVMSG:
2254 		return (linux_recvmsg(td, arg));
2255 	case LINUX_ACCEPT4:
2256 		return (linux_accept4(td, arg));
2257 	case LINUX_RECVMMSG:
2258 		return (linux_recvmmsg(td, arg));
2259 	case LINUX_SENDMMSG:
2260 		return (linux_sendmmsg(td, arg));
2261 	case LINUX_SENDFILE:
2262 		return (linux_sendfile(td, arg));
2263 	}
2264 
2265 	linux_msg(td, "socket type %d not implemented", args->what);
2266 	return (ENOSYS);
2267 }
2268 #endif /* __i386__ || __arm__ || (__amd64__ && COMPAT_LINUX32) */
2269