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