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