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