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