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