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