1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
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 "opt_inet6.h"
30
31 #include <sys/param.h>
32 #include <sys/capsicum.h>
33 #include <sys/filedesc.h>
34 #include <sys/limits.h>
35 #include <sys/malloc.h>
36 #include <sys/mbuf.h>
37 #include <sys/proc.h>
38 #include <sys/protosw.h>
39 #include <sys/socket.h>
40 #include <sys/socketvar.h>
41 #include <sys/syscallsubr.h>
42 #include <sys/sysproto.h>
43 #include <sys/vnode.h>
44 #include <sys/un.h>
45 #include <sys/unistd.h>
46
47 #include <security/audit/audit.h>
48
49 #include <net/if.h>
50 #include <net/vnet.h>
51 #include <netinet/in.h>
52 #include <netinet/ip.h>
53 #include <netinet/tcp.h>
54 #ifdef INET6
55 #include <netinet/ip6.h>
56 #include <netinet6/ip6_var.h>
57 #endif
58
59 #ifdef COMPAT_LINUX32
60 #include <compat/freebsd32/freebsd32_util.h>
61 #include <machine/../linux32/linux.h>
62 #include <machine/../linux32/linux32_proto.h>
63 #else
64 #include <machine/../linux/linux.h>
65 #include <machine/../linux/linux_proto.h>
66 #endif
67 #include <compat/linux/linux_common.h>
68 #include <compat/linux/linux_emul.h>
69 #include <compat/linux/linux_file.h>
70 #include <compat/linux/linux_mib.h>
71 #include <compat/linux/linux_socket.h>
72 #include <compat/linux/linux_time.h>
73 #include <compat/linux/linux_util.h>
74
75 _Static_assert(offsetof(struct l_ifreq, ifr_ifru) ==
76 offsetof(struct ifreq, ifr_ifru),
77 "Linux ifreq members names should be equal to FreeeBSD");
78 _Static_assert(offsetof(struct l_ifreq, ifr_index) ==
79 offsetof(struct ifreq, ifr_index),
80 "Linux ifreq members names should be equal to FreeeBSD");
81 _Static_assert(offsetof(struct l_ifreq, ifr_name) ==
82 offsetof(struct ifreq, ifr_name),
83 "Linux ifreq members names should be equal to FreeeBSD");
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 #define SOL_NETLINK 270
94
95 static int
linux_to_bsd_sockopt_level(int level)96 linux_to_bsd_sockopt_level(int level)
97 {
98
99 if (level == LINUX_SOL_SOCKET)
100 return (SOL_SOCKET);
101 /* Remaining values are RFC-defined protocol numbers. */
102 return (level);
103 }
104
105 static int
bsd_to_linux_sockopt_level(int level)106 bsd_to_linux_sockopt_level(int level)
107 {
108
109 if (level == SOL_SOCKET)
110 return (LINUX_SOL_SOCKET);
111 return (level);
112 }
113
114 static int
linux_to_bsd_ip_sockopt(int opt)115 linux_to_bsd_ip_sockopt(int opt)
116 {
117
118 switch (opt) {
119 /* known and translated sockopts */
120 case LINUX_IP_TOS:
121 return (IP_TOS);
122 case LINUX_IP_TTL:
123 return (IP_TTL);
124 case LINUX_IP_HDRINCL:
125 return (IP_HDRINCL);
126 case LINUX_IP_OPTIONS:
127 return (IP_OPTIONS);
128 case LINUX_IP_RECVOPTS:
129 LINUX_RATELIMIT_MSG_NOTTESTED("IPv4 socket option IP_RECVOPTS");
130 return (IP_RECVOPTS);
131 case LINUX_IP_RETOPTS:
132 LINUX_RATELIMIT_MSG_NOTTESTED("IPv4 socket option IP_REETOPTS");
133 return (IP_RETOPTS);
134 case LINUX_IP_RECVTTL:
135 LINUX_RATELIMIT_MSG_NOTTESTED("IPv4 socket option IP_RECVTTL");
136 return (IP_RECVTTL);
137 case LINUX_IP_RECVTOS:
138 LINUX_RATELIMIT_MSG_NOTTESTED("IPv4 socket option IP_RECVTOS");
139 return (IP_RECVTOS);
140 case LINUX_IP_FREEBIND:
141 LINUX_RATELIMIT_MSG_NOTTESTED("IPv4 socket option IP_FREEBIND");
142 return (IP_BINDANY);
143 case LINUX_IP_IPSEC_POLICY:
144 /* we have this option, but not documented in ip(4) manpage */
145 LINUX_RATELIMIT_MSG_NOTTESTED("IPv4 socket option IP_IPSEC_POLICY");
146 return (IP_IPSEC_POLICY);
147 case LINUX_IP_MINTTL:
148 LINUX_RATELIMIT_MSG_NOTTESTED("IPv4 socket option IP_MINTTL");
149 return (IP_MINTTL);
150 case LINUX_IP_MULTICAST_IF:
151 return (IP_MULTICAST_IF);
152 case LINUX_IP_MULTICAST_TTL:
153 return (IP_MULTICAST_TTL);
154 case LINUX_IP_MULTICAST_LOOP:
155 return (IP_MULTICAST_LOOP);
156 case LINUX_IP_ADD_MEMBERSHIP:
157 return (IP_ADD_MEMBERSHIP);
158 case LINUX_IP_DROP_MEMBERSHIP:
159 return (IP_DROP_MEMBERSHIP);
160 case LINUX_IP_UNBLOCK_SOURCE:
161 LINUX_RATELIMIT_MSG_NOTTESTED("IPv4 socket option IP_UNBLOCK_SOURCE");
162 return (IP_UNBLOCK_SOURCE);
163 case LINUX_IP_BLOCK_SOURCE:
164 LINUX_RATELIMIT_MSG_NOTTESTED("IPv4 socket option IP_BLOCK_SOURCE");
165 return (IP_BLOCK_SOURCE);
166 case LINUX_IP_ADD_SOURCE_MEMBERSHIP:
167 LINUX_RATELIMIT_MSG_NOTTESTED("IPv4 socket option IP_ADD_SOURCE_MEMBERSHIP");
168 return (IP_ADD_SOURCE_MEMBERSHIP);
169 case LINUX_IP_DROP_SOURCE_MEMBERSHIP:
170 LINUX_RATELIMIT_MSG_NOTTESTED("IPv4 socket option IP_DROP_SOURCE_MEMBERSHIP");
171 return (IP_DROP_SOURCE_MEMBERSHIP);
172 case LINUX_MCAST_JOIN_GROUP:
173 LINUX_RATELIMIT_MSG_NOTTESTED("IPv4 socket option IP_MCAST_JOIN_GROUP");
174 return (MCAST_JOIN_GROUP);
175 case LINUX_MCAST_LEAVE_GROUP:
176 LINUX_RATELIMIT_MSG_NOTTESTED("IPv4 socket option IP_MCAST_LEAVE_GROUP");
177 return (MCAST_LEAVE_GROUP);
178 case LINUX_MCAST_JOIN_SOURCE_GROUP:
179 LINUX_RATELIMIT_MSG_NOTTESTED("IPv4 socket option IP_MCAST_JOIN_SOURCE_GROUP");
180 return (MCAST_JOIN_SOURCE_GROUP);
181 case LINUX_MCAST_LEAVE_SOURCE_GROUP:
182 LINUX_RATELIMIT_MSG_NOTTESTED("IPv4 socket option IP_MCAST_LEAVE_SOURCE_GROUP");
183 return (MCAST_LEAVE_SOURCE_GROUP);
184 case LINUX_IP_RECVORIGDSTADDR:
185 return (IP_RECVORIGDSTADDR);
186
187 /* known but not implemented sockopts */
188 case LINUX_IP_ROUTER_ALERT:
189 LINUX_RATELIMIT_MSG_OPT1(
190 "unsupported IPv4 socket option IP_ROUTER_ALERT (%d), you can not do user-space routing from linux programs",
191 opt);
192 return (-2);
193 case LINUX_IP_PKTINFO:
194 LINUX_RATELIMIT_MSG_OPT1(
195 "unsupported IPv4 socket option IP_PKTINFO (%d), you can not get extended packet info for datagram sockets in linux programs",
196 opt);
197 return (-2);
198 case LINUX_IP_PKTOPTIONS:
199 LINUX_RATELIMIT_MSG_OPT1(
200 "unsupported IPv4 socket option IP_PKTOPTIONS (%d)",
201 opt);
202 return (-2);
203 case LINUX_IP_MTU_DISCOVER:
204 LINUX_RATELIMIT_MSG_OPT1(
205 "unsupported IPv4 socket option IP_MTU_DISCOVER (%d), your linux program can not control path-MTU discovery",
206 opt);
207 return (-2);
208 case LINUX_IP_RECVERR:
209 /* needed by steam */
210 LINUX_RATELIMIT_MSG_OPT1(
211 "unsupported IPv4 socket option IP_RECVERR (%d), you can not get extended reliability info in linux programs",
212 opt);
213 return (-2);
214 case LINUX_IP_MTU:
215 LINUX_RATELIMIT_MSG_OPT1(
216 "unsupported IPv4 socket option IP_MTU (%d), your linux program can not control the MTU on this socket",
217 opt);
218 return (-2);
219 case LINUX_IP_XFRM_POLICY:
220 LINUX_RATELIMIT_MSG_OPT1(
221 "unsupported IPv4 socket option IP_XFRM_POLICY (%d)",
222 opt);
223 return (-2);
224 case LINUX_IP_PASSSEC:
225 /* needed by steam */
226 LINUX_RATELIMIT_MSG_OPT1(
227 "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",
228 opt);
229 return (-2);
230 case LINUX_IP_TRANSPARENT:
231 /* IP_BINDANY or more? */
232 LINUX_RATELIMIT_MSG_OPT1(
233 "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",
234 opt);
235 return (-2);
236 case LINUX_IP_NODEFRAG:
237 LINUX_RATELIMIT_MSG_OPT1(
238 "unsupported IPv4 socket option IP_NODEFRAG (%d)",
239 opt);
240 return (-2);
241 case LINUX_IP_CHECKSUM:
242 LINUX_RATELIMIT_MSG_OPT1(
243 "unsupported IPv4 socket option IP_CHECKSUM (%d)",
244 opt);
245 return (-2);
246 case LINUX_IP_BIND_ADDRESS_NO_PORT:
247 LINUX_RATELIMIT_MSG_OPT1(
248 "unsupported IPv4 socket option IP_BIND_ADDRESS_NO_PORT (%d)",
249 opt);
250 return (-2);
251 case LINUX_IP_RECVFRAGSIZE:
252 LINUX_RATELIMIT_MSG_OPT1(
253 "unsupported IPv4 socket option IP_RECVFRAGSIZE (%d)",
254 opt);
255 return (-2);
256 case LINUX_MCAST_MSFILTER:
257 LINUX_RATELIMIT_MSG_OPT1(
258 "unsupported IPv4 socket option IP_MCAST_MSFILTER (%d)",
259 opt);
260 return (-2);
261 case LINUX_IP_MULTICAST_ALL:
262 LINUX_RATELIMIT_MSG_OPT1(
263 "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",
264 opt);
265 return (-2);
266 case LINUX_IP_UNICAST_IF:
267 LINUX_RATELIMIT_MSG_OPT1(
268 "unsupported IPv4 socket option IP_UNICAST_IF (%d)",
269 opt);
270 return (-2);
271
272 /* unknown sockopts */
273 default:
274 return (-1);
275 }
276 }
277
278 static int
linux_to_bsd_ip6_sockopt(int opt)279 linux_to_bsd_ip6_sockopt(int opt)
280 {
281
282 switch (opt) {
283 /* known and translated sockopts */
284 case LINUX_IPV6_2292PKTINFO:
285 LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_2292PKTINFO");
286 return (IPV6_2292PKTINFO);
287 case LINUX_IPV6_2292HOPOPTS:
288 LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_2292HOPOPTS");
289 return (IPV6_2292HOPOPTS);
290 case LINUX_IPV6_2292DSTOPTS:
291 LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_2292DSTOPTS");
292 return (IPV6_2292DSTOPTS);
293 case LINUX_IPV6_2292RTHDR:
294 LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_2292RTHDR");
295 return (IPV6_2292RTHDR);
296 case LINUX_IPV6_2292PKTOPTIONS:
297 LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_2292PKTOPTIONS");
298 return (IPV6_2292PKTOPTIONS);
299 case LINUX_IPV6_CHECKSUM:
300 LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_CHECKSUM");
301 return (IPV6_CHECKSUM);
302 case LINUX_IPV6_2292HOPLIMIT:
303 LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_2292HOPLIMIT");
304 return (IPV6_2292HOPLIMIT);
305 case LINUX_IPV6_NEXTHOP:
306 return (IPV6_NEXTHOP);
307 case LINUX_IPV6_UNICAST_HOPS:
308 return (IPV6_UNICAST_HOPS);
309 case LINUX_IPV6_MULTICAST_IF:
310 return (IPV6_MULTICAST_IF);
311 case LINUX_IPV6_MULTICAST_HOPS:
312 return (IPV6_MULTICAST_HOPS);
313 case LINUX_IPV6_MULTICAST_LOOP:
314 return (IPV6_MULTICAST_LOOP);
315 case LINUX_IPV6_ADD_MEMBERSHIP:
316 return (IPV6_JOIN_GROUP);
317 case LINUX_IPV6_DROP_MEMBERSHIP:
318 return (IPV6_LEAVE_GROUP);
319 case LINUX_IPV6_V6ONLY:
320 return (IPV6_V6ONLY);
321 case LINUX_IPV6_IPSEC_POLICY:
322 /* we have this option, but not documented in ip6(4) manpage */
323 LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_IPSEC_POLICY");
324 return (IPV6_IPSEC_POLICY);
325 case LINUX_MCAST_JOIN_GROUP:
326 LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_JOIN_GROUP");
327 return (IPV6_JOIN_GROUP);
328 case LINUX_MCAST_LEAVE_GROUP:
329 LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_LEAVE_GROUP");
330 return (IPV6_LEAVE_GROUP);
331 case LINUX_IPV6_RECVPKTINFO:
332 LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_RECVPKTINFO");
333 return (IPV6_RECVPKTINFO);
334 case LINUX_IPV6_PKTINFO:
335 LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_PKTINFO");
336 return (IPV6_PKTINFO);
337 case LINUX_IPV6_RECVHOPLIMIT:
338 LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_RECVHOPLIMIT");
339 return (IPV6_RECVHOPLIMIT);
340 case LINUX_IPV6_HOPLIMIT:
341 LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_HOPLIMIT");
342 return (IPV6_HOPLIMIT);
343 case LINUX_IPV6_RECVHOPOPTS:
344 LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_RECVHOPOPTS");
345 return (IPV6_RECVHOPOPTS);
346 case LINUX_IPV6_HOPOPTS:
347 LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_HOPOPTS");
348 return (IPV6_HOPOPTS);
349 case LINUX_IPV6_RTHDRDSTOPTS:
350 LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_RTHDRDSTOPTS");
351 return (IPV6_RTHDRDSTOPTS);
352 case LINUX_IPV6_RECVRTHDR:
353 LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_RECVRTHDR");
354 return (IPV6_RECVRTHDR);
355 case LINUX_IPV6_RTHDR:
356 LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_RTHDR");
357 return (IPV6_RTHDR);
358 case LINUX_IPV6_RECVDSTOPTS:
359 LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_RECVDSTOPTS");
360 return (IPV6_RECVDSTOPTS);
361 case LINUX_IPV6_DSTOPTS:
362 LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_DSTOPTS");
363 return (IPV6_DSTOPTS);
364 case LINUX_IPV6_RECVPATHMTU:
365 LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_RECVPATHMTU");
366 return (IPV6_RECVPATHMTU);
367 case LINUX_IPV6_PATHMTU:
368 LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_PATHMTU");
369 return (IPV6_PATHMTU);
370 case LINUX_IPV6_DONTFRAG:
371 return (IPV6_DONTFRAG);
372 case LINUX_IPV6_AUTOFLOWLABEL:
373 LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_AUTOFLOWLABEL");
374 return (IPV6_AUTOFLOWLABEL);
375 case LINUX_IPV6_ORIGDSTADDR:
376 LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_ORIGDSTADDR");
377 return (IPV6_ORIGDSTADDR);
378 case LINUX_IPV6_FREEBIND:
379 LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_FREEBIND");
380 return (IPV6_BINDANY);
381
382 /* known but not implemented sockopts */
383 case LINUX_IPV6_ADDRFORM:
384 LINUX_RATELIMIT_MSG_OPT1(
385 "unsupported IPv6 socket option IPV6_ADDRFORM (%d), you linux program can not convert the socket to IPv4",
386 opt);
387 return (-2);
388 case LINUX_IPV6_AUTHHDR:
389 LINUX_RATELIMIT_MSG_OPT1(
390 "unsupported IPv6 socket option IPV6_AUTHHDR (%d), your linux program can not get the authentication header info of IPv6 packets",
391 opt);
392 return (-2);
393 case LINUX_IPV6_FLOWINFO:
394 LINUX_RATELIMIT_MSG_OPT1(
395 "unsupported IPv6 socket option IPV6_FLOWINFO (%d), your linux program can not get the flowid of IPv6 packets",
396 opt);
397 return (-2);
398 case LINUX_IPV6_ROUTER_ALERT:
399 LINUX_RATELIMIT_MSG_OPT1(
400 "unsupported IPv6 socket option IPV6_ROUTER_ALERT (%d), you can not do user-space routing from linux programs",
401 opt);
402 return (-2);
403 case LINUX_IPV6_MTU_DISCOVER:
404 LINUX_RATELIMIT_MSG_OPT1(
405 "unsupported IPv6 socket option IPV6_MTU_DISCOVER (%d), your linux program can not control path-MTU discovery",
406 opt);
407 return (-2);
408 case LINUX_IPV6_MTU:
409 LINUX_RATELIMIT_MSG_OPT1(
410 "unsupported IPv6 socket option IPV6_MTU (%d), your linux program can not control the MTU on this socket",
411 opt);
412 return (-2);
413 case LINUX_IPV6_JOIN_ANYCAST:
414 LINUX_RATELIMIT_MSG_OPT1(
415 "unsupported IPv6 socket option IPV6_JOIN_ANYCAST (%d)",
416 opt);
417 return (-2);
418 case LINUX_IPV6_LEAVE_ANYCAST:
419 LINUX_RATELIMIT_MSG_OPT1(
420 "unsupported IPv6 socket option IPV6_LEAVE_ANYCAST (%d)",
421 opt);
422 return (-2);
423 case LINUX_IPV6_MULTICAST_ALL:
424 LINUX_RATELIMIT_MSG_OPT1(
425 "unsupported IPv6 socket option IPV6_MULTICAST_ALL (%d)",
426 opt);
427 return (-2);
428 case LINUX_IPV6_ROUTER_ALERT_ISOLATE:
429 LINUX_RATELIMIT_MSG_OPT1(
430 "unsupported IPv6 socket option IPV6_ROUTER_ALERT_ISOLATE (%d)",
431 opt);
432 return (-2);
433 case LINUX_IPV6_FLOWLABEL_MGR:
434 LINUX_RATELIMIT_MSG_OPT1(
435 "unsupported IPv6 socket option IPV6_FLOWLABEL_MGR (%d)",
436 opt);
437 return (-2);
438 case LINUX_IPV6_FLOWINFO_SEND:
439 LINUX_RATELIMIT_MSG_OPT1(
440 "unsupported IPv6 socket option IPV6_FLOWINFO_SEND (%d)",
441 opt);
442 return (-2);
443 case LINUX_IPV6_XFRM_POLICY:
444 LINUX_RATELIMIT_MSG_OPT1(
445 "unsupported IPv6 socket option IPV6_XFRM_POLICY (%d)",
446 opt);
447 return (-2);
448 case LINUX_IPV6_HDRINCL:
449 LINUX_RATELIMIT_MSG_OPT1(
450 "unsupported IPv6 socket option IPV6_HDRINCL (%d)",
451 opt);
452 return (-2);
453 case LINUX_MCAST_BLOCK_SOURCE:
454 LINUX_RATELIMIT_MSG_OPT1(
455 "unsupported IPv6 socket option MCAST_BLOCK_SOURCE (%d), your linux program may see more multicast stuff than it wants",
456 opt);
457 return (-2);
458 case LINUX_MCAST_UNBLOCK_SOURCE:
459 LINUX_RATELIMIT_MSG_OPT1(
460 "unsupported IPv6 socket option MCAST_UNBLOCK_SOURCE (%d), your linux program may not see all the multicast stuff it wants",
461 opt);
462 return (-2);
463 case LINUX_MCAST_JOIN_SOURCE_GROUP:
464 LINUX_RATELIMIT_MSG_OPT1(
465 "unsupported IPv6 socket option MCAST_JOIN_SOURCE_GROUP (%d), your linux program is not able to join a multicast source group",
466 opt);
467 return (-2);
468 case LINUX_MCAST_LEAVE_SOURCE_GROUP:
469 LINUX_RATELIMIT_MSG_OPT1(
470 "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",
471 opt);
472 return (-2);
473 case LINUX_MCAST_MSFILTER:
474 LINUX_RATELIMIT_MSG_OPT1(
475 "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",
476 opt);
477 return (-2);
478 case LINUX_IPV6_ADDR_PREFERENCES:
479 LINUX_RATELIMIT_MSG_OPT1(
480 "unsupported IPv6 socket option IPV6_ADDR_PREFERENCES (%d)",
481 opt);
482 return (-2);
483 case LINUX_IPV6_MINHOPCOUNT:
484 LINUX_RATELIMIT_MSG_OPT1(
485 "unsupported IPv6 socket option IPV6_MINHOPCOUNT (%d)",
486 opt);
487 return (-2);
488 case LINUX_IPV6_TRANSPARENT:
489 /* IP_BINDANY or more? */
490 LINUX_RATELIMIT_MSG_OPT1(
491 "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",
492 opt);
493 return (-2);
494 case LINUX_IPV6_UNICAST_IF:
495 LINUX_RATELIMIT_MSG_OPT1(
496 "unsupported IPv6 socket option IPV6_UNICAST_IF (%d)",
497 opt);
498 return (-2);
499 case LINUX_IPV6_RECVFRAGSIZE:
500 LINUX_RATELIMIT_MSG_OPT1(
501 "unsupported IPv6 socket option IPV6_RECVFRAGSIZE (%d)",
502 opt);
503 return (-2);
504 case LINUX_IPV6_RECVERR:
505 LINUX_RATELIMIT_MSG_OPT1(
506 "unsupported IPv6 socket option IPV6_RECVERR (%d), you can not get extended reliability info in linux programs",
507 opt);
508 return (-2);
509
510 /* unknown sockopts */
511 default:
512 return (-1);
513 }
514 }
515
516 static int
linux_to_bsd_so_sockopt(int opt)517 linux_to_bsd_so_sockopt(int opt)
518 {
519
520 switch (opt) {
521 case LINUX_SO_DEBUG:
522 return (SO_DEBUG);
523 case LINUX_SO_REUSEADDR:
524 return (SO_REUSEADDR);
525 case LINUX_SO_TYPE:
526 return (SO_TYPE);
527 case LINUX_SO_ERROR:
528 return (SO_ERROR);
529 case LINUX_SO_DONTROUTE:
530 return (SO_DONTROUTE);
531 case LINUX_SO_BROADCAST:
532 return (SO_BROADCAST);
533 case LINUX_SO_SNDBUF:
534 case LINUX_SO_SNDBUFFORCE:
535 return (SO_SNDBUF);
536 case LINUX_SO_RCVBUF:
537 case LINUX_SO_RCVBUFFORCE:
538 return (SO_RCVBUF);
539 case LINUX_SO_KEEPALIVE:
540 return (SO_KEEPALIVE);
541 case LINUX_SO_OOBINLINE:
542 return (SO_OOBINLINE);
543 case LINUX_SO_LINGER:
544 return (SO_LINGER);
545 case LINUX_SO_REUSEPORT:
546 return (SO_REUSEPORT_LB);
547 case LINUX_SO_PASSCRED:
548 return (LOCAL_CREDS_PERSISTENT);
549 case LINUX_SO_PEERCRED:
550 return (LOCAL_PEERCRED);
551 case LINUX_SO_RCVLOWAT:
552 return (SO_RCVLOWAT);
553 case LINUX_SO_SNDLOWAT:
554 return (SO_SNDLOWAT);
555 case LINUX_SO_RCVTIMEO:
556 return (SO_RCVTIMEO);
557 case LINUX_SO_SNDTIMEO:
558 return (SO_SNDTIMEO);
559 case LINUX_SO_TIMESTAMPO:
560 case LINUX_SO_TIMESTAMPN:
561 return (SO_TIMESTAMP);
562 case LINUX_SO_TIMESTAMPNSO:
563 case LINUX_SO_TIMESTAMPNSN:
564 return (SO_BINTIME);
565 case LINUX_SO_ACCEPTCONN:
566 return (SO_ACCEPTCONN);
567 case LINUX_SO_PROTOCOL:
568 return (SO_PROTOCOL);
569 case LINUX_SO_DOMAIN:
570 return (SO_DOMAIN);
571 }
572 return (-1);
573 }
574
575 static int
linux_to_bsd_tcp_sockopt(int opt)576 linux_to_bsd_tcp_sockopt(int opt)
577 {
578
579 switch (opt) {
580 case LINUX_TCP_NODELAY:
581 return (TCP_NODELAY);
582 case LINUX_TCP_MAXSEG:
583 return (TCP_MAXSEG);
584 case LINUX_TCP_CORK:
585 return (TCP_NOPUSH);
586 case LINUX_TCP_KEEPIDLE:
587 return (TCP_KEEPIDLE);
588 case LINUX_TCP_KEEPINTVL:
589 return (TCP_KEEPINTVL);
590 case LINUX_TCP_KEEPCNT:
591 return (TCP_KEEPCNT);
592 case LINUX_TCP_INFO:
593 LINUX_RATELIMIT_MSG_OPT1(
594 "unsupported TCP socket option TCP_INFO (%d)", opt);
595 return (-2);
596 case LINUX_TCP_MD5SIG:
597 return (TCP_MD5SIG);
598 }
599 return (-1);
600 }
601
602 static int
linux_to_bsd_msg_flags(int flags)603 linux_to_bsd_msg_flags(int flags)
604 {
605 int ret_flags = 0;
606
607 if (flags & LINUX_MSG_OOB)
608 ret_flags |= MSG_OOB;
609 if (flags & LINUX_MSG_PEEK)
610 ret_flags |= MSG_PEEK;
611 if (flags & LINUX_MSG_DONTROUTE)
612 ret_flags |= MSG_DONTROUTE;
613 if (flags & LINUX_MSG_CTRUNC)
614 ret_flags |= MSG_CTRUNC;
615 if (flags & LINUX_MSG_TRUNC)
616 ret_flags |= MSG_TRUNC;
617 if (flags & LINUX_MSG_DONTWAIT)
618 ret_flags |= MSG_DONTWAIT;
619 if (flags & LINUX_MSG_EOR)
620 ret_flags |= MSG_EOR;
621 if (flags & LINUX_MSG_WAITALL)
622 ret_flags |= MSG_WAITALL;
623 if (flags & LINUX_MSG_NOSIGNAL)
624 ret_flags |= MSG_NOSIGNAL;
625 if (flags & LINUX_MSG_PROXY)
626 LINUX_RATELIMIT_MSG_OPT1("socket message flag MSG_PROXY (%d) not handled",
627 LINUX_MSG_PROXY);
628 if (flags & LINUX_MSG_FIN)
629 LINUX_RATELIMIT_MSG_OPT1("socket message flag MSG_FIN (%d) not handled",
630 LINUX_MSG_FIN);
631 if (flags & LINUX_MSG_SYN)
632 LINUX_RATELIMIT_MSG_OPT1("socket message flag MSG_SYN (%d) not handled",
633 LINUX_MSG_SYN);
634 if (flags & LINUX_MSG_CONFIRM)
635 LINUX_RATELIMIT_MSG_OPT1("socket message flag MSG_CONFIRM (%d) not handled",
636 LINUX_MSG_CONFIRM);
637 if (flags & LINUX_MSG_RST)
638 LINUX_RATELIMIT_MSG_OPT1("socket message flag MSG_RST (%d) not handled",
639 LINUX_MSG_RST);
640 if (flags & LINUX_MSG_ERRQUEUE)
641 LINUX_RATELIMIT_MSG_OPT1("socket message flag MSG_ERRQUEUE (%d) not handled",
642 LINUX_MSG_ERRQUEUE);
643 return (ret_flags);
644 }
645
646 static int
linux_to_bsd_cmsg_type(int cmsg_type)647 linux_to_bsd_cmsg_type(int cmsg_type)
648 {
649
650 switch (cmsg_type) {
651 case LINUX_SCM_RIGHTS:
652 return (SCM_RIGHTS);
653 case LINUX_SCM_CREDENTIALS:
654 return (SCM_CREDS);
655 }
656 return (-1);
657 }
658
659 static int
bsd_to_linux_ip_cmsg_type(int cmsg_type)660 bsd_to_linux_ip_cmsg_type(int cmsg_type)
661 {
662
663 switch (cmsg_type) {
664 case IP_RECVORIGDSTADDR:
665 return (LINUX_IP_RECVORIGDSTADDR);
666 }
667 return (-1);
668 }
669
670 static int
bsd_to_linux_cmsg_type(struct proc * p,int cmsg_type,int cmsg_level)671 bsd_to_linux_cmsg_type(struct proc *p, int cmsg_type, int cmsg_level)
672 {
673 struct linux_pemuldata *pem;
674
675 if (cmsg_level == IPPROTO_IP)
676 return (bsd_to_linux_ip_cmsg_type(cmsg_type));
677 if (cmsg_level != SOL_SOCKET)
678 return (-1);
679
680 pem = pem_find(p);
681
682 switch (cmsg_type) {
683 case SCM_RIGHTS:
684 return (LINUX_SCM_RIGHTS);
685 case SCM_CREDS:
686 return (LINUX_SCM_CREDENTIALS);
687 case SCM_CREDS2:
688 return (LINUX_SCM_CREDENTIALS);
689 case SCM_TIMESTAMP:
690 return (pem->so_timestamp);
691 case SCM_BINTIME:
692 return (pem->so_timestampns);
693 }
694 return (-1);
695 }
696
697 static int
linux_to_bsd_msghdr(struct msghdr * bhdr,const struct l_msghdr * lhdr)698 linux_to_bsd_msghdr(struct msghdr *bhdr, const struct l_msghdr *lhdr)
699 {
700 if (lhdr->msg_controllen > INT_MAX)
701 return (ENOBUFS);
702
703 bhdr->msg_name = PTRIN(lhdr->msg_name);
704 bhdr->msg_namelen = lhdr->msg_namelen;
705 bhdr->msg_iov = PTRIN(lhdr->msg_iov);
706 bhdr->msg_iovlen = lhdr->msg_iovlen;
707 bhdr->msg_control = PTRIN(lhdr->msg_control);
708
709 /*
710 * msg_controllen is skipped since BSD and LINUX control messages
711 * are potentially different sizes (e.g. the cred structure used
712 * by SCM_CREDS is different between the two operating system).
713 *
714 * The caller can set it (if necessary) after converting all the
715 * control messages.
716 */
717
718 bhdr->msg_flags = linux_to_bsd_msg_flags(lhdr->msg_flags);
719 return (0);
720 }
721
722 static int
bsd_to_linux_msghdr(const struct msghdr * bhdr,struct l_msghdr * lhdr)723 bsd_to_linux_msghdr(const struct msghdr *bhdr, struct l_msghdr *lhdr)
724 {
725 lhdr->msg_name = PTROUT(bhdr->msg_name);
726 lhdr->msg_namelen = bhdr->msg_namelen;
727 lhdr->msg_iov = PTROUT(bhdr->msg_iov);
728 lhdr->msg_iovlen = bhdr->msg_iovlen;
729 lhdr->msg_control = PTROUT(bhdr->msg_control);
730
731 /*
732 * msg_controllen is skipped since BSD and LINUX control messages
733 * are potentially different sizes (e.g. the cred structure used
734 * by SCM_CREDS is different between the two operating system).
735 *
736 * The caller can set it (if necessary) after converting all the
737 * control messages.
738 */
739
740 /* msg_flags skipped */
741 return (0);
742 }
743
744 static int
linux_set_socket_flags(int lflags,int * flags)745 linux_set_socket_flags(int lflags, int *flags)
746 {
747
748 if (lflags & ~(LINUX_SOCK_CLOEXEC | LINUX_SOCK_NONBLOCK))
749 return (EINVAL);
750 if (lflags & LINUX_SOCK_NONBLOCK)
751 *flags |= SOCK_NONBLOCK;
752 if (lflags & LINUX_SOCK_CLOEXEC)
753 *flags |= SOCK_CLOEXEC;
754 return (0);
755 }
756
757 static int
linux_copyout_sockaddr(const struct sockaddr * sa,void * uaddr,size_t len)758 linux_copyout_sockaddr(const struct sockaddr *sa, void *uaddr, size_t len)
759 {
760 struct l_sockaddr *lsa;
761 int error;
762
763 error = bsd_to_linux_sockaddr(sa, &lsa, len);
764 if (error != 0)
765 return (error);
766
767 error = copyout(lsa, uaddr, len);
768 free(lsa, M_LINUX);
769
770 return (error);
771 }
772
773 static int
linux_sendit(struct thread * td,int s,struct msghdr * mp,int flags,struct mbuf * control,enum uio_seg segflg)774 linux_sendit(struct thread *td, int s, struct msghdr *mp, int flags,
775 struct mbuf *control, enum uio_seg segflg)
776 {
777 struct sockaddr *to;
778 int error, len;
779
780 if (mp->msg_name != NULL) {
781 len = mp->msg_namelen;
782 error = linux_to_bsd_sockaddr(mp->msg_name, &to, &len);
783 if (error != 0)
784 return (error);
785 mp->msg_name = to;
786 } else
787 to = NULL;
788
789 error = kern_sendit(td, s, mp, linux_to_bsd_msg_flags(flags), control,
790 segflg);
791
792 if (to)
793 free(to, M_SONAME);
794 return (error);
795 }
796
797 /* Return 0 if IP_HDRINCL is set for the given socket. */
798 static int
linux_check_hdrincl(struct thread * td,int s)799 linux_check_hdrincl(struct thread *td, int s)
800 {
801 int error, optval;
802 socklen_t size_val;
803
804 size_val = sizeof(optval);
805 error = kern_getsockopt(td, s, IPPROTO_IP, IP_HDRINCL,
806 &optval, UIO_SYSSPACE, &size_val);
807 if (error != 0)
808 return (error);
809
810 return (optval == 0);
811 }
812
813 /*
814 * Updated sendto() when IP_HDRINCL is set:
815 * tweak endian-dependent fields in the IP packet.
816 */
817 static int
linux_sendto_hdrincl(struct thread * td,struct linux_sendto_args * linux_args)818 linux_sendto_hdrincl(struct thread *td, struct linux_sendto_args *linux_args)
819 {
820 /*
821 * linux_ip_copysize defines how many bytes we should copy
822 * from the beginning of the IP packet before we customize it for BSD.
823 * It should include all the fields we modify (ip_len and ip_off).
824 */
825 #define linux_ip_copysize 8
826
827 struct ip *packet;
828 struct msghdr msg;
829 struct iovec aiov[1];
830 int error;
831
832 /* Check that the packet isn't too big or too small. */
833 if (linux_args->len < linux_ip_copysize ||
834 linux_args->len > IP_MAXPACKET)
835 return (EINVAL);
836
837 packet = (struct ip *)malloc(linux_args->len, M_LINUX, M_WAITOK);
838
839 /* Make kernel copy of the packet to be sent */
840 if ((error = copyin(PTRIN(linux_args->msg), packet,
841 linux_args->len)))
842 goto goout;
843
844 /* Convert fields from Linux to BSD raw IP socket format */
845 packet->ip_len = linux_args->len;
846 packet->ip_off = ntohs(packet->ip_off);
847
848 /* Prepare the msghdr and iovec structures describing the new packet */
849 msg.msg_name = PTRIN(linux_args->to);
850 msg.msg_namelen = linux_args->tolen;
851 msg.msg_iov = aiov;
852 msg.msg_iovlen = 1;
853 msg.msg_control = NULL;
854 msg.msg_flags = 0;
855 aiov[0].iov_base = (char *)packet;
856 aiov[0].iov_len = linux_args->len;
857 error = linux_sendit(td, linux_args->s, &msg, linux_args->flags,
858 NULL, UIO_SYSSPACE);
859 goout:
860 free(packet, M_LINUX);
861 return (error);
862 }
863
864 static const char *linux_netlink_names[] = {
865 [LINUX_NETLINK_ROUTE] = "ROUTE",
866 [LINUX_NETLINK_SOCK_DIAG] = "SOCK_DIAG",
867 [LINUX_NETLINK_NFLOG] = "NFLOG",
868 [LINUX_NETLINK_SELINUX] = "SELINUX",
869 [LINUX_NETLINK_AUDIT] = "AUDIT",
870 [LINUX_NETLINK_FIB_LOOKUP] = "FIB_LOOKUP",
871 [LINUX_NETLINK_NETFILTER] = "NETFILTER",
872 [LINUX_NETLINK_KOBJECT_UEVENT] = "KOBJECT_UEVENT",
873 };
874
875 int
linux_socket(struct thread * td,struct linux_socket_args * args)876 linux_socket(struct thread *td, struct linux_socket_args *args)
877 {
878 int retval_socket, type;
879 sa_family_t domain;
880
881 type = args->type & LINUX_SOCK_TYPE_MASK;
882 if (type < 0 || type > LINUX_SOCK_MAX)
883 return (EINVAL);
884 retval_socket = linux_set_socket_flags(args->type & ~LINUX_SOCK_TYPE_MASK,
885 &type);
886 if (retval_socket != 0)
887 return (retval_socket);
888 domain = linux_to_bsd_domain(args->domain);
889 if (domain == AF_UNKNOWN) {
890 /* Mask off SOCK_NONBLOCK / CLOEXEC for error messages. */
891 type = args->type & LINUX_SOCK_TYPE_MASK;
892 if (args->domain == LINUX_AF_NETLINK &&
893 args->protocol == LINUX_NETLINK_AUDIT) {
894 ; /* Do nothing, quietly. */
895 } else if (args->domain == LINUX_AF_NETLINK) {
896 const char *nl_name;
897
898 if (args->protocol >= 0 &&
899 args->protocol < nitems(linux_netlink_names))
900 nl_name = linux_netlink_names[args->protocol];
901 else
902 nl_name = NULL;
903 if (nl_name != NULL)
904 linux_msg(curthread,
905 "unsupported socket(AF_NETLINK, %d, "
906 "NETLINK_%s)", type, nl_name);
907 else
908 linux_msg(curthread,
909 "unsupported socket(AF_NETLINK, %d, %d)",
910 type, args->protocol);
911 } else {
912 linux_msg(curthread, "unsupported socket domain %d, "
913 "type %d, protocol %d", args->domain, type,
914 args->protocol);
915 }
916 return (EAFNOSUPPORT);
917 }
918
919 retval_socket = kern_socket(td, domain, type, args->protocol);
920 if (retval_socket)
921 return (retval_socket);
922
923 if (type == SOCK_RAW
924 && (args->protocol == IPPROTO_RAW || args->protocol == 0)
925 && domain == PF_INET) {
926 /* It's a raw IP socket: set the IP_HDRINCL option. */
927 int hdrincl;
928
929 hdrincl = 1;
930 /* We ignore any error returned by kern_setsockopt() */
931 kern_setsockopt(td, td->td_retval[0], IPPROTO_IP, IP_HDRINCL,
932 &hdrincl, UIO_SYSSPACE, sizeof(hdrincl));
933 }
934 #ifdef INET6
935 /*
936 * Linux AF_INET6 socket has IPV6_V6ONLY setsockopt set to 0 by default
937 * and some apps depend on this. So, set V6ONLY to 0 for Linux apps.
938 * For simplicity we do this unconditionally of the net.inet6.ip6.v6only
939 * sysctl value.
940 */
941 if (domain == PF_INET6) {
942 int v6only;
943
944 v6only = 0;
945 /* We ignore any error returned by setsockopt() */
946 kern_setsockopt(td, td->td_retval[0], IPPROTO_IPV6, IPV6_V6ONLY,
947 &v6only, UIO_SYSSPACE, sizeof(v6only));
948 }
949 #endif
950
951 return (retval_socket);
952 }
953
954 int
linux_bind(struct thread * td,struct linux_bind_args * args)955 linux_bind(struct thread *td, struct linux_bind_args *args)
956 {
957 struct sockaddr *sa;
958 int error;
959
960 error = linux_to_bsd_sockaddr(PTRIN(args->name), &sa,
961 &args->namelen);
962 if (error != 0)
963 return (error);
964
965 error = kern_bindat(td, AT_FDCWD, args->s, sa);
966 free(sa, M_SONAME);
967
968 /* XXX */
969 if (error == EADDRNOTAVAIL && args->namelen != sizeof(struct sockaddr_in))
970 return (EINVAL);
971 return (error);
972 }
973
974 int
linux_connect(struct thread * td,struct linux_connect_args * args)975 linux_connect(struct thread *td, struct linux_connect_args *args)
976 {
977 struct socket *so;
978 struct sockaddr *sa;
979 struct file *fp;
980 int error;
981
982 error = linux_to_bsd_sockaddr(PTRIN(args->name), &sa,
983 &args->namelen);
984 if (error != 0)
985 return (error);
986
987 error = kern_connectat(td, AT_FDCWD, args->s, sa);
988 free(sa, M_SONAME);
989 if (error != EISCONN)
990 return (error);
991
992 /*
993 * Linux doesn't return EISCONN the first time it occurs,
994 * when on a non-blocking socket. Instead it returns the
995 * error getsockopt(SOL_SOCKET, SO_ERROR) would return on BSD.
996 */
997 error = getsock(td, args->s, &cap_connect_rights, &fp);
998 if (error != 0)
999 return (error);
1000
1001 error = EISCONN;
1002 so = fp->f_data;
1003 if (atomic_load_int(&fp->f_flag) & FNONBLOCK) {
1004 SOCK_LOCK(so);
1005 if (so->so_emuldata == 0)
1006 error = so->so_error;
1007 so->so_emuldata = (void *)1;
1008 SOCK_UNLOCK(so);
1009 }
1010 fdrop(fp, td);
1011
1012 return (error);
1013 }
1014
1015 int
linux_listen(struct thread * td,struct linux_listen_args * args)1016 linux_listen(struct thread *td, struct linux_listen_args *args)
1017 {
1018
1019 return (kern_listen(td, args->s, args->backlog));
1020 }
1021
1022 static int
linux_accept_common(struct thread * td,int s,l_uintptr_t addr,l_uintptr_t namelen,int flags)1023 linux_accept_common(struct thread *td, int s, l_uintptr_t addr,
1024 l_uintptr_t namelen, int flags)
1025 {
1026 struct sockaddr_storage ss = { .ss_len = sizeof(ss) };
1027 struct file *fp, *fp1;
1028 struct socket *so;
1029 socklen_t len;
1030 int bflags, error, error1;
1031
1032 bflags = 0;
1033 fp = NULL;
1034
1035 error = linux_set_socket_flags(flags, &bflags);
1036 if (error != 0)
1037 return (error);
1038
1039 if (PTRIN(addr) != NULL) {
1040 error = copyin(PTRIN(namelen), &len, sizeof(len));
1041 if (error != 0)
1042 return (error);
1043 if (len < 0)
1044 return (EINVAL);
1045 } else
1046 len = 0;
1047
1048 error = kern_accept4(td, s, (struct sockaddr *)&ss, bflags, &fp);
1049
1050 /*
1051 * Translate errno values into ones used by Linux.
1052 */
1053 if (error != 0) {
1054 /*
1055 * XXX. This is wrong, different sockaddr structures
1056 * have different sizes.
1057 */
1058 switch (error) {
1059 case EFAULT:
1060 if (namelen != sizeof(struct sockaddr_in))
1061 error = EINVAL;
1062 break;
1063 case EINVAL:
1064 error1 = getsock(td, s, &cap_accept_rights, &fp1);
1065 if (error1 != 0) {
1066 error = error1;
1067 break;
1068 }
1069 so = fp1->f_data;
1070 if (so->so_type == SOCK_DGRAM)
1071 error = EOPNOTSUPP;
1072 fdrop(fp1, td);
1073 break;
1074 }
1075 return (error);
1076 }
1077
1078 if (PTRIN(addr) != NULL) {
1079 len = min(ss.ss_len, len);
1080 error = linux_copyout_sockaddr((struct sockaddr *)&ss,
1081 PTRIN(addr), len);
1082 if (error == 0) {
1083 len = ss.ss_len;
1084 error = copyout(&len, PTRIN(namelen), sizeof(len));
1085 }
1086 if (error != 0) {
1087 fdclose(td, fp, td->td_retval[0]);
1088 td->td_retval[0] = 0;
1089 }
1090 }
1091 if (fp != NULL)
1092 fdrop(fp, td);
1093 return (error);
1094 }
1095
1096 int
linux_accept(struct thread * td,struct linux_accept_args * args)1097 linux_accept(struct thread *td, struct linux_accept_args *args)
1098 {
1099
1100 return (linux_accept_common(td, args->s, args->addr,
1101 args->namelen, 0));
1102 }
1103
1104 int
linux_accept4(struct thread * td,struct linux_accept4_args * args)1105 linux_accept4(struct thread *td, struct linux_accept4_args *args)
1106 {
1107
1108 return (linux_accept_common(td, args->s, args->addr,
1109 args->namelen, args->flags));
1110 }
1111
1112 int
linux_getsockname(struct thread * td,struct linux_getsockname_args * args)1113 linux_getsockname(struct thread *td, struct linux_getsockname_args *args)
1114 {
1115 struct sockaddr_storage ss = { .ss_len = sizeof(ss) };
1116 socklen_t len;
1117 int error;
1118
1119 error = copyin(PTRIN(args->namelen), &len, sizeof(len));
1120 if (error != 0)
1121 return (error);
1122
1123 error = kern_getsockname(td, args->s, (struct sockaddr *)&ss);
1124 if (error != 0)
1125 return (error);
1126
1127 len = min(ss.ss_len, len);
1128 error = linux_copyout_sockaddr((struct sockaddr *)&ss,
1129 PTRIN(args->addr), len);
1130 if (error == 0) {
1131 len = ss.ss_len;
1132 error = copyout(&len, PTRIN(args->namelen), sizeof(len));
1133 }
1134 return (error);
1135 }
1136
1137 int
linux_getpeername(struct thread * td,struct linux_getpeername_args * args)1138 linux_getpeername(struct thread *td, struct linux_getpeername_args *args)
1139 {
1140 struct sockaddr_storage ss = { .ss_len = sizeof(ss) };
1141 socklen_t len;
1142 int error;
1143
1144 error = copyin(PTRIN(args->namelen), &len, sizeof(len));
1145 if (error != 0)
1146 return (error);
1147
1148 error = kern_getpeername(td, args->s, (struct sockaddr *)&ss);
1149 if (error != 0)
1150 return (error);
1151
1152 len = min(ss.ss_len, len);
1153 error = linux_copyout_sockaddr((struct sockaddr *)&ss,
1154 PTRIN(args->addr), len);
1155 if (error == 0) {
1156 len = ss.ss_len;
1157 error = copyout(&len, PTRIN(args->namelen), sizeof(len));
1158 }
1159 return (error);
1160 }
1161
1162 int
linux_socketpair(struct thread * td,struct linux_socketpair_args * args)1163 linux_socketpair(struct thread *td, struct linux_socketpair_args *args)
1164 {
1165 int domain, error, sv[2], type;
1166
1167 domain = linux_to_bsd_domain(args->domain);
1168 if (domain != PF_LOCAL)
1169 return (EAFNOSUPPORT);
1170 type = args->type & LINUX_SOCK_TYPE_MASK;
1171 if (type < 0 || type > LINUX_SOCK_MAX)
1172 return (EINVAL);
1173 error = linux_set_socket_flags(args->type & ~LINUX_SOCK_TYPE_MASK,
1174 &type);
1175 if (error != 0)
1176 return (error);
1177 if (args->protocol != 0 && args->protocol != PF_UNIX) {
1178 /*
1179 * Use of PF_UNIX as protocol argument is not right,
1180 * but Linux does it.
1181 * Do not map PF_UNIX as its Linux value is identical
1182 * to FreeBSD one.
1183 */
1184 return (EPROTONOSUPPORT);
1185 }
1186 error = kern_socketpair(td, domain, type, 0, sv);
1187 if (error != 0)
1188 return (error);
1189 error = copyout(sv, PTRIN(args->rsv), 2 * sizeof(int));
1190 if (error != 0) {
1191 (void)kern_close(td, sv[0]);
1192 (void)kern_close(td, sv[1]);
1193 }
1194 return (error);
1195 }
1196
1197 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1198 struct linux_send_args {
1199 register_t s;
1200 register_t msg;
1201 register_t len;
1202 register_t flags;
1203 };
1204
1205 static int
linux_send(struct thread * td,struct linux_send_args * args)1206 linux_send(struct thread *td, struct linux_send_args *args)
1207 {
1208 struct sendto_args /* {
1209 int s;
1210 caddr_t buf;
1211 int len;
1212 int flags;
1213 caddr_t to;
1214 int tolen;
1215 } */ bsd_args;
1216 struct file *fp;
1217 int error;
1218
1219 bsd_args.s = args->s;
1220 bsd_args.buf = (caddr_t)PTRIN(args->msg);
1221 bsd_args.len = args->len;
1222 bsd_args.flags = linux_to_bsd_msg_flags(args->flags);
1223 bsd_args.to = NULL;
1224 bsd_args.tolen = 0;
1225 error = sys_sendto(td, &bsd_args);
1226 if (error == ENOTCONN) {
1227 /*
1228 * Linux doesn't return ENOTCONN for non-blocking sockets.
1229 * Instead it returns the EAGAIN.
1230 */
1231 error = getsock(td, args->s, &cap_send_rights, &fp);
1232 if (error == 0) {
1233 if (atomic_load_int(&fp->f_flag) & FNONBLOCK)
1234 error = EAGAIN;
1235 fdrop(fp, td);
1236 }
1237 }
1238 return (error);
1239 }
1240
1241 struct linux_recv_args {
1242 register_t s;
1243 register_t msg;
1244 register_t len;
1245 register_t flags;
1246 };
1247
1248 static int
linux_recv(struct thread * td,struct linux_recv_args * args)1249 linux_recv(struct thread *td, struct linux_recv_args *args)
1250 {
1251 struct recvfrom_args /* {
1252 int s;
1253 caddr_t buf;
1254 int len;
1255 int flags;
1256 struct sockaddr *from;
1257 socklen_t fromlenaddr;
1258 } */ bsd_args;
1259
1260 bsd_args.s = args->s;
1261 bsd_args.buf = (caddr_t)PTRIN(args->msg);
1262 bsd_args.len = args->len;
1263 bsd_args.flags = linux_to_bsd_msg_flags(args->flags);
1264 bsd_args.from = NULL;
1265 bsd_args.fromlenaddr = 0;
1266 return (sys_recvfrom(td, &bsd_args));
1267 }
1268 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1269
1270 int
linux_sendto(struct thread * td,struct linux_sendto_args * args)1271 linux_sendto(struct thread *td, struct linux_sendto_args *args)
1272 {
1273 struct msghdr msg;
1274 struct iovec aiov;
1275 struct socket *so;
1276 struct file *fp;
1277 int error;
1278
1279 if (linux_check_hdrincl(td, args->s) == 0)
1280 /* IP_HDRINCL set, tweak the packet before sending */
1281 return (linux_sendto_hdrincl(td, args));
1282
1283 bzero(&msg, sizeof(msg));
1284 error = getsock(td, args->s, &cap_send_connect_rights, &fp);
1285 if (error != 0)
1286 return (error);
1287 so = fp->f_data;
1288 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0) {
1289 msg.msg_name = PTRIN(args->to);
1290 msg.msg_namelen = args->tolen;
1291 }
1292 msg.msg_iov = &aiov;
1293 msg.msg_iovlen = 1;
1294 aiov.iov_base = PTRIN(args->msg);
1295 aiov.iov_len = args->len;
1296 fdrop(fp, td);
1297 return (linux_sendit(td, args->s, &msg, args->flags, NULL,
1298 UIO_USERSPACE));
1299 }
1300
1301 int
linux_recvfrom(struct thread * td,struct linux_recvfrom_args * args)1302 linux_recvfrom(struct thread *td, struct linux_recvfrom_args *args)
1303 {
1304 struct sockaddr *sa;
1305 struct msghdr msg;
1306 struct iovec aiov;
1307 int error, fromlen;
1308
1309 if (PTRIN(args->fromlen) != NULL) {
1310 error = copyin(PTRIN(args->fromlen), &fromlen,
1311 sizeof(fromlen));
1312 if (error != 0)
1313 return (error);
1314 if (fromlen < 0)
1315 return (EINVAL);
1316 fromlen = min(fromlen, SOCK_MAXADDRLEN);
1317 sa = malloc(fromlen, M_SONAME, M_WAITOK);
1318 } else {
1319 fromlen = 0;
1320 sa = NULL;
1321 }
1322
1323 msg.msg_name = sa;
1324 msg.msg_namelen = fromlen;
1325 msg.msg_iov = &aiov;
1326 msg.msg_iovlen = 1;
1327 aiov.iov_base = PTRIN(args->buf);
1328 aiov.iov_len = args->len;
1329 msg.msg_control = 0;
1330 msg.msg_flags = linux_to_bsd_msg_flags(args->flags);
1331
1332 error = kern_recvit(td, args->s, &msg, UIO_SYSSPACE, NULL);
1333 if (error != 0)
1334 goto out;
1335
1336 /*
1337 * XXX. Seems that FreeBSD is different from Linux here. Linux
1338 * fill source address if underlying protocol provides it, while
1339 * FreeBSD fill it if underlying protocol is not connection-oriented.
1340 * So, kern_recvit() set msg.msg_namelen to 0 if protocol pr_flags
1341 * does not contains PR_ADDR flag.
1342 */
1343 if (PTRIN(args->from) != NULL && msg.msg_namelen != 0)
1344 error = linux_copyout_sockaddr(sa, PTRIN(args->from),
1345 msg.msg_namelen);
1346
1347 if (error == 0 && PTRIN(args->fromlen) != NULL)
1348 error = copyout(&msg.msg_namelen, PTRIN(args->fromlen),
1349 sizeof(msg.msg_namelen));
1350 out:
1351 free(sa, M_SONAME);
1352 return (error);
1353 }
1354
1355 static int
linux_sendmsg_common(struct thread * td,l_int s,struct l_msghdr * msghdr,l_uint flags)1356 linux_sendmsg_common(struct thread *td, l_int s, struct l_msghdr *msghdr,
1357 l_uint flags)
1358 {
1359 struct sockaddr_storage ss = { .ss_len = sizeof(ss) };
1360 struct cmsghdr *cmsg;
1361 struct mbuf *control;
1362 struct msghdr msg;
1363 struct l_cmsghdr linux_cmsg;
1364 struct l_cmsghdr *ptr_cmsg;
1365 struct l_msghdr linux_msghdr;
1366 struct iovec *iov;
1367 socklen_t datalen;
1368 struct socket *so;
1369 sa_family_t sa_family;
1370 struct file *fp;
1371 void *data;
1372 l_size_t len;
1373 l_size_t clen;
1374 int error;
1375
1376 error = copyin(msghdr, &linux_msghdr, sizeof(linux_msghdr));
1377 if (error != 0)
1378 return (error);
1379
1380 /*
1381 * Some Linux applications (ping) define a non-NULL control data
1382 * pointer, but a msg_controllen of 0, which is not allowed in the
1383 * FreeBSD system call interface. NULL the msg_control pointer in
1384 * order to handle this case. This should be checked, but allows the
1385 * Linux ping to work.
1386 */
1387 if (PTRIN(linux_msghdr.msg_control) != NULL &&
1388 linux_msghdr.msg_controllen == 0)
1389 linux_msghdr.msg_control = PTROUT(NULL);
1390
1391 error = linux_to_bsd_msghdr(&msg, &linux_msghdr);
1392 if (error != 0)
1393 return (error);
1394
1395 #ifdef COMPAT_LINUX32
1396 error = freebsd32_copyiniov(PTRIN(msg.msg_iov), msg.msg_iovlen,
1397 &iov, EMSGSIZE);
1398 #else
1399 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
1400 #endif
1401 if (error != 0)
1402 return (error);
1403
1404 control = NULL;
1405
1406 error = kern_getsockname(td, s, (struct sockaddr *)&ss);
1407 if (error != 0)
1408 goto bad;
1409 sa_family = ss.ss_family;
1410
1411 if (flags & LINUX_MSG_OOB) {
1412 error = EOPNOTSUPP;
1413 if (sa_family == AF_UNIX)
1414 goto bad;
1415
1416 error = getsock(td, s, &cap_send_rights, &fp);
1417 if (error != 0)
1418 goto bad;
1419 so = fp->f_data;
1420 if (so->so_type != SOCK_STREAM)
1421 error = EOPNOTSUPP;
1422 fdrop(fp, td);
1423 if (error != 0)
1424 goto bad;
1425 }
1426
1427 if (linux_msghdr.msg_controllen >= sizeof(struct l_cmsghdr)) {
1428 error = ENOBUFS;
1429 control = m_get(M_WAITOK, MT_CONTROL);
1430 MCLGET(control, M_WAITOK);
1431 data = mtod(control, void *);
1432 datalen = 0;
1433
1434 ptr_cmsg = PTRIN(linux_msghdr.msg_control);
1435 clen = linux_msghdr.msg_controllen;
1436 do {
1437 error = copyin(ptr_cmsg, &linux_cmsg,
1438 sizeof(struct l_cmsghdr));
1439 if (error != 0)
1440 goto bad;
1441
1442 error = EINVAL;
1443 if (linux_cmsg.cmsg_len < sizeof(struct l_cmsghdr) ||
1444 linux_cmsg.cmsg_len > clen)
1445 goto bad;
1446
1447 if (datalen + CMSG_HDRSZ > MCLBYTES)
1448 goto bad;
1449
1450 /*
1451 * Now we support only SCM_RIGHTS and SCM_CRED,
1452 * so return EINVAL in any other cmsg_type
1453 */
1454 cmsg = data;
1455 cmsg->cmsg_type =
1456 linux_to_bsd_cmsg_type(linux_cmsg.cmsg_type);
1457 cmsg->cmsg_level =
1458 linux_to_bsd_sockopt_level(linux_cmsg.cmsg_level);
1459 if (cmsg->cmsg_type == -1
1460 || cmsg->cmsg_level != SOL_SOCKET) {
1461 linux_msg(curthread,
1462 "unsupported sendmsg cmsg level %d type %d",
1463 linux_cmsg.cmsg_level, linux_cmsg.cmsg_type);
1464 goto bad;
1465 }
1466
1467 /*
1468 * Some applications (e.g. pulseaudio) attempt to
1469 * send ancillary data even if the underlying protocol
1470 * doesn't support it which is not allowed in the
1471 * FreeBSD system call interface.
1472 */
1473 if (sa_family != AF_UNIX)
1474 goto next;
1475
1476 if (cmsg->cmsg_type == SCM_CREDS) {
1477 len = sizeof(struct cmsgcred);
1478 if (datalen + CMSG_SPACE(len) > MCLBYTES)
1479 goto bad;
1480
1481 /*
1482 * The lower levels will fill in the structure
1483 */
1484 memset(CMSG_DATA(data), 0, len);
1485 } else {
1486 len = linux_cmsg.cmsg_len - L_CMSG_HDRSZ;
1487 if (datalen + CMSG_SPACE(len) < datalen ||
1488 datalen + CMSG_SPACE(len) > MCLBYTES)
1489 goto bad;
1490
1491 error = copyin(LINUX_CMSG_DATA(ptr_cmsg),
1492 CMSG_DATA(data), len);
1493 if (error != 0)
1494 goto bad;
1495 }
1496
1497 cmsg->cmsg_len = CMSG_LEN(len);
1498 data = (char *)data + CMSG_SPACE(len);
1499 datalen += CMSG_SPACE(len);
1500
1501 next:
1502 if (clen <= LINUX_CMSG_ALIGN(linux_cmsg.cmsg_len))
1503 break;
1504
1505 clen -= LINUX_CMSG_ALIGN(linux_cmsg.cmsg_len);
1506 ptr_cmsg = (struct l_cmsghdr *)((char *)ptr_cmsg +
1507 LINUX_CMSG_ALIGN(linux_cmsg.cmsg_len));
1508 } while(clen >= sizeof(struct l_cmsghdr));
1509
1510 control->m_len = datalen;
1511 if (datalen == 0) {
1512 m_freem(control);
1513 control = NULL;
1514 }
1515 }
1516
1517 msg.msg_iov = iov;
1518 msg.msg_flags = 0;
1519 error = linux_sendit(td, s, &msg, flags, control, UIO_USERSPACE);
1520 control = NULL;
1521
1522 bad:
1523 m_freem(control);
1524 free(iov, M_IOV);
1525 return (error);
1526 }
1527
1528 int
linux_sendmsg(struct thread * td,struct linux_sendmsg_args * args)1529 linux_sendmsg(struct thread *td, struct linux_sendmsg_args *args)
1530 {
1531
1532 return (linux_sendmsg_common(td, args->s, PTRIN(args->msg),
1533 args->flags));
1534 }
1535
1536 int
linux_sendmmsg(struct thread * td,struct linux_sendmmsg_args * args)1537 linux_sendmmsg(struct thread *td, struct linux_sendmmsg_args *args)
1538 {
1539 struct l_mmsghdr *msg;
1540 l_uint retval;
1541 int error, datagrams;
1542
1543 if (args->vlen > UIO_MAXIOV)
1544 args->vlen = UIO_MAXIOV;
1545
1546 msg = PTRIN(args->msg);
1547 datagrams = 0;
1548 while (datagrams < args->vlen) {
1549 error = linux_sendmsg_common(td, args->s, &msg->msg_hdr,
1550 args->flags);
1551 if (error != 0)
1552 break;
1553
1554 retval = td->td_retval[0];
1555 error = copyout(&retval, &msg->msg_len, sizeof(msg->msg_len));
1556 if (error != 0)
1557 break;
1558 ++msg;
1559 ++datagrams;
1560 }
1561 if (error == 0)
1562 td->td_retval[0] = datagrams;
1563 return (error);
1564 }
1565
1566 static int
recvmsg_scm_rights(struct thread * td,l_uint flags,socklen_t * datalen,void ** data,void ** udata)1567 recvmsg_scm_rights(struct thread *td, l_uint flags, socklen_t *datalen,
1568 void **data, void **udata)
1569 {
1570 int i, fd, fds, *fdp;
1571
1572 if (flags & LINUX_MSG_CMSG_CLOEXEC) {
1573 fds = *datalen / sizeof(int);
1574 fdp = *data;
1575 for (i = 0; i < fds; i++) {
1576 fd = *fdp++;
1577 (void)kern_fcntl(td, fd, F_SETFD, FD_CLOEXEC);
1578 }
1579 }
1580 return (0);
1581 }
1582
1583
1584 static int
recvmsg_scm_creds(socklen_t * datalen,void ** data,void ** udata)1585 recvmsg_scm_creds(socklen_t *datalen, void **data, void **udata)
1586 {
1587 struct cmsgcred *cmcred;
1588 struct l_ucred lu;
1589
1590 cmcred = *data;
1591 lu.pid = cmcred->cmcred_pid;
1592 lu.uid = cmcred->cmcred_uid;
1593 lu.gid = cmcred->cmcred_gid;
1594 memmove(*data, &lu, sizeof(lu));
1595 *datalen = sizeof(lu);
1596 return (0);
1597 }
1598 _Static_assert(sizeof(struct cmsgcred) >= sizeof(struct l_ucred),
1599 "scm_creds sizeof l_ucred");
1600
1601 static int
recvmsg_scm_creds2(socklen_t * datalen,void ** data,void ** udata)1602 recvmsg_scm_creds2(socklen_t *datalen, void **data, void **udata)
1603 {
1604 struct sockcred2 *scred;
1605 struct l_ucred lu;
1606
1607 scred = *data;
1608 lu.pid = scred->sc_pid;
1609 lu.uid = scred->sc_uid;
1610 lu.gid = scred->sc_gid;
1611 memmove(*data, &lu, sizeof(lu));
1612 *datalen = sizeof(lu);
1613 return (0);
1614 }
1615 _Static_assert(sizeof(struct sockcred2) >= sizeof(struct l_ucred),
1616 "scm_creds2 sizeof l_ucred");
1617
1618 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1619 static int
recvmsg_scm_timestamp(l_int msg_type,socklen_t * datalen,void ** data,void ** udata)1620 recvmsg_scm_timestamp(l_int msg_type, socklen_t *datalen, void **data,
1621 void **udata)
1622 {
1623 l_sock_timeval ltv64;
1624 l_timeval ltv;
1625 struct timeval *tv;
1626 socklen_t len;
1627 void *buf;
1628
1629 if (*datalen != sizeof(struct timeval))
1630 return (EMSGSIZE);
1631
1632 tv = *data;
1633 #if defined(COMPAT_LINUX32)
1634 if (msg_type == LINUX_SCM_TIMESTAMPO &&
1635 (tv->tv_sec > INT_MAX || tv->tv_sec < INT_MIN))
1636 return (EOVERFLOW);
1637 #endif
1638 if (msg_type == LINUX_SCM_TIMESTAMPN)
1639 len = sizeof(ltv64);
1640 else
1641 len = sizeof(ltv);
1642
1643 buf = malloc(len, M_LINUX, M_WAITOK);
1644 if (msg_type == LINUX_SCM_TIMESTAMPN) {
1645 ltv64.tv_sec = tv->tv_sec;
1646 ltv64.tv_usec = tv->tv_usec;
1647 memmove(buf, <v64, len);
1648 } else {
1649 ltv.tv_sec = tv->tv_sec;
1650 ltv.tv_usec = tv->tv_usec;
1651 memmove(buf, <v, len);
1652 }
1653 *data = *udata = buf;
1654 *datalen = len;
1655 return (0);
1656 }
1657 #else
1658 _Static_assert(sizeof(struct timeval) == sizeof(l_timeval),
1659 "scm_timestamp sizeof l_timeval");
1660 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1661
1662 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1663 static int
recvmsg_scm_timestampns(l_int msg_type,socklen_t * datalen,void ** data,void ** udata)1664 recvmsg_scm_timestampns(l_int msg_type, socklen_t *datalen, void **data,
1665 void **udata)
1666 {
1667 struct l_timespec64 ts64;
1668 struct l_timespec ts32;
1669 struct timespec ts;
1670 socklen_t len;
1671 void *buf;
1672
1673 if (msg_type == LINUX_SCM_TIMESTAMPNSO)
1674 len = sizeof(ts32);
1675 else
1676 len = sizeof(ts64);
1677
1678 buf = malloc(len, M_LINUX, M_WAITOK);
1679 bintime2timespec(*data, &ts);
1680 if (msg_type == LINUX_SCM_TIMESTAMPNSO) {
1681 ts32.tv_sec = ts.tv_sec;
1682 ts32.tv_nsec = ts.tv_nsec;
1683 memmove(buf, &ts32, len);
1684 } else {
1685 ts64.tv_sec = ts.tv_sec;
1686 ts64.tv_nsec = ts.tv_nsec;
1687 memmove(buf, &ts64, len);
1688 }
1689 *data = *udata = buf;
1690 *datalen = len;
1691 return (0);
1692 }
1693 #else
1694 static int
recvmsg_scm_timestampns(l_int msg_type,socklen_t * datalen,void ** data,void ** udata)1695 recvmsg_scm_timestampns(l_int msg_type, socklen_t *datalen, void **data,
1696 void **udata)
1697 {
1698 struct timespec ts;
1699
1700 bintime2timespec(*data, &ts);
1701 memmove(*data, &ts, sizeof(struct timespec));
1702 *datalen = sizeof(struct timespec);
1703 return (0);
1704 }
1705 _Static_assert(sizeof(struct bintime) >= sizeof(struct timespec),
1706 "scm_timestampns sizeof timespec");
1707 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1708
1709 static int
recvmsg_scm_sol_socket(struct thread * td,l_int msg_type,l_int lmsg_type,l_uint flags,socklen_t * datalen,void ** data,void ** udata)1710 recvmsg_scm_sol_socket(struct thread *td, l_int msg_type, l_int lmsg_type,
1711 l_uint flags, socklen_t *datalen, void **data, void **udata)
1712 {
1713 int error;
1714
1715 error = 0;
1716 switch (msg_type) {
1717 case SCM_RIGHTS:
1718 error = recvmsg_scm_rights(td, flags, datalen,
1719 data, udata);
1720 break;
1721 case SCM_CREDS:
1722 error = recvmsg_scm_creds(datalen, data, udata);
1723 break;
1724 case SCM_CREDS2:
1725 error = recvmsg_scm_creds2(datalen, data, udata);
1726 break;
1727 case SCM_TIMESTAMP:
1728 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1729 error = recvmsg_scm_timestamp(lmsg_type, datalen,
1730 data, udata);
1731 #endif
1732 break;
1733 case SCM_BINTIME:
1734 error = recvmsg_scm_timestampns(lmsg_type, datalen,
1735 data, udata);
1736 break;
1737 }
1738
1739 return (error);
1740 }
1741
1742 static int
recvmsg_scm_ip_origdstaddr(socklen_t * datalen,void ** data,void ** udata)1743 recvmsg_scm_ip_origdstaddr(socklen_t *datalen, void **data, void **udata)
1744 {
1745 struct l_sockaddr *lsa;
1746 int error;
1747
1748 error = bsd_to_linux_sockaddr(*data, &lsa, *datalen);
1749 if (error == 0) {
1750 *data = *udata = lsa;
1751 *datalen = sizeof(*lsa);
1752 }
1753 return (error);
1754 }
1755
1756 static int
recvmsg_scm_ipproto_ip(l_int msg_type,l_int lmsg_type,socklen_t * datalen,void ** data,void ** udata)1757 recvmsg_scm_ipproto_ip(l_int msg_type, l_int lmsg_type, socklen_t *datalen,
1758 void **data, void **udata)
1759 {
1760 int error;
1761
1762 error = 0;
1763 switch (msg_type) {
1764 case IP_ORIGDSTADDR:
1765 error = recvmsg_scm_ip_origdstaddr(datalen, data,
1766 udata);
1767 break;
1768 }
1769
1770 return (error);
1771 }
1772
1773 static int
linux_recvmsg_common(struct thread * td,l_int s,struct l_msghdr * msghdr,l_uint flags,struct msghdr * msg)1774 linux_recvmsg_common(struct thread *td, l_int s, struct l_msghdr *msghdr,
1775 l_uint flags, struct msghdr *msg)
1776 {
1777 struct proc *p = td->td_proc;
1778 struct cmsghdr *cm;
1779 struct l_cmsghdr *lcm = NULL;
1780 socklen_t datalen, maxlen, outlen;
1781 struct l_msghdr l_msghdr;
1782 struct iovec *iov, *uiov;
1783 struct mbuf *m, *control = NULL;
1784 struct mbuf **controlp;
1785 struct sockaddr *sa;
1786 caddr_t outbuf;
1787 void *data, *udata;
1788 int error, skiped;
1789
1790 error = copyin(msghdr, &l_msghdr, sizeof(l_msghdr));
1791 if (error != 0)
1792 return (error);
1793
1794 /*
1795 * Pass user-supplied recvmsg() flags in msg_flags field,
1796 * following sys_recvmsg() convention.
1797 */
1798 l_msghdr.msg_flags = flags;
1799
1800 error = linux_to_bsd_msghdr(msg, &l_msghdr);
1801 if (error != 0)
1802 return (error);
1803
1804 #ifdef COMPAT_LINUX32
1805 error = freebsd32_copyiniov(PTRIN(msg->msg_iov), msg->msg_iovlen,
1806 &iov, EMSGSIZE);
1807 #else
1808 error = copyiniov(msg->msg_iov, msg->msg_iovlen, &iov, EMSGSIZE);
1809 #endif
1810 if (error != 0)
1811 return (error);
1812
1813 if (msg->msg_name != NULL && msg->msg_namelen > 0) {
1814 msg->msg_namelen = min(msg->msg_namelen, SOCK_MAXADDRLEN);
1815 sa = malloc(msg->msg_namelen, M_SONAME, M_WAITOK);
1816 msg->msg_name = sa;
1817 } else {
1818 sa = NULL;
1819 msg->msg_name = NULL;
1820 }
1821
1822 uiov = msg->msg_iov;
1823 msg->msg_iov = iov;
1824 controlp = (msg->msg_control != NULL) ? &control : NULL;
1825 error = kern_recvit(td, s, msg, UIO_SYSSPACE, controlp);
1826 msg->msg_iov = uiov;
1827 if (error != 0)
1828 goto bad;
1829
1830 /*
1831 * Note that kern_recvit() updates msg->msg_namelen.
1832 */
1833 if (msg->msg_name != NULL && msg->msg_namelen > 0) {
1834 msg->msg_name = PTRIN(l_msghdr.msg_name);
1835 error = linux_copyout_sockaddr(sa, msg->msg_name,
1836 msg->msg_namelen);
1837 if (error != 0)
1838 goto bad;
1839 }
1840
1841 error = bsd_to_linux_msghdr(msg, &l_msghdr);
1842 if (error != 0)
1843 goto bad;
1844
1845 skiped = outlen = 0;
1846 maxlen = l_msghdr.msg_controllen;
1847 if (control == NULL)
1848 goto out;
1849
1850 lcm = malloc(L_CMSG_HDRSZ, M_LINUX, M_WAITOK | M_ZERO);
1851 msg->msg_control = mtod(control, struct cmsghdr *);
1852 msg->msg_controllen = control->m_len;
1853 outbuf = PTRIN(l_msghdr.msg_control);
1854 for (m = control; m != NULL; m = m->m_next) {
1855 cm = mtod(m, struct cmsghdr *);
1856 lcm->cmsg_type = bsd_to_linux_cmsg_type(p, cm->cmsg_type,
1857 cm->cmsg_level);
1858 lcm->cmsg_level = bsd_to_linux_sockopt_level(cm->cmsg_level);
1859
1860 if (lcm->cmsg_type == -1 ||
1861 cm->cmsg_level == -1) {
1862 LINUX_RATELIMIT_MSG_OPT2(
1863 "unsupported recvmsg cmsg level %d type %d",
1864 cm->cmsg_level, cm->cmsg_type);
1865 /* Skip unsupported messages */
1866 skiped++;
1867 continue;
1868 }
1869 data = CMSG_DATA(cm);
1870 datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
1871 udata = NULL;
1872 error = 0;
1873
1874 switch (cm->cmsg_level) {
1875 case IPPROTO_IP:
1876 error = recvmsg_scm_ipproto_ip(cm->cmsg_type,
1877 lcm->cmsg_type, &datalen, &data, &udata);
1878 break;
1879 case SOL_SOCKET:
1880 error = recvmsg_scm_sol_socket(td, cm->cmsg_type,
1881 lcm->cmsg_type, flags, &datalen, &data, &udata);
1882 break;
1883 }
1884
1885 /* The recvmsg_scm_ is responsible to free udata on error. */
1886 if (error != 0)
1887 goto bad;
1888
1889 if (outlen + LINUX_CMSG_LEN(datalen) > maxlen) {
1890 if (outlen == 0) {
1891 error = EMSGSIZE;
1892 goto err;
1893 } else {
1894 l_msghdr.msg_flags |= LINUX_MSG_CTRUNC;
1895 m_dispose_extcontrolm(control);
1896 free(udata, M_LINUX);
1897 goto out;
1898 }
1899 }
1900
1901 lcm->cmsg_len = LINUX_CMSG_LEN(datalen);
1902 error = copyout(lcm, outbuf, L_CMSG_HDRSZ);
1903 if (error == 0) {
1904 error = copyout(data, LINUX_CMSG_DATA(outbuf), datalen);
1905 if (error == 0) {
1906 outbuf += LINUX_CMSG_SPACE(datalen);
1907 outlen += LINUX_CMSG_SPACE(datalen);
1908 }
1909 }
1910 err:
1911 free(udata, M_LINUX);
1912 if (error != 0)
1913 goto bad;
1914 }
1915 if (outlen == 0 && skiped > 0) {
1916 error = EINVAL;
1917 goto bad;
1918 }
1919
1920 out:
1921 l_msghdr.msg_controllen = outlen;
1922 error = copyout(&l_msghdr, msghdr, sizeof(l_msghdr));
1923
1924 bad:
1925 if (control != NULL) {
1926 if (error != 0)
1927 m_dispose_extcontrolm(control);
1928 m_freem(control);
1929 }
1930 free(iov, M_IOV);
1931 free(lcm, M_LINUX);
1932 free(sa, M_SONAME);
1933
1934 return (error);
1935 }
1936
1937 int
linux_recvmsg(struct thread * td,struct linux_recvmsg_args * args)1938 linux_recvmsg(struct thread *td, struct linux_recvmsg_args *args)
1939 {
1940 struct msghdr bsd_msg;
1941 struct file *fp;
1942 int error;
1943
1944 error = getsock(td, args->s, &cap_recv_rights, &fp);
1945 if (error != 0)
1946 return (error);
1947 fdrop(fp, td);
1948 return (linux_recvmsg_common(td, args->s, PTRIN(args->msg),
1949 args->flags, &bsd_msg));
1950 }
1951
1952 static int
linux_recvmmsg_common(struct thread * td,l_int s,struct l_mmsghdr * msg,l_uint vlen,l_uint flags,struct timespec * tts)1953 linux_recvmmsg_common(struct thread *td, l_int s, struct l_mmsghdr *msg,
1954 l_uint vlen, l_uint flags, struct timespec *tts)
1955 {
1956 struct msghdr bsd_msg;
1957 struct timespec ts;
1958 struct file *fp;
1959 l_uint retval;
1960 int error, datagrams;
1961
1962 error = getsock(td, s, &cap_recv_rights, &fp);
1963 if (error != 0)
1964 return (error);
1965 datagrams = 0;
1966 while (datagrams < vlen) {
1967 error = linux_recvmsg_common(td, s, &msg->msg_hdr,
1968 flags & ~LINUX_MSG_WAITFORONE, &bsd_msg);
1969 if (error != 0)
1970 break;
1971
1972 retval = td->td_retval[0];
1973 error = copyout(&retval, &msg->msg_len, sizeof(msg->msg_len));
1974 if (error != 0)
1975 break;
1976 ++msg;
1977 ++datagrams;
1978
1979 /*
1980 * MSG_WAITFORONE turns on MSG_DONTWAIT after one packet.
1981 */
1982 if (flags & LINUX_MSG_WAITFORONE)
1983 flags |= LINUX_MSG_DONTWAIT;
1984
1985 /*
1986 * See BUGS section of recvmmsg(2).
1987 */
1988 if (tts) {
1989 getnanotime(&ts);
1990 timespecsub(&ts, tts, &ts);
1991 if (!timespecisset(&ts) || ts.tv_sec > 0)
1992 break;
1993 }
1994 /* Out of band data, return right away. */
1995 if (bsd_msg.msg_flags & MSG_OOB)
1996 break;
1997 }
1998 if (error == 0)
1999 td->td_retval[0] = datagrams;
2000 fdrop(fp, td);
2001 return (error);
2002 }
2003
2004 int
linux_recvmmsg(struct thread * td,struct linux_recvmmsg_args * args)2005 linux_recvmmsg(struct thread *td, struct linux_recvmmsg_args *args)
2006 {
2007 struct timespec ts, tts, *ptts;
2008 int error;
2009
2010 if (args->timeout) {
2011 error = linux_get_timespec(&ts, args->timeout);
2012 if (error != 0)
2013 return (error);
2014 getnanotime(&tts);
2015 timespecadd(&tts, &ts, &tts);
2016 ptts = &tts;
2017 }
2018 else ptts = NULL;
2019
2020 return (linux_recvmmsg_common(td, args->s, PTRIN(args->msg),
2021 args->vlen, args->flags, ptts));
2022 }
2023
2024 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
2025 int
linux_recvmmsg_time64(struct thread * td,struct linux_recvmmsg_time64_args * args)2026 linux_recvmmsg_time64(struct thread *td, struct linux_recvmmsg_time64_args *args)
2027 {
2028 struct timespec ts, tts, *ptts;
2029 int error;
2030
2031 if (args->timeout) {
2032 error = linux_get_timespec64(&ts, args->timeout);
2033 if (error != 0)
2034 return (error);
2035 getnanotime(&tts);
2036 timespecadd(&tts, &ts, &tts);
2037 ptts = &tts;
2038 }
2039 else ptts = NULL;
2040
2041 return (linux_recvmmsg_common(td, args->s, PTRIN(args->msg),
2042 args->vlen, args->flags, ptts));
2043 }
2044 #endif
2045
2046 int
linux_shutdown(struct thread * td,struct linux_shutdown_args * args)2047 linux_shutdown(struct thread *td, struct linux_shutdown_args *args)
2048 {
2049
2050 return (kern_shutdown(td, args->s, args->how));
2051 }
2052
2053 int
linux_setsockopt(struct thread * td,struct linux_setsockopt_args * args)2054 linux_setsockopt(struct thread *td, struct linux_setsockopt_args *args)
2055 {
2056 struct proc *p = td->td_proc;
2057 struct linux_pemuldata *pem;
2058 l_timeval linux_tv;
2059 struct sockaddr *sa;
2060 struct timeval tv;
2061 socklen_t len;
2062 int error, level, name, val;
2063
2064 level = linux_to_bsd_sockopt_level(args->level);
2065 switch (level) {
2066 case SOL_SOCKET:
2067 name = linux_to_bsd_so_sockopt(args->optname);
2068 switch (name) {
2069 case LOCAL_CREDS_PERSISTENT:
2070 level = SOL_LOCAL;
2071 break;
2072 case SO_RCVTIMEO:
2073 /* FALLTHROUGH */
2074 case SO_SNDTIMEO:
2075 error = copyin(PTRIN(args->optval), &linux_tv,
2076 sizeof(linux_tv));
2077 if (error != 0)
2078 return (error);
2079 tv.tv_sec = linux_tv.tv_sec;
2080 tv.tv_usec = linux_tv.tv_usec;
2081 return (kern_setsockopt(td, args->s, level,
2082 name, &tv, UIO_SYSSPACE, sizeof(tv)));
2083 /* NOTREACHED */
2084 case SO_TIMESTAMP:
2085 /* overwrite SO_BINTIME */
2086 val = 0;
2087 error = kern_setsockopt(td, args->s, level,
2088 SO_BINTIME, &val, UIO_SYSSPACE, sizeof(val));
2089 if (error != 0)
2090 return (error);
2091 pem = pem_find(p);
2092 pem->so_timestamp = args->optname;
2093 break;
2094 case SO_BINTIME:
2095 /* overwrite SO_TIMESTAMP */
2096 val = 0;
2097 error = kern_setsockopt(td, args->s, level,
2098 SO_TIMESTAMP, &val, UIO_SYSSPACE, sizeof(val));
2099 if (error != 0)
2100 return (error);
2101 pem = pem_find(p);
2102 pem->so_timestampns = args->optname;
2103 break;
2104 default:
2105 break;
2106 }
2107 break;
2108 case IPPROTO_IP:
2109 if (args->optname == LINUX_IP_RECVERR &&
2110 linux_ignore_ip_recverr) {
2111 /*
2112 * XXX: This is a hack to unbreak DNS resolution
2113 * with glibc 2.30 and above.
2114 */
2115 return (0);
2116 }
2117 name = linux_to_bsd_ip_sockopt(args->optname);
2118 break;
2119 case IPPROTO_IPV6:
2120 if (args->optname == LINUX_IPV6_RECVERR &&
2121 linux_ignore_ip_recverr) {
2122 /*
2123 * XXX: This is a hack to unbreak DNS resolution
2124 * with glibc 2.30 and above.
2125 */
2126 return (0);
2127 }
2128 name = linux_to_bsd_ip6_sockopt(args->optname);
2129 break;
2130 case IPPROTO_TCP:
2131 name = linux_to_bsd_tcp_sockopt(args->optname);
2132 break;
2133 case SOL_NETLINK:
2134 name = args->optname;
2135 break;
2136 default:
2137 name = -1;
2138 break;
2139 }
2140 if (name < 0) {
2141 if (name == -1)
2142 linux_msg(curthread,
2143 "unsupported setsockopt level %d optname %d",
2144 args->level, args->optname);
2145 return (ENOPROTOOPT);
2146 }
2147
2148 if (name == IPV6_NEXTHOP) {
2149 len = args->optlen;
2150 error = linux_to_bsd_sockaddr(PTRIN(args->optval), &sa, &len);
2151 if (error != 0)
2152 return (error);
2153
2154 error = kern_setsockopt(td, args->s, level,
2155 name, sa, UIO_SYSSPACE, len);
2156 free(sa, M_SONAME);
2157 } else {
2158 error = kern_setsockopt(td, args->s, level,
2159 name, PTRIN(args->optval), UIO_USERSPACE, args->optlen);
2160 }
2161
2162 return (error);
2163 }
2164
2165 static int
linux_sockopt_copyout(struct thread * td,void * val,socklen_t len,struct linux_getsockopt_args * args)2166 linux_sockopt_copyout(struct thread *td, void *val, socklen_t len,
2167 struct linux_getsockopt_args *args)
2168 {
2169 int error;
2170
2171 error = copyout(val, PTRIN(args->optval), len);
2172 if (error == 0)
2173 error = copyout(&len, PTRIN(args->optlen), sizeof(len));
2174 return (error);
2175 }
2176
2177 static int
linux_getsockopt_so_peergroups(struct thread * td,struct linux_getsockopt_args * args)2178 linux_getsockopt_so_peergroups(struct thread *td,
2179 struct linux_getsockopt_args *args)
2180 {
2181 struct xucred xu;
2182 socklen_t xulen, len;
2183 int error, i;
2184
2185 xulen = sizeof(xu);
2186 error = kern_getsockopt(td, args->s, 0,
2187 LOCAL_PEERCRED, &xu, UIO_SYSSPACE, &xulen);
2188 if (error != 0)
2189 return (error);
2190
2191 len = xu.cr_ngroups * sizeof(l_gid_t);
2192 if (args->optlen < len) {
2193 error = copyout(&len, PTRIN(args->optlen), sizeof(len));
2194 if (error == 0)
2195 error = ERANGE;
2196 return (error);
2197 }
2198
2199 /*
2200 * "- 1" to skip the primary group.
2201 */
2202 for (i = 0; i < xu.cr_ngroups - 1; i++) {
2203 error = copyout(xu.cr_groups + i + 1,
2204 (void *)(args->optval + i * sizeof(l_gid_t)),
2205 sizeof(l_gid_t));
2206 if (error != 0)
2207 return (error);
2208 }
2209
2210 error = copyout(&len, PTRIN(args->optlen), sizeof(len));
2211 return (error);
2212 }
2213
2214 static int
linux_getsockopt_so_peersec(struct thread * td,struct linux_getsockopt_args * args)2215 linux_getsockopt_so_peersec(struct thread *td,
2216 struct linux_getsockopt_args *args)
2217 {
2218 socklen_t len;
2219 int error;
2220
2221 len = sizeof(SECURITY_CONTEXT_STRING);
2222 if (args->optlen < len) {
2223 error = copyout(&len, PTRIN(args->optlen), sizeof(len));
2224 if (error == 0)
2225 error = ERANGE;
2226 return (error);
2227 }
2228
2229 return (linux_sockopt_copyout(td, SECURITY_CONTEXT_STRING,
2230 len, args));
2231 }
2232
2233 static int
linux_getsockopt_so_linger(struct thread * td,struct linux_getsockopt_args * args)2234 linux_getsockopt_so_linger(struct thread *td,
2235 struct linux_getsockopt_args *args)
2236 {
2237 struct linger ling;
2238 socklen_t len;
2239 int error;
2240
2241 len = sizeof(ling);
2242 error = kern_getsockopt(td, args->s, SOL_SOCKET,
2243 SO_LINGER, &ling, UIO_SYSSPACE, &len);
2244 if (error != 0)
2245 return (error);
2246 ling.l_onoff = ((ling.l_onoff & SO_LINGER) != 0);
2247 return (linux_sockopt_copyout(td, &ling, len, args));
2248 }
2249
2250 int
linux_getsockopt(struct thread * td,struct linux_getsockopt_args * args)2251 linux_getsockopt(struct thread *td, struct linux_getsockopt_args *args)
2252 {
2253 l_timeval linux_tv;
2254 struct timeval tv;
2255 socklen_t tv_len, xulen, len;
2256 struct sockaddr *sa;
2257 struct xucred xu;
2258 struct l_ucred lxu;
2259 int error, level, name, newval;
2260
2261 level = linux_to_bsd_sockopt_level(args->level);
2262 switch (level) {
2263 case SOL_SOCKET:
2264 switch (args->optname) {
2265 case LINUX_SO_PEERGROUPS:
2266 return (linux_getsockopt_so_peergroups(td, args));
2267 case LINUX_SO_PEERSEC:
2268 return (linux_getsockopt_so_peersec(td, args));
2269 default:
2270 break;
2271 }
2272
2273 name = linux_to_bsd_so_sockopt(args->optname);
2274 switch (name) {
2275 case LOCAL_CREDS_PERSISTENT:
2276 level = SOL_LOCAL;
2277 break;
2278 case SO_RCVTIMEO:
2279 /* FALLTHROUGH */
2280 case SO_SNDTIMEO:
2281 tv_len = sizeof(tv);
2282 error = kern_getsockopt(td, args->s, level,
2283 name, &tv, UIO_SYSSPACE, &tv_len);
2284 if (error != 0)
2285 return (error);
2286 linux_tv.tv_sec = tv.tv_sec;
2287 linux_tv.tv_usec = tv.tv_usec;
2288 return (linux_sockopt_copyout(td, &linux_tv,
2289 sizeof(linux_tv), args));
2290 /* NOTREACHED */
2291 case LOCAL_PEERCRED:
2292 if (args->optlen < sizeof(lxu))
2293 return (EINVAL);
2294 /*
2295 * LOCAL_PEERCRED is not served at the SOL_SOCKET level,
2296 * but by the Unix socket's level 0.
2297 */
2298 level = 0;
2299 xulen = sizeof(xu);
2300 error = kern_getsockopt(td, args->s, level,
2301 name, &xu, UIO_SYSSPACE, &xulen);
2302 if (error != 0)
2303 return (error);
2304 lxu.pid = xu.cr_pid;
2305 lxu.uid = xu.cr_uid;
2306 lxu.gid = xu.cr_gid;
2307 return (linux_sockopt_copyout(td, &lxu,
2308 sizeof(lxu), args));
2309 /* NOTREACHED */
2310 case SO_ERROR:
2311 len = sizeof(newval);
2312 error = kern_getsockopt(td, args->s, level,
2313 name, &newval, UIO_SYSSPACE, &len);
2314 if (error != 0)
2315 return (error);
2316 newval = -bsd_to_linux_errno(newval);
2317 return (linux_sockopt_copyout(td, &newval,
2318 len, args));
2319 /* NOTREACHED */
2320 case SO_DOMAIN:
2321 len = sizeof(newval);
2322 error = kern_getsockopt(td, args->s, level,
2323 name, &newval, UIO_SYSSPACE, &len);
2324 if (error != 0)
2325 return (error);
2326 newval = bsd_to_linux_domain((sa_family_t)newval);
2327 if (newval == AF_UNKNOWN)
2328 return (ENOPROTOOPT);
2329 return (linux_sockopt_copyout(td, &newval,
2330 len, args));
2331 /* NOTREACHED */
2332 case SO_LINGER:
2333 return (linux_getsockopt_so_linger(td, args));
2334 /* NOTREACHED */
2335 default:
2336 break;
2337 }
2338 break;
2339 case IPPROTO_IP:
2340 name = linux_to_bsd_ip_sockopt(args->optname);
2341 break;
2342 case IPPROTO_IPV6:
2343 name = linux_to_bsd_ip6_sockopt(args->optname);
2344 break;
2345 case IPPROTO_TCP:
2346 name = linux_to_bsd_tcp_sockopt(args->optname);
2347 break;
2348 default:
2349 name = -1;
2350 break;
2351 }
2352 if (name < 0) {
2353 if (name == -1)
2354 linux_msg(curthread,
2355 "unsupported getsockopt level %d optname %d",
2356 args->level, args->optname);
2357 return (EINVAL);
2358 }
2359
2360 if (name == IPV6_NEXTHOP) {
2361 error = copyin(PTRIN(args->optlen), &len, sizeof(len));
2362 if (error != 0)
2363 return (error);
2364 sa = malloc(len, M_SONAME, M_WAITOK);
2365
2366 error = kern_getsockopt(td, args->s, level,
2367 name, sa, UIO_SYSSPACE, &len);
2368 if (error != 0)
2369 goto out;
2370
2371 error = linux_copyout_sockaddr(sa, PTRIN(args->optval), len);
2372 if (error == 0)
2373 error = copyout(&len, PTRIN(args->optlen),
2374 sizeof(len));
2375 out:
2376 free(sa, M_SONAME);
2377 } else {
2378 if (args->optval) {
2379 error = copyin(PTRIN(args->optlen), &len, sizeof(len));
2380 if (error != 0)
2381 return (error);
2382 }
2383 error = kern_getsockopt(td, args->s, level,
2384 name, PTRIN(args->optval), UIO_USERSPACE, &len);
2385 if (error == 0)
2386 error = copyout(&len, PTRIN(args->optlen),
2387 sizeof(len));
2388 }
2389
2390 return (error);
2391 }
2392
2393 /*
2394 * Based on sendfile_getsock from kern_sendfile.c
2395 * Determines whether an fd is a stream socket that can be used
2396 * with FreeBSD sendfile.
2397 */
2398 static bool
is_sendfile(struct file * fp,struct file * ofp)2399 is_sendfile(struct file *fp, struct file *ofp)
2400 {
2401 struct socket *so;
2402
2403 /*
2404 * FreeBSD sendfile() system call sends a regular file or
2405 * shared memory object out a stream socket.
2406 */
2407 if ((fp->f_type != DTYPE_SHM && fp->f_type != DTYPE_VNODE) ||
2408 (fp->f_type == DTYPE_VNODE &&
2409 (fp->f_vnode == NULL || fp->f_vnode->v_type != VREG)))
2410 return (false);
2411 /*
2412 * The socket must be a stream socket and connected.
2413 */
2414 if (ofp->f_type != DTYPE_SOCKET)
2415 return (false);
2416 so = ofp->f_data;
2417 if (so->so_type != SOCK_STREAM)
2418 return (false);
2419 /*
2420 * SCTP one-to-one style sockets currently don't work with
2421 * sendfile().
2422 */
2423 if (so->so_proto->pr_protocol == IPPROTO_SCTP)
2424 return (false);
2425 return (!SOLISTENING(so));
2426 }
2427
2428 static bool
is_regular_file(struct file * fp)2429 is_regular_file(struct file *fp)
2430 {
2431
2432 return (fp->f_type == DTYPE_VNODE && fp->f_vnode != NULL &&
2433 fp->f_vnode->v_type == VREG);
2434 }
2435
2436 static int
sendfile_fallback(struct thread * td,struct file * fp,l_int out,off_t * offset,l_size_t count,off_t * sbytes)2437 sendfile_fallback(struct thread *td, struct file *fp, l_int out,
2438 off_t *offset, l_size_t count, off_t *sbytes)
2439 {
2440 off_t current_offset, out_offset, to_send;
2441 l_size_t bytes_sent, n_read;
2442 struct file *ofp;
2443 struct iovec aiov;
2444 struct uio auio;
2445 bool seekable;
2446 size_t bufsz;
2447 void *buf;
2448 int flags, error;
2449
2450 if (offset == NULL) {
2451 if ((error = fo_seek(fp, 0, SEEK_CUR, td)) != 0)
2452 return (error);
2453 current_offset = td->td_uretoff.tdu_off;
2454 } else {
2455 if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0)
2456 return (ESPIPE);
2457 current_offset = *offset;
2458 }
2459 error = fget_write(td, out, &cap_pwrite_rights, &ofp);
2460 if (error != 0)
2461 return (error);
2462 seekable = (ofp->f_ops->fo_flags & DFLAG_SEEKABLE) != 0;
2463 if (seekable) {
2464 if ((error = fo_seek(ofp, 0, SEEK_CUR, td)) != 0)
2465 goto drop;
2466 out_offset = td->td_uretoff.tdu_off;
2467 } else
2468 out_offset = 0;
2469
2470 flags = FOF_OFFSET | FOF_NOUPDATE;
2471 bufsz = min(count, maxphys);
2472 buf = malloc(bufsz, M_LINUX, M_WAITOK);
2473 bytes_sent = 0;
2474 while (bytes_sent < count) {
2475 to_send = min(count - bytes_sent, bufsz);
2476 aiov.iov_base = buf;
2477 aiov.iov_len = bufsz;
2478 auio.uio_iov = &aiov;
2479 auio.uio_iovcnt = 1;
2480 auio.uio_segflg = UIO_SYSSPACE;
2481 auio.uio_td = td;
2482 auio.uio_rw = UIO_READ;
2483 auio.uio_offset = current_offset;
2484 auio.uio_resid = to_send;
2485 error = fo_read(fp, &auio, fp->f_cred, flags, td);
2486 if (error != 0)
2487 break;
2488 n_read = to_send - auio.uio_resid;
2489 if (n_read == 0)
2490 break;
2491 aiov.iov_base = buf;
2492 aiov.iov_len = bufsz;
2493 auio.uio_iov = &aiov;
2494 auio.uio_iovcnt = 1;
2495 auio.uio_segflg = UIO_SYSSPACE;
2496 auio.uio_td = td;
2497 auio.uio_rw = UIO_WRITE;
2498 auio.uio_offset = (seekable) ? out_offset : 0;
2499 auio.uio_resid = n_read;
2500 error = fo_write(ofp, &auio, ofp->f_cred, flags, td);
2501 if (error != 0)
2502 break;
2503 bytes_sent += n_read;
2504 current_offset += n_read;
2505 out_offset += n_read;
2506 }
2507 free(buf, M_LINUX);
2508
2509 if (error == 0) {
2510 *sbytes = bytes_sent;
2511 if (offset != NULL)
2512 *offset = current_offset;
2513 else
2514 error = fo_seek(fp, current_offset, SEEK_SET, td);
2515 }
2516 if (error == 0 && seekable)
2517 error = fo_seek(ofp, out_offset, SEEK_SET, td);
2518
2519 drop:
2520 fdrop(ofp, td);
2521 return (error);
2522 }
2523
2524 static int
sendfile_sendfile(struct thread * td,struct file * fp,l_int out,off_t * offset,l_size_t count,off_t * sbytes)2525 sendfile_sendfile(struct thread *td, struct file *fp, l_int out,
2526 off_t *offset, l_size_t count, off_t *sbytes)
2527 {
2528 off_t current_offset;
2529 int error;
2530
2531 if (offset == NULL) {
2532 if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0)
2533 return (ESPIPE);
2534 if ((error = fo_seek(fp, 0, SEEK_CUR, td)) != 0)
2535 return (error);
2536 current_offset = td->td_uretoff.tdu_off;
2537 } else
2538 current_offset = *offset;
2539 error = fo_sendfile(fp, out, NULL, NULL, current_offset, count,
2540 sbytes, 0, td);
2541 if (error == EAGAIN && *sbytes > 0) {
2542 /*
2543 * The socket is non-blocking and we didn't finish sending.
2544 * Squash the error, since that's what Linux does.
2545 */
2546 error = 0;
2547 }
2548 if (error == 0) {
2549 current_offset += *sbytes;
2550 if (offset != NULL)
2551 *offset = current_offset;
2552 else
2553 error = fo_seek(fp, current_offset, SEEK_SET, td);
2554 }
2555 return (error);
2556 }
2557
2558 static int
linux_sendfile_common(struct thread * td,l_int out,l_int in,off_t * offset,l_size_t count)2559 linux_sendfile_common(struct thread *td, l_int out, l_int in,
2560 off_t *offset, l_size_t count)
2561 {
2562 struct file *fp, *ofp;
2563 off_t sbytes;
2564 int error;
2565
2566 /* Linux cannot have 0 count. */
2567 if (count <= 0 || (offset != NULL && *offset < 0))
2568 return (EINVAL);
2569
2570 AUDIT_ARG_FD(in);
2571 error = fget_read(td, in, &cap_pread_rights, &fp);
2572 if (error != 0)
2573 return (error);
2574 if ((fp->f_type != DTYPE_SHM && fp->f_type != DTYPE_VNODE) ||
2575 (fp->f_type == DTYPE_VNODE &&
2576 (fp->f_vnode == NULL || fp->f_vnode->v_type != VREG))) {
2577 error = EINVAL;
2578 goto drop;
2579 }
2580 error = fget_unlocked(td, out, &cap_no_rights, &ofp);
2581 if (error != 0)
2582 goto drop;
2583
2584 if (is_regular_file(fp) && is_regular_file(ofp)) {
2585 error = kern_copy_file_range(td, in, offset, out, NULL, count,
2586 0);
2587 } else {
2588 sbytes = 0;
2589 if (is_sendfile(fp, ofp))
2590 error = sendfile_sendfile(td, fp, out, offset, count,
2591 &sbytes);
2592 else
2593 error = sendfile_fallback(td, fp, out, offset, count,
2594 &sbytes);
2595 if (error == ENOBUFS && (ofp->f_flag & FNONBLOCK) != 0)
2596 error = EAGAIN;
2597 if (error == 0)
2598 td->td_retval[0] = sbytes;
2599 }
2600 fdrop(ofp, td);
2601
2602 drop:
2603 fdrop(fp, td);
2604 return (error);
2605 }
2606
2607 int
linux_sendfile(struct thread * td,struct linux_sendfile_args * arg)2608 linux_sendfile(struct thread *td, struct linux_sendfile_args *arg)
2609 {
2610 /*
2611 * Differences between FreeBSD and Linux sendfile:
2612 * - Linux doesn't send anything when count is 0 (FreeBSD uses 0 to
2613 * mean send the whole file).
2614 * - Linux can send to any fd whereas FreeBSD only supports sockets.
2615 * We therefore use FreeBSD sendfile where possible for performance,
2616 * but fall back on a manual copy (sendfile_fallback).
2617 * - Linux doesn't have an equivalent for FreeBSD's flags and sf_hdtr.
2618 * - Linux takes an offset pointer and updates it to the read location.
2619 * FreeBSD takes in an offset and a 'bytes read' parameter which is
2620 * only filled if it isn't NULL. We use this parameter to update the
2621 * offset pointer if it exists.
2622 * - Linux sendfile returns bytes read on success while FreeBSD
2623 * returns 0. We use the 'bytes read' parameter to get this value.
2624 */
2625
2626 off_t offset64;
2627 l_off_t offset;
2628 int error;
2629
2630 if (arg->offset != NULL) {
2631 error = copyin(arg->offset, &offset, sizeof(offset));
2632 if (error != 0)
2633 return (error);
2634 offset64 = offset;
2635 }
2636
2637 error = linux_sendfile_common(td, arg->out, arg->in,
2638 arg->offset != NULL ? &offset64 : NULL, arg->count);
2639
2640 if (error == 0 && arg->offset != NULL) {
2641 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
2642 if (offset64 > INT32_MAX)
2643 return (EOVERFLOW);
2644 #endif
2645 offset = (l_off_t)offset64;
2646 error = copyout(&offset, arg->offset, sizeof(offset));
2647 }
2648
2649 return (error);
2650 }
2651
2652 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
2653 int
linux_sendfile64(struct thread * td,struct linux_sendfile64_args * arg)2654 linux_sendfile64(struct thread *td, struct linux_sendfile64_args *arg)
2655 {
2656 off_t offset;
2657 int error;
2658
2659 if (arg->offset != NULL) {
2660 error = copyin(arg->offset, &offset, sizeof(offset));
2661 if (error != 0)
2662 return (error);
2663 }
2664
2665 error = linux_sendfile_common(td, arg->out, arg->in,
2666 arg->offset != NULL ? &offset : NULL, arg->count);
2667
2668 if (error == 0 && arg->offset != NULL)
2669 error = copyout(&offset, arg->offset, sizeof(offset));
2670
2671 return (error);
2672 }
2673
2674 /* Argument list sizes for linux_socketcall */
2675 static const unsigned char lxs_args_cnt[] = {
2676 0 /* unused*/, 3 /* socket */,
2677 3 /* bind */, 3 /* connect */,
2678 2 /* listen */, 3 /* accept */,
2679 3 /* getsockname */, 3 /* getpeername */,
2680 4 /* socketpair */, 4 /* send */,
2681 4 /* recv */, 6 /* sendto */,
2682 6 /* recvfrom */, 2 /* shutdown */,
2683 5 /* setsockopt */, 5 /* getsockopt */,
2684 3 /* sendmsg */, 3 /* recvmsg */,
2685 4 /* accept4 */, 5 /* recvmmsg */,
2686 4 /* sendmmsg */, 4 /* sendfile */
2687 };
2688 #define LINUX_ARGS_CNT (nitems(lxs_args_cnt) - 1)
2689 #define LINUX_ARG_SIZE(x) (lxs_args_cnt[x] * sizeof(l_ulong))
2690
2691 int
linux_socketcall(struct thread * td,struct linux_socketcall_args * args)2692 linux_socketcall(struct thread *td, struct linux_socketcall_args *args)
2693 {
2694 l_ulong a[6];
2695 #if defined(__amd64__) && defined(COMPAT_LINUX32)
2696 register_t l_args[6];
2697 #endif
2698 void *arg;
2699 int error;
2700
2701 if (args->what < LINUX_SOCKET || args->what > LINUX_ARGS_CNT)
2702 return (EINVAL);
2703 error = copyin(PTRIN(args->args), a, LINUX_ARG_SIZE(args->what));
2704 if (error != 0)
2705 return (error);
2706
2707 #if defined(__amd64__) && defined(COMPAT_LINUX32)
2708 for (int i = 0; i < lxs_args_cnt[args->what]; ++i)
2709 l_args[i] = a[i];
2710 arg = l_args;
2711 #else
2712 arg = a;
2713 #endif
2714 switch (args->what) {
2715 case LINUX_SOCKET:
2716 return (linux_socket(td, arg));
2717 case LINUX_BIND:
2718 return (linux_bind(td, arg));
2719 case LINUX_CONNECT:
2720 return (linux_connect(td, arg));
2721 case LINUX_LISTEN:
2722 return (linux_listen(td, arg));
2723 case LINUX_ACCEPT:
2724 return (linux_accept(td, arg));
2725 case LINUX_GETSOCKNAME:
2726 return (linux_getsockname(td, arg));
2727 case LINUX_GETPEERNAME:
2728 return (linux_getpeername(td, arg));
2729 case LINUX_SOCKETPAIR:
2730 return (linux_socketpair(td, arg));
2731 case LINUX_SEND:
2732 return (linux_send(td, arg));
2733 case LINUX_RECV:
2734 return (linux_recv(td, arg));
2735 case LINUX_SENDTO:
2736 return (linux_sendto(td, arg));
2737 case LINUX_RECVFROM:
2738 return (linux_recvfrom(td, arg));
2739 case LINUX_SHUTDOWN:
2740 return (linux_shutdown(td, arg));
2741 case LINUX_SETSOCKOPT:
2742 return (linux_setsockopt(td, arg));
2743 case LINUX_GETSOCKOPT:
2744 return (linux_getsockopt(td, arg));
2745 case LINUX_SENDMSG:
2746 return (linux_sendmsg(td, arg));
2747 case LINUX_RECVMSG:
2748 return (linux_recvmsg(td, arg));
2749 case LINUX_ACCEPT4:
2750 return (linux_accept4(td, arg));
2751 case LINUX_RECVMMSG:
2752 return (linux_recvmmsg(td, arg));
2753 case LINUX_SENDMMSG:
2754 return (linux_sendmmsg(td, arg));
2755 case LINUX_SENDFILE:
2756 return (linux_sendfile(td, arg));
2757 }
2758
2759 linux_msg(td, "socket type %d not implemented", args->what);
2760 return (ENOSYS);
2761 }
2762 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
2763