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