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