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