1c21dee17SSøren Schmidt /*- 2c21dee17SSøren Schmidt * Copyright (c) 1995 S�ren Schmidt 3c21dee17SSøren Schmidt * All rights reserved. 4c21dee17SSøren Schmidt * 5c21dee17SSøren Schmidt * Redistribution and use in source and binary forms, with or without 6c21dee17SSøren Schmidt * modification, are permitted provided that the following conditions 7c21dee17SSøren Schmidt * are met: 8c21dee17SSøren Schmidt * 1. Redistributions of source code must retain the above copyright 9c21dee17SSøren Schmidt * notice, this list of conditions and the following disclaimer 10c21dee17SSøren Schmidt * in this position and unchanged. 11c21dee17SSøren Schmidt * 2. Redistributions in binary form must reproduce the above copyright 12c21dee17SSøren Schmidt * notice, this list of conditions and the following disclaimer in the 13c21dee17SSøren Schmidt * documentation and/or other materials provided with the distribution. 14c21dee17SSøren Schmidt * 3. The name of the author may not be used to endorse or promote products 1521dc7d4fSJens Schweikhardt * derived from this software without specific prior written permission 16c21dee17SSøren Schmidt * 17c21dee17SSøren Schmidt * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18c21dee17SSøren Schmidt * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19c21dee17SSøren Schmidt * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20c21dee17SSøren Schmidt * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21c21dee17SSøren Schmidt * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22c21dee17SSøren Schmidt * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23c21dee17SSøren Schmidt * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24c21dee17SSøren Schmidt * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25c21dee17SSøren Schmidt * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26c21dee17SSøren Schmidt * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27c21dee17SSøren Schmidt */ 28c21dee17SSøren Schmidt 2916dbc7f2SDavid E. O'Brien #include <sys/cdefs.h> 3016dbc7f2SDavid E. O'Brien __FBSDID("$FreeBSD$"); 3116dbc7f2SDavid E. O'Brien 321f3dad5aSBruce Evans /* XXX we use functions that might not exist. */ 33aefce619SRuslan Ermilov #include "opt_compat.h" 34ca26842eSHajimu UMEMOTO #include "opt_inet6.h" 355591b823SEivind Eklund 36c21dee17SSøren Schmidt #include <sys/param.h> 37f2477ae1SMike Smith #include <sys/proc.h> 38c21dee17SSøren Schmidt #include <sys/systm.h> 391f3dad5aSBruce Evans #include <sys/sysproto.h> 40a9d2f8d8SRobert Watson #include <sys/capability.h> 41dad3b88aSMike Smith #include <sys/fcntl.h> 420bf301c0SJonathan Lemon #include <sys/file.h> 43104a9b7eSAlexander Kabaev #include <sys/limits.h> 444641373fSJohn Baldwin #include <sys/lock.h> 45ca26842eSHajimu UMEMOTO #include <sys/malloc.h> 464641373fSJohn Baldwin #include <sys/mutex.h> 475a8a13e0SDavid Malone #include <sys/mbuf.h> 48c21dee17SSøren Schmidt #include <sys/socket.h> 490bf301c0SJonathan Lemon #include <sys/socketvar.h> 50ca26842eSHajimu UMEMOTO #include <sys/syscallsubr.h> 5108637435SBruce Evans #include <sys/uio.h> 52ca26842eSHajimu UMEMOTO #include <sys/syslog.h> 53d0b2365eSKonstantin Belousov #include <sys/un.h> 541f3dad5aSBruce Evans 554b79449eSBjoern A. Zeeb #include <net/if.h> 56c21dee17SSøren Schmidt #include <netinet/in.h> 57f2477ae1SMike Smith #include <netinet/in_systm.h> 58f2477ae1SMike Smith #include <netinet/ip.h> 59ca26842eSHajimu UMEMOTO #ifdef INET6 60ca26842eSHajimu UMEMOTO #include <netinet/ip6.h> 61ca26842eSHajimu UMEMOTO #include <netinet6/ip6_var.h> 624b79449eSBjoern A. Zeeb #include <netinet6/in6_var.h> 63ca26842eSHajimu UMEMOTO #endif 64c21dee17SSøren Schmidt 651997c537SDavid E. O'Brien #ifdef COMPAT_LINUX32 664af27623STim J. Robbins #include <machine/../linux32/linux.h> 674af27623STim J. Robbins #include <machine/../linux32/linux32_proto.h> 681997c537SDavid E. O'Brien #else 691997c537SDavid E. O'Brien #include <machine/../linux/linux.h> 701997c537SDavid E. O'Brien #include <machine/../linux/linux_proto.h> 714af27623STim J. Robbins #endif 7240dbba57SAssar Westerlund #include <compat/linux/linux_socket.h> 73ac951e62SMarcel Moolenaar #include <compat/linux/linux_util.h> 74c21dee17SSøren Schmidt 75ca26842eSHajimu UMEMOTO static int do_sa_get(struct sockaddr **, const struct osockaddr *, int *, 76ca26842eSHajimu UMEMOTO struct malloc_type *); 77ca26842eSHajimu UMEMOTO static int linux_to_bsd_domain(int); 78ca26842eSHajimu UMEMOTO 794730796cSBill Fenner /* 80ca26842eSHajimu UMEMOTO * Reads a linux sockaddr and does any necessary translation. 81ca26842eSHajimu UMEMOTO * Linux sockaddrs don't have a length field, only a family. 824730796cSBill Fenner */ 834730796cSBill Fenner static int 84ca26842eSHajimu UMEMOTO linux_getsockaddr(struct sockaddr **sap, const struct osockaddr *osa, int len) 854730796cSBill Fenner { 86ca26842eSHajimu UMEMOTO int osalen = len; 874730796cSBill Fenner 88ca26842eSHajimu UMEMOTO return (do_sa_get(sap, osa, &osalen, M_SONAME)); 894730796cSBill Fenner } 904730796cSBill Fenner 91ca26842eSHajimu UMEMOTO /* 92ca26842eSHajimu UMEMOTO * Copy the osockaddr structure pointed to by osa to kernel, adjust 93ca26842eSHajimu UMEMOTO * family and convert to sockaddr. 94ca26842eSHajimu UMEMOTO */ 95ca26842eSHajimu UMEMOTO static int 96ca26842eSHajimu UMEMOTO do_sa_get(struct sockaddr **sap, const struct osockaddr *osa, int *osalen, 97ca26842eSHajimu UMEMOTO struct malloc_type *mtype) 98ca26842eSHajimu UMEMOTO { 99ca26842eSHajimu UMEMOTO int error=0, bdom; 100ca26842eSHajimu UMEMOTO struct sockaddr *sa; 101ca26842eSHajimu UMEMOTO struct osockaddr *kosa; 102ca26842eSHajimu UMEMOTO #ifdef INET6 103ca26842eSHajimu UMEMOTO int oldv6size; 104ca26842eSHajimu UMEMOTO struct sockaddr_in6 *sin6; 105ca26842eSHajimu UMEMOTO #endif 106*c15cdbf2SJung-uk Kim int alloclen, hdrlen, namelen; 107ca26842eSHajimu UMEMOTO 108ca26842eSHajimu UMEMOTO if (*osalen < 2 || *osalen > UCHAR_MAX || !osa) 109ca26842eSHajimu UMEMOTO return (EINVAL); 110ca26842eSHajimu UMEMOTO 111ca26842eSHajimu UMEMOTO alloclen = *osalen; 112ca26842eSHajimu UMEMOTO #ifdef INET6 113ca26842eSHajimu UMEMOTO oldv6size = 0; 114ca26842eSHajimu UMEMOTO /* 115ca26842eSHajimu UMEMOTO * Check for old (pre-RFC2553) sockaddr_in6. We may accept it 116ca26842eSHajimu UMEMOTO * if it's a v4-mapped address, so reserve the proper space 117ca26842eSHajimu UMEMOTO * for it. 118ca26842eSHajimu UMEMOTO */ 119ca26842eSHajimu UMEMOTO if (alloclen == sizeof (struct sockaddr_in6) - sizeof (u_int32_t)) { 120ca26842eSHajimu UMEMOTO alloclen = sizeof (struct sockaddr_in6); 121ca26842eSHajimu UMEMOTO oldv6size = 1; 122ca26842eSHajimu UMEMOTO } 123ca26842eSHajimu UMEMOTO #endif 124ca26842eSHajimu UMEMOTO 1251ede983cSDag-Erling Smørgrav kosa = malloc(alloclen, mtype, M_WAITOK); 126ca26842eSHajimu UMEMOTO 1274b7ef73dSDag-Erling Smørgrav if ((error = copyin(osa, kosa, *osalen))) 128ca26842eSHajimu UMEMOTO goto out; 129ca26842eSHajimu UMEMOTO 130ca26842eSHajimu UMEMOTO bdom = linux_to_bsd_domain(kosa->sa_family); 131ca26842eSHajimu UMEMOTO if (bdom == -1) { 1325cb9c68cSXin LI error = EAFNOSUPPORT; 133ca26842eSHajimu UMEMOTO goto out; 134ca26842eSHajimu UMEMOTO } 135ca26842eSHajimu UMEMOTO 136ca26842eSHajimu UMEMOTO #ifdef INET6 137ca26842eSHajimu UMEMOTO /* 138ca26842eSHajimu UMEMOTO * Older Linux IPv6 code uses obsolete RFC2133 struct sockaddr_in6, 139ca26842eSHajimu UMEMOTO * which lacks the scope id compared with RFC2553 one. If we detect 140ca26842eSHajimu UMEMOTO * the situation, reject the address and write a message to system log. 141ca26842eSHajimu UMEMOTO * 142ca26842eSHajimu UMEMOTO * Still accept addresses for which the scope id is not used. 143ca26842eSHajimu UMEMOTO */ 144ca26842eSHajimu UMEMOTO if (oldv6size && bdom == AF_INET6) { 145ca26842eSHajimu UMEMOTO sin6 = (struct sockaddr_in6 *)kosa; 146ca26842eSHajimu UMEMOTO if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr) || 147ca26842eSHajimu UMEMOTO (!IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) && 148ca26842eSHajimu UMEMOTO !IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr) && 149ca26842eSHajimu UMEMOTO !IN6_IS_ADDR_V4COMPAT(&sin6->sin6_addr) && 150ca26842eSHajimu UMEMOTO !IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) && 151ca26842eSHajimu UMEMOTO !IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))) { 152ca26842eSHajimu UMEMOTO sin6->sin6_scope_id = 0; 153ca26842eSHajimu UMEMOTO } else { 154ca26842eSHajimu UMEMOTO log(LOG_DEBUG, 1553c616032SGleb Smirnoff "obsolete pre-RFC2553 sockaddr_in6 rejected\n"); 156ca26842eSHajimu UMEMOTO error = EINVAL; 157ca26842eSHajimu UMEMOTO goto out; 158ca26842eSHajimu UMEMOTO } 159ca26842eSHajimu UMEMOTO } else 160ca26842eSHajimu UMEMOTO #endif 1615cb9c68cSXin LI if (bdom == AF_INET) { 162ca26842eSHajimu UMEMOTO alloclen = sizeof(struct sockaddr_in); 1635cb9c68cSXin LI if (*osalen < alloclen) { 1645cb9c68cSXin LI error = EINVAL; 1655cb9c68cSXin LI goto out; 1665cb9c68cSXin LI } 1675cb9c68cSXin LI } 168ca26842eSHajimu UMEMOTO 169*c15cdbf2SJung-uk Kim if (bdom == AF_LOCAL && *osalen > sizeof(struct sockaddr_un)) { 170*c15cdbf2SJung-uk Kim hdrlen = offsetof(struct sockaddr_un, sun_path); 171*c15cdbf2SJung-uk Kim namelen = strnlen(((struct sockaddr_un *)kosa)->sun_path, 172*c15cdbf2SJung-uk Kim *osalen - hdrlen); 173*c15cdbf2SJung-uk Kim if (hdrlen + namelen > sizeof(struct sockaddr_un)) { 1745da3eb94SColin Percival error = EINVAL; 1755da3eb94SColin Percival goto out; 1765da3eb94SColin Percival } 1775da3eb94SColin Percival alloclen = sizeof(struct sockaddr_un); 1785da3eb94SColin Percival } 1795da3eb94SColin Percival 180ca26842eSHajimu UMEMOTO sa = (struct sockaddr *) kosa; 181ca26842eSHajimu UMEMOTO sa->sa_family = bdom; 182ca26842eSHajimu UMEMOTO sa->sa_len = alloclen; 183ca26842eSHajimu UMEMOTO 184ca26842eSHajimu UMEMOTO *sap = sa; 185ca26842eSHajimu UMEMOTO *osalen = alloclen; 186ca26842eSHajimu UMEMOTO return (0); 187ca26842eSHajimu UMEMOTO 188ca26842eSHajimu UMEMOTO out: 1891ede983cSDag-Erling Smørgrav free(kosa, mtype); 190ca26842eSHajimu UMEMOTO return (error); 191ca26842eSHajimu UMEMOTO } 192ca26842eSHajimu UMEMOTO 193c21dee17SSøren Schmidt static int 194c21dee17SSøren Schmidt linux_to_bsd_domain(int domain) 195c21dee17SSøren Schmidt { 1963f3a4815SMarcel Moolenaar 197c21dee17SSøren Schmidt switch (domain) { 198c21dee17SSøren Schmidt case LINUX_AF_UNSPEC: 1993f3a4815SMarcel Moolenaar return (AF_UNSPEC); 200c21dee17SSøren Schmidt case LINUX_AF_UNIX: 2013f3a4815SMarcel Moolenaar return (AF_LOCAL); 202c21dee17SSøren Schmidt case LINUX_AF_INET: 2033f3a4815SMarcel Moolenaar return (AF_INET); 204ca26842eSHajimu UMEMOTO case LINUX_AF_INET6: 205ca26842eSHajimu UMEMOTO return (AF_INET6); 206c21dee17SSøren Schmidt case LINUX_AF_AX25: 2073f3a4815SMarcel Moolenaar return (AF_CCITT); 208c21dee17SSøren Schmidt case LINUX_AF_IPX: 2093f3a4815SMarcel Moolenaar return (AF_IPX); 210c21dee17SSøren Schmidt case LINUX_AF_APPLETALK: 2113f3a4815SMarcel Moolenaar return (AF_APPLETALK); 212c21dee17SSøren Schmidt } 2133f3a4815SMarcel Moolenaar return (-1); 214c21dee17SSøren Schmidt } 215c21dee17SSøren Schmidt 216ca26842eSHajimu UMEMOTO static int 217ca26842eSHajimu UMEMOTO bsd_to_linux_domain(int domain) 218ca26842eSHajimu UMEMOTO { 219ca26842eSHajimu UMEMOTO 220ca26842eSHajimu UMEMOTO switch (domain) { 221ca26842eSHajimu UMEMOTO case AF_UNSPEC: 222ca26842eSHajimu UMEMOTO return (LINUX_AF_UNSPEC); 223ca26842eSHajimu UMEMOTO case AF_LOCAL: 224ca26842eSHajimu UMEMOTO return (LINUX_AF_UNIX); 225ca26842eSHajimu UMEMOTO case AF_INET: 226ca26842eSHajimu UMEMOTO return (LINUX_AF_INET); 227ca26842eSHajimu UMEMOTO case AF_INET6: 228ca26842eSHajimu UMEMOTO return (LINUX_AF_INET6); 229ca26842eSHajimu UMEMOTO case AF_CCITT: 230ca26842eSHajimu UMEMOTO return (LINUX_AF_AX25); 231ca26842eSHajimu UMEMOTO case AF_IPX: 232ca26842eSHajimu UMEMOTO return (LINUX_AF_IPX); 233ca26842eSHajimu UMEMOTO case AF_APPLETALK: 234ca26842eSHajimu UMEMOTO return (LINUX_AF_APPLETALK); 235ca26842eSHajimu UMEMOTO } 236ca26842eSHajimu UMEMOTO return (-1); 237ca26842eSHajimu UMEMOTO } 238ca26842eSHajimu UMEMOTO 239c21dee17SSøren Schmidt static int 240c21dee17SSøren Schmidt linux_to_bsd_sockopt_level(int level) 241c21dee17SSøren Schmidt { 2423f3a4815SMarcel Moolenaar 243c21dee17SSøren Schmidt switch (level) { 244c21dee17SSøren Schmidt case LINUX_SOL_SOCKET: 2453f3a4815SMarcel Moolenaar return (SOL_SOCKET); 246c21dee17SSøren Schmidt } 2473f3a4815SMarcel Moolenaar return (level); 248c21dee17SSøren Schmidt } 249c21dee17SSøren Schmidt 2503f3a4815SMarcel Moolenaar static int 25184b11cd8SMitsuru IWASAKI bsd_to_linux_sockopt_level(int level) 25284b11cd8SMitsuru IWASAKI { 25384b11cd8SMitsuru IWASAKI 25484b11cd8SMitsuru IWASAKI switch (level) { 25584b11cd8SMitsuru IWASAKI case SOL_SOCKET: 25684b11cd8SMitsuru IWASAKI return (LINUX_SOL_SOCKET); 25784b11cd8SMitsuru IWASAKI } 25884b11cd8SMitsuru IWASAKI return (level); 25984b11cd8SMitsuru IWASAKI } 26084b11cd8SMitsuru IWASAKI 26184b11cd8SMitsuru IWASAKI static int 2623f3a4815SMarcel Moolenaar linux_to_bsd_ip_sockopt(int opt) 263c21dee17SSøren Schmidt { 2643f3a4815SMarcel Moolenaar 265c21dee17SSøren Schmidt switch (opt) { 266c21dee17SSøren Schmidt case LINUX_IP_TOS: 2673f3a4815SMarcel Moolenaar return (IP_TOS); 268c21dee17SSøren Schmidt case LINUX_IP_TTL: 2693f3a4815SMarcel Moolenaar return (IP_TTL); 27066ff6a3cSBill Fenner case LINUX_IP_OPTIONS: 2713f3a4815SMarcel Moolenaar return (IP_OPTIONS); 27266ff6a3cSBill Fenner case LINUX_IP_MULTICAST_IF: 2733f3a4815SMarcel Moolenaar return (IP_MULTICAST_IF); 27466ff6a3cSBill Fenner case LINUX_IP_MULTICAST_TTL: 2753f3a4815SMarcel Moolenaar return (IP_MULTICAST_TTL); 27666ff6a3cSBill Fenner case LINUX_IP_MULTICAST_LOOP: 2773f3a4815SMarcel Moolenaar return (IP_MULTICAST_LOOP); 27866ff6a3cSBill Fenner case LINUX_IP_ADD_MEMBERSHIP: 2793f3a4815SMarcel Moolenaar return (IP_ADD_MEMBERSHIP); 28066ff6a3cSBill Fenner case LINUX_IP_DROP_MEMBERSHIP: 2813f3a4815SMarcel Moolenaar return (IP_DROP_MEMBERSHIP); 28266ff6a3cSBill Fenner case LINUX_IP_HDRINCL: 2833f3a4815SMarcel Moolenaar return (IP_HDRINCL); 284c21dee17SSøren Schmidt } 2853f3a4815SMarcel Moolenaar return (-1); 286c21dee17SSøren Schmidt } 287c21dee17SSøren Schmidt 288c21dee17SSøren Schmidt static int 289c21dee17SSøren Schmidt linux_to_bsd_so_sockopt(int opt) 290c21dee17SSøren Schmidt { 2913f3a4815SMarcel Moolenaar 292c21dee17SSøren Schmidt switch (opt) { 293c21dee17SSøren Schmidt case LINUX_SO_DEBUG: 2943f3a4815SMarcel Moolenaar return (SO_DEBUG); 295c21dee17SSøren Schmidt case LINUX_SO_REUSEADDR: 2963f3a4815SMarcel Moolenaar return (SO_REUSEADDR); 297c21dee17SSøren Schmidt case LINUX_SO_TYPE: 2983f3a4815SMarcel Moolenaar return (SO_TYPE); 299c21dee17SSøren Schmidt case LINUX_SO_ERROR: 3003f3a4815SMarcel Moolenaar return (SO_ERROR); 301c21dee17SSøren Schmidt case LINUX_SO_DONTROUTE: 3023f3a4815SMarcel Moolenaar return (SO_DONTROUTE); 303c21dee17SSøren Schmidt case LINUX_SO_BROADCAST: 3043f3a4815SMarcel Moolenaar return (SO_BROADCAST); 305c21dee17SSøren Schmidt case LINUX_SO_SNDBUF: 3063f3a4815SMarcel Moolenaar return (SO_SNDBUF); 307c21dee17SSøren Schmidt case LINUX_SO_RCVBUF: 3083f3a4815SMarcel Moolenaar return (SO_RCVBUF); 309c21dee17SSøren Schmidt case LINUX_SO_KEEPALIVE: 3103f3a4815SMarcel Moolenaar return (SO_KEEPALIVE); 311c21dee17SSøren Schmidt case LINUX_SO_OOBINLINE: 3123f3a4815SMarcel Moolenaar return (SO_OOBINLINE); 313c21dee17SSøren Schmidt case LINUX_SO_LINGER: 3143f3a4815SMarcel Moolenaar return (SO_LINGER); 315d0b2365eSKonstantin Belousov case LINUX_SO_PEERCRED: 316d0b2365eSKonstantin Belousov return (LOCAL_PEERCRED); 317d0b2365eSKonstantin Belousov case LINUX_SO_RCVLOWAT: 318d0b2365eSKonstantin Belousov return (SO_RCVLOWAT); 319d0b2365eSKonstantin Belousov case LINUX_SO_SNDLOWAT: 320d0b2365eSKonstantin Belousov return (SO_SNDLOWAT); 321d0b2365eSKonstantin Belousov case LINUX_SO_RCVTIMEO: 322d0b2365eSKonstantin Belousov return (SO_RCVTIMEO); 323d0b2365eSKonstantin Belousov case LINUX_SO_SNDTIMEO: 324d0b2365eSKonstantin Belousov return (SO_SNDTIMEO); 325d0b2365eSKonstantin Belousov case LINUX_SO_TIMESTAMP: 326d0b2365eSKonstantin Belousov return (SO_TIMESTAMP); 327d0b2365eSKonstantin Belousov case LINUX_SO_ACCEPTCONN: 328d0b2365eSKonstantin Belousov return (SO_ACCEPTCONN); 329c21dee17SSøren Schmidt } 3303f3a4815SMarcel Moolenaar return (-1); 331c21dee17SSøren Schmidt } 332c21dee17SSøren Schmidt 33340dbba57SAssar Westerlund static int 33440dbba57SAssar Westerlund linux_to_bsd_msg_flags(int flags) 33540dbba57SAssar Westerlund { 33640dbba57SAssar Westerlund int ret_flags = 0; 33740dbba57SAssar Westerlund 33840dbba57SAssar Westerlund if (flags & LINUX_MSG_OOB) 33940dbba57SAssar Westerlund ret_flags |= MSG_OOB; 34040dbba57SAssar Westerlund if (flags & LINUX_MSG_PEEK) 34140dbba57SAssar Westerlund ret_flags |= MSG_PEEK; 34240dbba57SAssar Westerlund if (flags & LINUX_MSG_DONTROUTE) 34340dbba57SAssar Westerlund ret_flags |= MSG_DONTROUTE; 34440dbba57SAssar Westerlund if (flags & LINUX_MSG_CTRUNC) 34540dbba57SAssar Westerlund ret_flags |= MSG_CTRUNC; 34640dbba57SAssar Westerlund if (flags & LINUX_MSG_TRUNC) 34740dbba57SAssar Westerlund ret_flags |= MSG_TRUNC; 34840dbba57SAssar Westerlund if (flags & LINUX_MSG_DONTWAIT) 34940dbba57SAssar Westerlund ret_flags |= MSG_DONTWAIT; 35040dbba57SAssar Westerlund if (flags & LINUX_MSG_EOR) 35140dbba57SAssar Westerlund ret_flags |= MSG_EOR; 35240dbba57SAssar Westerlund if (flags & LINUX_MSG_WAITALL) 35340dbba57SAssar Westerlund ret_flags |= MSG_WAITALL; 3548d6e40c3SMaxim Sobolev if (flags & LINUX_MSG_NOSIGNAL) 3558d6e40c3SMaxim Sobolev ret_flags |= MSG_NOSIGNAL; 35640dbba57SAssar Westerlund #if 0 /* not handled */ 35740dbba57SAssar Westerlund if (flags & LINUX_MSG_PROXY) 35840dbba57SAssar Westerlund ; 35940dbba57SAssar Westerlund if (flags & LINUX_MSG_FIN) 36040dbba57SAssar Westerlund ; 36140dbba57SAssar Westerlund if (flags & LINUX_MSG_SYN) 36240dbba57SAssar Westerlund ; 36340dbba57SAssar Westerlund if (flags & LINUX_MSG_CONFIRM) 36440dbba57SAssar Westerlund ; 36540dbba57SAssar Westerlund if (flags & LINUX_MSG_RST) 36640dbba57SAssar Westerlund ; 36740dbba57SAssar Westerlund if (flags & LINUX_MSG_ERRQUEUE) 36840dbba57SAssar Westerlund ; 36940dbba57SAssar Westerlund #endif 37040dbba57SAssar Westerlund return ret_flags; 37140dbba57SAssar Westerlund } 37240dbba57SAssar Westerlund 3735c8919adSAlexander Leidinger /* 3745c8919adSAlexander Leidinger * If bsd_to_linux_sockaddr() or linux_to_bsd_sockaddr() faults, then the 3755c8919adSAlexander Leidinger * native syscall will fault. Thus, we don't really need to check the 3765c8919adSAlexander Leidinger * return values for these functions. 3775c8919adSAlexander Leidinger */ 3785c8919adSAlexander Leidinger 3795c8919adSAlexander Leidinger static int 3805c8919adSAlexander Leidinger bsd_to_linux_sockaddr(struct sockaddr *arg) 3815c8919adSAlexander Leidinger { 3825c8919adSAlexander Leidinger struct sockaddr sa; 3835c8919adSAlexander Leidinger size_t sa_len = sizeof(struct sockaddr); 3845c8919adSAlexander Leidinger int error; 3855c8919adSAlexander Leidinger 3865c8919adSAlexander Leidinger if ((error = copyin(arg, &sa, sa_len))) 3875c8919adSAlexander Leidinger return (error); 3885c8919adSAlexander Leidinger 3895c8919adSAlexander Leidinger *(u_short *)&sa = sa.sa_family; 3905c8919adSAlexander Leidinger 3915c8919adSAlexander Leidinger error = copyout(&sa, arg, sa_len); 3925c8919adSAlexander Leidinger 3935c8919adSAlexander Leidinger return (error); 3945c8919adSAlexander Leidinger } 3955c8919adSAlexander Leidinger 3965c8919adSAlexander Leidinger static int 3975c8919adSAlexander Leidinger linux_to_bsd_sockaddr(struct sockaddr *arg, int len) 3985c8919adSAlexander Leidinger { 3995c8919adSAlexander Leidinger struct sockaddr sa; 4005c8919adSAlexander Leidinger size_t sa_len = sizeof(struct sockaddr); 4015c8919adSAlexander Leidinger int error; 4025c8919adSAlexander Leidinger 4035c8919adSAlexander Leidinger if ((error = copyin(arg, &sa, sa_len))) 4045c8919adSAlexander Leidinger return (error); 4055c8919adSAlexander Leidinger 4065c8919adSAlexander Leidinger sa.sa_family = *(sa_family_t *)&sa; 4075c8919adSAlexander Leidinger sa.sa_len = len; 4085c8919adSAlexander Leidinger 4095c8919adSAlexander Leidinger error = copyout(&sa, arg, sa_len); 4105c8919adSAlexander Leidinger 4115c8919adSAlexander Leidinger return (error); 4125c8919adSAlexander Leidinger } 4135c8919adSAlexander Leidinger 4145c8919adSAlexander Leidinger 415ca26842eSHajimu UMEMOTO static int 416ca26842eSHajimu UMEMOTO linux_sa_put(struct osockaddr *osa) 417ca26842eSHajimu UMEMOTO { 418ca26842eSHajimu UMEMOTO struct osockaddr sa; 419ca26842eSHajimu UMEMOTO int error, bdom; 420ca26842eSHajimu UMEMOTO 421ca26842eSHajimu UMEMOTO /* 422ca26842eSHajimu UMEMOTO * Only read/write the osockaddr family part, the rest is 423ca26842eSHajimu UMEMOTO * not changed. 424ca26842eSHajimu UMEMOTO */ 4254b7ef73dSDag-Erling Smørgrav error = copyin(osa, &sa, sizeof(sa.sa_family)); 426ca26842eSHajimu UMEMOTO if (error) 427ca26842eSHajimu UMEMOTO return (error); 428ca26842eSHajimu UMEMOTO 429ca26842eSHajimu UMEMOTO bdom = bsd_to_linux_domain(sa.sa_family); 430ca26842eSHajimu UMEMOTO if (bdom == -1) 431ca26842eSHajimu UMEMOTO return (EINVAL); 432ca26842eSHajimu UMEMOTO 433ca26842eSHajimu UMEMOTO sa.sa_family = bdom; 434ca26842eSHajimu UMEMOTO error = copyout(&sa, osa, sizeof(sa.sa_family)); 435ca26842eSHajimu UMEMOTO if (error) 436ca26842eSHajimu UMEMOTO return (error); 437ca26842eSHajimu UMEMOTO 438ca26842eSHajimu UMEMOTO return (0); 439ca26842eSHajimu UMEMOTO } 440ca26842eSHajimu UMEMOTO 4415a8a13e0SDavid Malone static int 44274f5d680SKonstantin Belousov linux_to_bsd_cmsg_type(int cmsg_type) 4435a8a13e0SDavid Malone { 44474f5d680SKonstantin Belousov 44574f5d680SKonstantin Belousov switch (cmsg_type) { 44674f5d680SKonstantin Belousov case LINUX_SCM_RIGHTS: 44774f5d680SKonstantin Belousov return (SCM_RIGHTS); 448605da56bSAndriy Gapon case LINUX_SCM_CREDENTIALS: 449605da56bSAndriy Gapon return (SCM_CREDS); 45074f5d680SKonstantin Belousov } 45174f5d680SKonstantin Belousov return (-1); 45274f5d680SKonstantin Belousov } 45374f5d680SKonstantin Belousov 45474f5d680SKonstantin Belousov static int 45574f5d680SKonstantin Belousov bsd_to_linux_cmsg_type(int cmsg_type) 45674f5d680SKonstantin Belousov { 45774f5d680SKonstantin Belousov 45874f5d680SKonstantin Belousov switch (cmsg_type) { 45974f5d680SKonstantin Belousov case SCM_RIGHTS: 46074f5d680SKonstantin Belousov return (LINUX_SCM_RIGHTS); 461605da56bSAndriy Gapon case SCM_CREDS: 462605da56bSAndriy Gapon return (LINUX_SCM_CREDENTIALS); 46374f5d680SKonstantin Belousov } 46474f5d680SKonstantin Belousov return (-1); 46574f5d680SKonstantin Belousov } 46674f5d680SKonstantin Belousov 46774f5d680SKonstantin Belousov static int 46874f5d680SKonstantin Belousov linux_to_bsd_msghdr(struct msghdr *bhdr, const struct l_msghdr *lhdr) 46974f5d680SKonstantin Belousov { 47074f5d680SKonstantin Belousov if (lhdr->msg_controllen > INT_MAX) 47174f5d680SKonstantin Belousov return (ENOBUFS); 47274f5d680SKonstantin Belousov 47374f5d680SKonstantin Belousov bhdr->msg_name = PTRIN(lhdr->msg_name); 47474f5d680SKonstantin Belousov bhdr->msg_namelen = lhdr->msg_namelen; 47574f5d680SKonstantin Belousov bhdr->msg_iov = PTRIN(lhdr->msg_iov); 47674f5d680SKonstantin Belousov bhdr->msg_iovlen = lhdr->msg_iovlen; 47774f5d680SKonstantin Belousov bhdr->msg_control = PTRIN(lhdr->msg_control); 478605da56bSAndriy Gapon 479605da56bSAndriy Gapon /* 480605da56bSAndriy Gapon * msg_controllen is skipped since BSD and LINUX control messages 481605da56bSAndriy Gapon * are potentially different sizes (e.g. the cred structure used 482605da56bSAndriy Gapon * by SCM_CREDS is different between the two operating system). 483605da56bSAndriy Gapon * 484605da56bSAndriy Gapon * The caller can set it (if necessary) after converting all the 485605da56bSAndriy Gapon * control messages. 486605da56bSAndriy Gapon */ 487605da56bSAndriy Gapon 48874f5d680SKonstantin Belousov bhdr->msg_flags = linux_to_bsd_msg_flags(lhdr->msg_flags); 48974f5d680SKonstantin Belousov return (0); 49074f5d680SKonstantin Belousov } 49174f5d680SKonstantin Belousov 49274f5d680SKonstantin Belousov static int 49374f5d680SKonstantin Belousov bsd_to_linux_msghdr(const struct msghdr *bhdr, struct l_msghdr *lhdr) 49474f5d680SKonstantin Belousov { 49574f5d680SKonstantin Belousov lhdr->msg_name = PTROUT(bhdr->msg_name); 49674f5d680SKonstantin Belousov lhdr->msg_namelen = bhdr->msg_namelen; 49774f5d680SKonstantin Belousov lhdr->msg_iov = PTROUT(bhdr->msg_iov); 49874f5d680SKonstantin Belousov lhdr->msg_iovlen = bhdr->msg_iovlen; 49974f5d680SKonstantin Belousov lhdr->msg_control = PTROUT(bhdr->msg_control); 500605da56bSAndriy Gapon 501605da56bSAndriy Gapon /* 502605da56bSAndriy Gapon * msg_controllen is skipped since BSD and LINUX control messages 503605da56bSAndriy Gapon * are potentially different sizes (e.g. the cred structure used 504605da56bSAndriy Gapon * by SCM_CREDS is different between the two operating system). 505605da56bSAndriy Gapon * 506605da56bSAndriy Gapon * The caller can set it (if necessary) after converting all the 507605da56bSAndriy Gapon * control messages. 508605da56bSAndriy Gapon */ 509605da56bSAndriy Gapon 51074f5d680SKonstantin Belousov /* msg_flags skipped */ 51174f5d680SKonstantin Belousov return (0); 51274f5d680SKonstantin Belousov } 51374f5d680SKonstantin Belousov 51474f5d680SKonstantin Belousov static int 51538a18e97SDmitry Chagin linux_set_socket_flags(struct thread *td, int s, int flags) 51638a18e97SDmitry Chagin { 51738a18e97SDmitry Chagin int error; 51838a18e97SDmitry Chagin 51938a18e97SDmitry Chagin if (flags & LINUX_SOCK_NONBLOCK) { 52038a18e97SDmitry Chagin error = kern_fcntl(td, s, F_SETFL, O_NONBLOCK); 52138a18e97SDmitry Chagin if (error) 52238a18e97SDmitry Chagin return (error); 52338a18e97SDmitry Chagin } 52438a18e97SDmitry Chagin if (flags & LINUX_SOCK_CLOEXEC) { 52538a18e97SDmitry Chagin error = kern_fcntl(td, s, F_SETFD, FD_CLOEXEC); 52638a18e97SDmitry Chagin if (error) 52738a18e97SDmitry Chagin return (error); 52838a18e97SDmitry Chagin } 52938a18e97SDmitry Chagin return (0); 53038a18e97SDmitry Chagin } 53138a18e97SDmitry Chagin 53238a18e97SDmitry Chagin static int 53374f5d680SKonstantin Belousov linux_sendit(struct thread *td, int s, struct msghdr *mp, int flags, 53474f5d680SKonstantin Belousov struct mbuf *control, enum uio_seg segflg) 53574f5d680SKonstantin Belousov { 5365a8a13e0SDavid Malone struct sockaddr *to; 5375a8a13e0SDavid Malone int error; 5385a8a13e0SDavid Malone 5395a8a13e0SDavid Malone if (mp->msg_name != NULL) { 5405a8a13e0SDavid Malone error = linux_getsockaddr(&to, mp->msg_name, mp->msg_namelen); 5415a8a13e0SDavid Malone if (error) 5425a8a13e0SDavid Malone return (error); 5435a8a13e0SDavid Malone mp->msg_name = to; 5445a8a13e0SDavid Malone } else 5455a8a13e0SDavid Malone to = NULL; 5465a8a13e0SDavid Malone 547a6886ef1SMaxim Sobolev error = kern_sendit(td, s, mp, linux_to_bsd_msg_flags(flags), control, 548a6886ef1SMaxim Sobolev segflg); 5495a8a13e0SDavid Malone 5505a8a13e0SDavid Malone if (to) 5511ede983cSDag-Erling Smørgrav free(to, M_SONAME); 5525a8a13e0SDavid Malone return (error); 5535a8a13e0SDavid Malone } 5545a8a13e0SDavid Malone 5553f3a4815SMarcel Moolenaar /* Return 0 if IP_HDRINCL is set for the given socket. */ 556f2477ae1SMike Smith static int 557e140eb43SDavid Malone linux_check_hdrincl(struct thread *td, int s) 558f2477ae1SMike Smith { 5593db2a843SBruce Evans int error, optval, size_val; 560f2477ae1SMike Smith 561e140eb43SDavid Malone size_val = sizeof(optval); 562e140eb43SDavid Malone error = kern_getsockopt(td, s, IPPROTO_IP, IP_HDRINCL, 563e140eb43SDavid Malone &optval, UIO_SYSSPACE, &size_val); 564e140eb43SDavid Malone if (error) 5653f3a4815SMarcel Moolenaar return (error); 5663f3a4815SMarcel Moolenaar 5673f3a4815SMarcel Moolenaar return (optval == 0); 568f2477ae1SMike Smith } 569f2477ae1SMike Smith 5705a8a13e0SDavid Malone struct linux_sendto_args { 5715a8a13e0SDavid Malone int s; 5724af27623STim J. Robbins l_uintptr_t msg; 5735a8a13e0SDavid Malone int len; 5745a8a13e0SDavid Malone int flags; 5754af27623STim J. Robbins l_uintptr_t to; 5765a8a13e0SDavid Malone int tolen; 5775a8a13e0SDavid Malone }; 5785a8a13e0SDavid Malone 579f2477ae1SMike Smith /* 580f2477ae1SMike Smith * Updated sendto() when IP_HDRINCL is set: 581f2477ae1SMike Smith * tweak endian-dependent fields in the IP packet. 582f2477ae1SMike Smith */ 583f2477ae1SMike Smith static int 58438da2381SRobert Watson linux_sendto_hdrincl(struct thread *td, struct linux_sendto_args *linux_args) 585f2477ae1SMike Smith { 586f2477ae1SMike Smith /* 587f2477ae1SMike Smith * linux_ip_copysize defines how many bytes we should copy 588f2477ae1SMike Smith * from the beginning of the IP packet before we customize it for BSD. 589a6886ef1SMaxim Sobolev * It should include all the fields we modify (ip_len and ip_off). 590f2477ae1SMike Smith */ 591f2477ae1SMike Smith #define linux_ip_copysize 8 592f2477ae1SMike Smith 593f2477ae1SMike Smith struct ip *packet; 5945a8a13e0SDavid Malone struct msghdr msg; 595a6886ef1SMaxim Sobolev struct iovec aiov[1]; 596f2477ae1SMike Smith int error; 597f2477ae1SMike Smith 598aa675b57SDavid Schultz /* Check that the packet isn't too big or too small. */ 599aa675b57SDavid Schultz if (linux_args->len < linux_ip_copysize || 600aa675b57SDavid Schultz linux_args->len > IP_MAXPACKET) 6013f3a4815SMarcel Moolenaar return (EINVAL); 602f2477ae1SMike Smith 603a6886ef1SMaxim Sobolev packet = (struct ip *)malloc(linux_args->len, M_TEMP, M_WAITOK); 604f2477ae1SMike Smith 605a6886ef1SMaxim Sobolev /* Make kernel copy of the packet to be sent */ 6064af27623STim J. Robbins if ((error = copyin(PTRIN(linux_args->msg), packet, 607a6886ef1SMaxim Sobolev linux_args->len))) 608a6886ef1SMaxim Sobolev goto goout; 609f2477ae1SMike Smith 610f2477ae1SMike Smith /* Convert fields from Linux to BSD raw IP socket format */ 6115a8a13e0SDavid Malone packet->ip_len = linux_args->len; 612f2477ae1SMike Smith packet->ip_off = ntohs(packet->ip_off); 613f2477ae1SMike Smith 614f2477ae1SMike Smith /* Prepare the msghdr and iovec structures describing the new packet */ 6154af27623STim J. Robbins msg.msg_name = PTRIN(linux_args->to); 6165a8a13e0SDavid Malone msg.msg_namelen = linux_args->tolen; 6175a8a13e0SDavid Malone msg.msg_iov = aiov; 618a6886ef1SMaxim Sobolev msg.msg_iovlen = 1; 6195a8a13e0SDavid Malone msg.msg_control = NULL; 6205a8a13e0SDavid Malone msg.msg_flags = 0; 6215a8a13e0SDavid Malone aiov[0].iov_base = (char *)packet; 622a6886ef1SMaxim Sobolev aiov[0].iov_len = linux_args->len; 623a6886ef1SMaxim Sobolev error = linux_sendit(td, linux_args->s, &msg, linux_args->flags, 62474f5d680SKonstantin Belousov NULL, UIO_SYSSPACE); 625a6886ef1SMaxim Sobolev goout: 626a6886ef1SMaxim Sobolev free(packet, M_TEMP); 6275a8a13e0SDavid Malone return (error); 628f2477ae1SMike Smith } 629f2477ae1SMike Smith 630c21dee17SSøren Schmidt struct linux_socket_args { 631c21dee17SSøren Schmidt int domain; 632c21dee17SSøren Schmidt int type; 633c21dee17SSøren Schmidt int protocol; 634c21dee17SSøren Schmidt }; 635c21dee17SSøren Schmidt 636c21dee17SSøren Schmidt static int 637b40ce416SJulian Elischer linux_socket(struct thread *td, struct linux_socket_args *args) 638c21dee17SSøren Schmidt { 639ef04503dSPeter Wemm struct socket_args /* { 640c21dee17SSøren Schmidt int domain; 641c21dee17SSøren Schmidt int type; 642c21dee17SSøren Schmidt int protocol; 643ef04503dSPeter Wemm } */ bsd_args; 6443933bde2SDmitry Chagin int retval_socket, socket_flags; 645c21dee17SSøren Schmidt 646745aaef5SKonstantin Belousov bsd_args.protocol = args->protocol; 6473933bde2SDmitry Chagin socket_flags = args->type & ~LINUX_SOCK_TYPE_MASK; 6483933bde2SDmitry Chagin if (socket_flags & ~(LINUX_SOCK_CLOEXEC | LINUX_SOCK_NONBLOCK)) 6493933bde2SDmitry Chagin return (EINVAL); 6503933bde2SDmitry Chagin bsd_args.type = args->type & LINUX_SOCK_TYPE_MASK; 651eeb63e51SDmitry Chagin if (bsd_args.type < 0 || bsd_args.type > LINUX_SOCK_MAX) 652eeb63e51SDmitry Chagin return (EINVAL); 653745aaef5SKonstantin Belousov bsd_args.domain = linux_to_bsd_domain(args->domain); 654c21dee17SSøren Schmidt if (bsd_args.domain == -1) 655d9b063ccSDmitry Chagin return (EAFNOSUPPORT); 656f2477ae1SMike Smith 6578451d0ddSKip Macy retval_socket = sys_socket(td, &bsd_args); 6586994ea54SDmitry Chagin if (retval_socket) 6596994ea54SDmitry Chagin return (retval_socket); 6606994ea54SDmitry Chagin 66138a18e97SDmitry Chagin retval_socket = linux_set_socket_flags(td, td->td_retval[0], 66238a18e97SDmitry Chagin socket_flags); 6633933bde2SDmitry Chagin if (retval_socket) { 6643933bde2SDmitry Chagin (void)kern_close(td, td->td_retval[0]); 6653933bde2SDmitry Chagin goto out; 6663933bde2SDmitry Chagin } 6673933bde2SDmitry Chagin 668f2477ae1SMike Smith if (bsd_args.type == SOCK_RAW 669f2477ae1SMike Smith && (bsd_args.protocol == IPPROTO_RAW || bsd_args.protocol == 0) 6706994ea54SDmitry Chagin && bsd_args.domain == PF_INET) { 671f2477ae1SMike Smith /* It's a raw IP socket: set the IP_HDRINCL option. */ 672e140eb43SDavid Malone int hdrincl; 673f2477ae1SMike Smith 674e140eb43SDavid Malone hdrincl = 1; 675e140eb43SDavid Malone /* We ignore any error returned by kern_setsockopt() */ 676e140eb43SDavid Malone kern_setsockopt(td, td->td_retval[0], IPPROTO_IP, IP_HDRINCL, 677e140eb43SDavid Malone &hdrincl, UIO_SYSSPACE, sizeof(hdrincl)); 678f2477ae1SMike Smith } 679ca26842eSHajimu UMEMOTO #ifdef INET6 680ca26842eSHajimu UMEMOTO /* 681d97bee3eSBjoern A. Zeeb * Linux AF_INET6 socket has IPV6_V6ONLY setsockopt set to 0 by default 682d97bee3eSBjoern A. Zeeb * and some apps depend on this. So, set V6ONLY to 0 for Linux apps. 683d97bee3eSBjoern A. Zeeb * For simplicity we do this unconditionally of the net.inet6.ip6.v6only 684d97bee3eSBjoern A. Zeeb * sysctl value. 685ca26842eSHajimu UMEMOTO */ 686d97bee3eSBjoern A. Zeeb if (bsd_args.domain == PF_INET6) { 687e140eb43SDavid Malone int v6only; 688ca26842eSHajimu UMEMOTO 689e140eb43SDavid Malone v6only = 0; 690ca26842eSHajimu UMEMOTO /* We ignore any error returned by setsockopt() */ 691e140eb43SDavid Malone kern_setsockopt(td, td->td_retval[0], IPPROTO_IPV6, IPV6_V6ONLY, 692e140eb43SDavid Malone &v6only, UIO_SYSSPACE, sizeof(v6only)); 693ca26842eSHajimu UMEMOTO } 694ca26842eSHajimu UMEMOTO #endif 6953f3a4815SMarcel Moolenaar 6963933bde2SDmitry Chagin out: 6973f3a4815SMarcel Moolenaar return (retval_socket); 698c21dee17SSøren Schmidt } 699c21dee17SSøren Schmidt 700c21dee17SSøren Schmidt struct linux_bind_args { 701c21dee17SSøren Schmidt int s; 7024af27623STim J. Robbins l_uintptr_t name; 703c21dee17SSøren Schmidt int namelen; 704c21dee17SSøren Schmidt }; 705c21dee17SSøren Schmidt 706c21dee17SSøren Schmidt static int 707b40ce416SJulian Elischer linux_bind(struct thread *td, struct linux_bind_args *args) 708c21dee17SSøren Schmidt { 709ca26842eSHajimu UMEMOTO struct sockaddr *sa; 710c21dee17SSøren Schmidt int error; 711c21dee17SSøren Schmidt 712745aaef5SKonstantin Belousov error = linux_getsockaddr(&sa, PTRIN(args->name), 713745aaef5SKonstantin Belousov args->namelen); 714ca26842eSHajimu UMEMOTO if (error) 715ca26842eSHajimu UMEMOTO return (error); 716ca26842eSHajimu UMEMOTO 717745aaef5SKonstantin Belousov error = kern_bind(td, args->s, sa); 718b33887eaSJohn Baldwin free(sa, M_SONAME); 719745aaef5SKonstantin Belousov if (error == EADDRNOTAVAIL && args->namelen != sizeof(struct sockaddr_in)) 720d4b7423fSAlexander Leidinger return (EINVAL); 721b33887eaSJohn Baldwin return (error); 722c21dee17SSøren Schmidt } 723c21dee17SSøren Schmidt 72401e0ffbaSAlexander Leidinger struct linux_connect_args { 725c21dee17SSøren Schmidt int s; 7264af27623STim J. Robbins l_uintptr_t name; 727c21dee17SSøren Schmidt int namelen; 728c21dee17SSøren Schmidt }; 729b40ce416SJulian Elischer int linux_connect(struct thread *, struct linux_connect_args *); 730c21dee17SSøren Schmidt 731930a65feSAndrew Gallatin int 732b40ce416SJulian Elischer linux_connect(struct thread *td, struct linux_connect_args *args) 733c21dee17SSøren Schmidt { 7340bf301c0SJonathan Lemon struct socket *so; 735ca26842eSHajimu UMEMOTO struct sockaddr *sa; 73639c95b83SMatthew Dillon u_int fflag; 737c21dee17SSøren Schmidt int error; 738c21dee17SSøren Schmidt 739745aaef5SKonstantin Belousov error = linux_getsockaddr(&sa, (struct osockaddr *)PTRIN(args->name), 740745aaef5SKonstantin Belousov args->namelen); 741ca26842eSHajimu UMEMOTO if (error) 742ca26842eSHajimu UMEMOTO return (error); 743ca26842eSHajimu UMEMOTO 744745aaef5SKonstantin Belousov error = kern_connect(td, args->s, sa); 745b33887eaSJohn Baldwin free(sa, M_SONAME); 7460bf301c0SJonathan Lemon if (error != EISCONN) 7470bf301c0SJonathan Lemon return (error); 7480bf301c0SJonathan Lemon 749dad3b88aSMike Smith /* 750dad3b88aSMike Smith * Linux doesn't return EISCONN the first time it occurs, 751dad3b88aSMike Smith * when on a non-blocking socket. Instead it returns the 752dad3b88aSMike Smith * error getsockopt(SOL_SOCKET, SO_ERROR) would return on BSD. 753f7f45ac8SRobert Watson * 754f7f45ac8SRobert Watson * XXXRW: Instead of using fgetsock(), check that it is a 755f7f45ac8SRobert Watson * socket and use the file descriptor reference instead of 756f7f45ac8SRobert Watson * creating a new one. 757dad3b88aSMike Smith */ 758a9d2f8d8SRobert Watson error = fgetsock(td, args->s, CAP_CONNECT, &so, &fflag); 7594641373fSJohn Baldwin if (error == 0) { 7600bf301c0SJonathan Lemon error = EISCONN; 76139c95b83SMatthew Dillon if (fflag & FNONBLOCK) { 7624641373fSJohn Baldwin SOCK_LOCK(so); 7635002a60fSMarcel Moolenaar if (so->so_emuldata == 0) 7640bf301c0SJonathan Lemon error = so->so_error; 7650bf301c0SJonathan Lemon so->so_emuldata = (void *)1; 7664641373fSJohn Baldwin SOCK_UNLOCK(so); 767dad3b88aSMike Smith } 76839c95b83SMatthew Dillon fputsock(so); 7694641373fSJohn Baldwin } 7703f3a4815SMarcel Moolenaar return (error); 771c21dee17SSøren Schmidt } 772c21dee17SSøren Schmidt 773c21dee17SSøren Schmidt struct linux_listen_args { 774c21dee17SSøren Schmidt int s; 775c21dee17SSøren Schmidt int backlog; 776c21dee17SSøren Schmidt }; 777c21dee17SSøren Schmidt 778c21dee17SSøren Schmidt static int 779b40ce416SJulian Elischer linux_listen(struct thread *td, struct linux_listen_args *args) 780c21dee17SSøren Schmidt { 781ef04503dSPeter Wemm struct listen_args /* { 782c21dee17SSøren Schmidt int s; 783c21dee17SSøren Schmidt int backlog; 784ef04503dSPeter Wemm } */ bsd_args; 785c21dee17SSøren Schmidt 786745aaef5SKonstantin Belousov bsd_args.s = args->s; 787745aaef5SKonstantin Belousov bsd_args.backlog = args->backlog; 7888451d0ddSKip Macy return (sys_listen(td, &bsd_args)); 789c21dee17SSøren Schmidt } 790c21dee17SSøren Schmidt 79101e0ffbaSAlexander Leidinger static int 792c8f37d61SDmitry Chagin linux_accept_common(struct thread *td, int s, l_uintptr_t addr, 793f83427b8SDmitry Chagin l_uintptr_t namelen, int flags) 794c21dee17SSøren Schmidt { 795ef04503dSPeter Wemm struct accept_args /* { 796c21dee17SSøren Schmidt int s; 7973db2a843SBruce Evans struct sockaddr * __restrict name; 7983db2a843SBruce Evans socklen_t * __restrict anamelen; 799ef04503dSPeter Wemm } */ bsd_args; 80093e694c9SDmitry Chagin int error; 80193e694c9SDmitry Chagin 80293e694c9SDmitry Chagin if (flags & ~(LINUX_SOCK_CLOEXEC | LINUX_SOCK_NONBLOCK)) 80393e694c9SDmitry Chagin return (EINVAL); 804c21dee17SSøren Schmidt 805c8f37d61SDmitry Chagin bsd_args.s = s; 8063db2a843SBruce Evans /* XXX: */ 807c8f37d61SDmitry Chagin bsd_args.name = (struct sockaddr * __restrict)PTRIN(addr); 808c8f37d61SDmitry Chagin bsd_args.anamelen = PTRIN(namelen);/* XXX */ 8098451d0ddSKip Macy error = sys_accept(td, &bsd_args); 8105c8919adSAlexander Leidinger bsd_to_linux_sockaddr((struct sockaddr *)bsd_args.name); 811d4b7423fSAlexander Leidinger if (error) { 812c8f37d61SDmitry Chagin if (error == EFAULT && namelen != sizeof(struct sockaddr_in)) 813d4b7423fSAlexander Leidinger return (EINVAL); 814dba5ab66SMarcel Moolenaar return (error); 815d4b7423fSAlexander Leidinger } 816dba5ab66SMarcel Moolenaar 817dba5ab66SMarcel Moolenaar /* 818dba5ab66SMarcel Moolenaar * linux appears not to copy flags from the parent socket to the 81993e694c9SDmitry Chagin * accepted one, so we must clear the flags in the new descriptor 82093e694c9SDmitry Chagin * and apply the requested flags. 821dba5ab66SMarcel Moolenaar */ 82293e694c9SDmitry Chagin error = kern_fcntl(td, td->td_retval[0], F_SETFL, 0); 82393e694c9SDmitry Chagin if (error) 82493e694c9SDmitry Chagin goto out; 82593e694c9SDmitry Chagin error = linux_set_socket_flags(td, td->td_retval[0], flags); 82693e694c9SDmitry Chagin if (error) 82793e694c9SDmitry Chagin goto out; 82893e694c9SDmitry Chagin if (addr) 82993e694c9SDmitry Chagin error = linux_sa_put(PTRIN(addr)); 83093e694c9SDmitry Chagin 83193e694c9SDmitry Chagin out: 83293e694c9SDmitry Chagin if (error) { 83393e694c9SDmitry Chagin (void)kern_close(td, td->td_retval[0]); 83493e694c9SDmitry Chagin td->td_retval[0] = 0; 83593e694c9SDmitry Chagin } 83693e694c9SDmitry Chagin return (error); 837c21dee17SSøren Schmidt } 838c21dee17SSøren Schmidt 839c8f37d61SDmitry Chagin struct linux_accept_args { 840c8f37d61SDmitry Chagin int s; 841c8f37d61SDmitry Chagin l_uintptr_t addr; 842c8f37d61SDmitry Chagin l_uintptr_t namelen; 843c8f37d61SDmitry Chagin }; 844c8f37d61SDmitry Chagin 845c8f37d61SDmitry Chagin static int 846c8f37d61SDmitry Chagin linux_accept(struct thread *td, struct linux_accept_args *args) 847c8f37d61SDmitry Chagin { 848c8f37d61SDmitry Chagin 849c8f37d61SDmitry Chagin return (linux_accept_common(td, args->s, args->addr, 850f83427b8SDmitry Chagin args->namelen, 0)); 851c8f37d61SDmitry Chagin } 852c8f37d61SDmitry Chagin 853f8cd0af2SDmitry Chagin struct linux_accept4_args { 854f8cd0af2SDmitry Chagin int s; 855f8cd0af2SDmitry Chagin l_uintptr_t addr; 856f8cd0af2SDmitry Chagin l_uintptr_t namelen; 857f8cd0af2SDmitry Chagin int flags; 858f8cd0af2SDmitry Chagin }; 859f8cd0af2SDmitry Chagin 860f8cd0af2SDmitry Chagin static int 861f8cd0af2SDmitry Chagin linux_accept4(struct thread *td, struct linux_accept4_args *args) 862f8cd0af2SDmitry Chagin { 863f8cd0af2SDmitry Chagin 864f8cd0af2SDmitry Chagin return (linux_accept_common(td, args->s, args->addr, 865f8cd0af2SDmitry Chagin args->namelen, args->flags)); 866f8cd0af2SDmitry Chagin } 867f8cd0af2SDmitry Chagin 86801e0ffbaSAlexander Leidinger struct linux_getsockname_args { 869c21dee17SSøren Schmidt int s; 8704af27623STim J. Robbins l_uintptr_t addr; 8714af27623STim J. Robbins l_uintptr_t namelen; 872c21dee17SSøren Schmidt }; 873c21dee17SSøren Schmidt 87401e0ffbaSAlexander Leidinger static int 875b40ce416SJulian Elischer linux_getsockname(struct thread *td, struct linux_getsockname_args *args) 876c21dee17SSøren Schmidt { 877ef04503dSPeter Wemm struct getsockname_args /* { 878c21dee17SSøren Schmidt int fdes; 8793db2a843SBruce Evans struct sockaddr * __restrict asa; 8803db2a843SBruce Evans socklen_t * __restrict alen; 881ef04503dSPeter Wemm } */ bsd_args; 882c21dee17SSøren Schmidt int error; 883c21dee17SSøren Schmidt 884745aaef5SKonstantin Belousov bsd_args.fdes = args->s; 8853db2a843SBruce Evans /* XXX: */ 886745aaef5SKonstantin Belousov bsd_args.asa = (struct sockaddr * __restrict)PTRIN(args->addr); 887745aaef5SKonstantin Belousov bsd_args.alen = PTRIN(args->namelen); /* XXX */ 8888451d0ddSKip Macy error = sys_getsockname(td, &bsd_args); 8895c8919adSAlexander Leidinger bsd_to_linux_sockaddr((struct sockaddr *)bsd_args.asa); 890ca26842eSHajimu UMEMOTO if (error) 891ca26842eSHajimu UMEMOTO return (error); 892745aaef5SKonstantin Belousov error = linux_sa_put(PTRIN(args->addr)); 893ca26842eSHajimu UMEMOTO if (error) 894ca26842eSHajimu UMEMOTO return (error); 895ca26842eSHajimu UMEMOTO return (0); 896c21dee17SSøren Schmidt } 897c21dee17SSøren Schmidt 89801e0ffbaSAlexander Leidinger struct linux_getpeername_args { 899c21dee17SSøren Schmidt int s; 9004af27623STim J. Robbins l_uintptr_t addr; 9014af27623STim J. Robbins l_uintptr_t namelen; 902c21dee17SSøren Schmidt }; 903c21dee17SSøren Schmidt 90401e0ffbaSAlexander Leidinger static int 905b40ce416SJulian Elischer linux_getpeername(struct thread *td, struct linux_getpeername_args *args) 906c21dee17SSøren Schmidt { 9075c8919adSAlexander Leidinger struct getpeername_args /* { 908c21dee17SSøren Schmidt int fdes; 909c21dee17SSøren Schmidt caddr_t asa; 910c21dee17SSøren Schmidt int *alen; 911ef04503dSPeter Wemm } */ bsd_args; 912c21dee17SSøren Schmidt int error; 913c21dee17SSøren Schmidt 914745aaef5SKonstantin Belousov bsd_args.fdes = args->s; 915745aaef5SKonstantin Belousov bsd_args.asa = (struct sockaddr *)PTRIN(args->addr); 916745aaef5SKonstantin Belousov bsd_args.alen = (int *)PTRIN(args->namelen); 9178451d0ddSKip Macy error = sys_getpeername(td, &bsd_args); 9185c8919adSAlexander Leidinger bsd_to_linux_sockaddr((struct sockaddr *)bsd_args.asa); 919ca26842eSHajimu UMEMOTO if (error) 920ca26842eSHajimu UMEMOTO return (error); 921745aaef5SKonstantin Belousov error = linux_sa_put(PTRIN(args->addr)); 922ca26842eSHajimu UMEMOTO if (error) 923ca26842eSHajimu UMEMOTO return (error); 924ca26842eSHajimu UMEMOTO return (0); 925c21dee17SSøren Schmidt } 926c21dee17SSøren Schmidt 92701e0ffbaSAlexander Leidinger struct linux_socketpair_args { 928c21dee17SSøren Schmidt int domain; 929c21dee17SSøren Schmidt int type; 930c21dee17SSøren Schmidt int protocol; 9314af27623STim J. Robbins l_uintptr_t rsv; 932c21dee17SSøren Schmidt }; 933c21dee17SSøren Schmidt 93401e0ffbaSAlexander Leidinger static int 935b40ce416SJulian Elischer linux_socketpair(struct thread *td, struct linux_socketpair_args *args) 936c21dee17SSøren Schmidt { 937ef04503dSPeter Wemm struct socketpair_args /* { 938c21dee17SSøren Schmidt int domain; 939c21dee17SSøren Schmidt int type; 940c21dee17SSøren Schmidt int protocol; 941c21dee17SSøren Schmidt int *rsv; 942ef04503dSPeter Wemm } */ bsd_args; 94339253cf9SDmitry Chagin int error, socket_flags; 94439253cf9SDmitry Chagin int sv[2]; 945c21dee17SSøren Schmidt 946745aaef5SKonstantin Belousov bsd_args.domain = linux_to_bsd_domain(args->domain); 9471a52a4abSDmitry Chagin if (bsd_args.domain != PF_LOCAL) 9481a52a4abSDmitry Chagin return (EAFNOSUPPORT); 9493f3a4815SMarcel Moolenaar 95039253cf9SDmitry Chagin socket_flags = args->type & ~LINUX_SOCK_TYPE_MASK; 95139253cf9SDmitry Chagin if (socket_flags & ~(LINUX_SOCK_CLOEXEC | LINUX_SOCK_NONBLOCK)) 95239253cf9SDmitry Chagin return (EINVAL); 95339253cf9SDmitry Chagin bsd_args.type = args->type & LINUX_SOCK_TYPE_MASK; 95439253cf9SDmitry Chagin if (bsd_args.type < 0 || bsd_args.type > LINUX_SOCK_MAX) 95539253cf9SDmitry Chagin return (EINVAL); 95639253cf9SDmitry Chagin 9571a52a4abSDmitry Chagin if (args->protocol != 0 && args->protocol != PF_UNIX) 9581a52a4abSDmitry Chagin 9591a52a4abSDmitry Chagin /* 9601a52a4abSDmitry Chagin * Use of PF_UNIX as protocol argument is not right, 9611a52a4abSDmitry Chagin * but Linux does it. 9621a52a4abSDmitry Chagin * Do not map PF_UNIX as its Linux value is identical 9631a52a4abSDmitry Chagin * to FreeBSD one. 9641a52a4abSDmitry Chagin */ 9651a52a4abSDmitry Chagin return (EPROTONOSUPPORT); 96640092d93SDmitry Chagin else 9671a52a4abSDmitry Chagin bsd_args.protocol = 0; 968745aaef5SKonstantin Belousov bsd_args.rsv = (int *)PTRIN(args->rsv); 96939253cf9SDmitry Chagin error = kern_socketpair(td, bsd_args.domain, bsd_args.type, 97039253cf9SDmitry Chagin bsd_args.protocol, sv); 97139253cf9SDmitry Chagin if (error) 97239253cf9SDmitry Chagin return (error); 97339253cf9SDmitry Chagin error = linux_set_socket_flags(td, sv[0], socket_flags); 97439253cf9SDmitry Chagin if (error) 97539253cf9SDmitry Chagin goto out; 97639253cf9SDmitry Chagin error = linux_set_socket_flags(td, sv[1], socket_flags); 97739253cf9SDmitry Chagin if (error) 97839253cf9SDmitry Chagin goto out; 97939253cf9SDmitry Chagin 98039253cf9SDmitry Chagin error = copyout(sv, bsd_args.rsv, 2 * sizeof(int)); 98139253cf9SDmitry Chagin 98239253cf9SDmitry Chagin out: 98339253cf9SDmitry Chagin if (error) { 98439253cf9SDmitry Chagin (void)kern_close(td, sv[0]); 98539253cf9SDmitry Chagin (void)kern_close(td, sv[1]); 98639253cf9SDmitry Chagin } 98739253cf9SDmitry Chagin return (error); 988c21dee17SSøren Schmidt } 989c21dee17SSøren Schmidt 99001e0ffbaSAlexander Leidinger struct linux_send_args { 991c21dee17SSøren Schmidt int s; 9924af27623STim J. Robbins l_uintptr_t msg; 993c21dee17SSøren Schmidt int len; 994c21dee17SSøren Schmidt int flags; 995c21dee17SSøren Schmidt }; 996c21dee17SSøren Schmidt 99701e0ffbaSAlexander Leidinger static int 998b40ce416SJulian Elischer linux_send(struct thread *td, struct linux_send_args *args) 999c21dee17SSøren Schmidt { 100087d72a8fSPoul-Henning Kamp struct sendto_args /* { 1001c21dee17SSøren Schmidt int s; 1002c21dee17SSøren Schmidt caddr_t buf; 1003044af7c3SJonathan Mini int len; 1004c21dee17SSøren Schmidt int flags; 100587d72a8fSPoul-Henning Kamp caddr_t to; 100687d72a8fSPoul-Henning Kamp int tolen; 1007ef04503dSPeter Wemm } */ bsd_args; 1008c21dee17SSøren Schmidt 1009745aaef5SKonstantin Belousov bsd_args.s = args->s; 1010745aaef5SKonstantin Belousov bsd_args.buf = (caddr_t)PTRIN(args->msg); 1011745aaef5SKonstantin Belousov bsd_args.len = args->len; 1012745aaef5SKonstantin Belousov bsd_args.flags = args->flags; 101387d72a8fSPoul-Henning Kamp bsd_args.to = NULL; 101487d72a8fSPoul-Henning Kamp bsd_args.tolen = 0; 10158451d0ddSKip Macy return sys_sendto(td, &bsd_args); 1016c21dee17SSøren Schmidt } 1017c21dee17SSøren Schmidt 101801e0ffbaSAlexander Leidinger struct linux_recv_args { 1019c21dee17SSøren Schmidt int s; 10204af27623STim J. Robbins l_uintptr_t msg; 1021c21dee17SSøren Schmidt int len; 1022c21dee17SSøren Schmidt int flags; 1023c21dee17SSøren Schmidt }; 1024c21dee17SSøren Schmidt 102501e0ffbaSAlexander Leidinger static int 1026b40ce416SJulian Elischer linux_recv(struct thread *td, struct linux_recv_args *args) 1027c21dee17SSøren Schmidt { 102887d72a8fSPoul-Henning Kamp struct recvfrom_args /* { 1029c21dee17SSøren Schmidt int s; 1030c21dee17SSøren Schmidt caddr_t buf; 1031c21dee17SSøren Schmidt int len; 1032c21dee17SSøren Schmidt int flags; 103387d72a8fSPoul-Henning Kamp struct sockaddr *from; 103487d72a8fSPoul-Henning Kamp socklen_t fromlenaddr; 1035ef04503dSPeter Wemm } */ bsd_args; 1036c21dee17SSøren Schmidt 1037745aaef5SKonstantin Belousov bsd_args.s = args->s; 1038745aaef5SKonstantin Belousov bsd_args.buf = (caddr_t)PTRIN(args->msg); 1039745aaef5SKonstantin Belousov bsd_args.len = args->len; 10403980a435SDmitry Chagin bsd_args.flags = linux_to_bsd_msg_flags(args->flags); 104187d72a8fSPoul-Henning Kamp bsd_args.from = NULL; 104287d72a8fSPoul-Henning Kamp bsd_args.fromlenaddr = 0; 10438451d0ddSKip Macy return (sys_recvfrom(td, &bsd_args)); 1044c21dee17SSøren Schmidt } 1045c21dee17SSøren Schmidt 1046c21dee17SSøren Schmidt static int 1047b40ce416SJulian Elischer linux_sendto(struct thread *td, struct linux_sendto_args *args) 1048c21dee17SSøren Schmidt { 10495a8a13e0SDavid Malone struct msghdr msg; 10505a8a13e0SDavid Malone struct iovec aiov; 10515a8a13e0SDavid Malone int error; 1052c21dee17SSøren Schmidt 1053745aaef5SKonstantin Belousov if (linux_check_hdrincl(td, args->s) == 0) 1054f2477ae1SMike Smith /* IP_HDRINCL set, tweak the packet before sending */ 1055745aaef5SKonstantin Belousov return (linux_sendto_hdrincl(td, args)); 1056f2477ae1SMike Smith 1057745aaef5SKonstantin Belousov msg.msg_name = PTRIN(args->to); 1058745aaef5SKonstantin Belousov msg.msg_namelen = args->tolen; 10595a8a13e0SDavid Malone msg.msg_iov = &aiov; 10605a8a13e0SDavid Malone msg.msg_iovlen = 1; 10615a8a13e0SDavid Malone msg.msg_control = NULL; 10625a8a13e0SDavid Malone msg.msg_flags = 0; 1063745aaef5SKonstantin Belousov aiov.iov_base = PTRIN(args->msg); 1064745aaef5SKonstantin Belousov aiov.iov_len = args->len; 106574f5d680SKonstantin Belousov error = linux_sendit(td, args->s, &msg, args->flags, NULL, 106674f5d680SKonstantin Belousov UIO_USERSPACE); 10675a8a13e0SDavid Malone return (error); 1068c21dee17SSøren Schmidt } 1069c21dee17SSøren Schmidt 107001e0ffbaSAlexander Leidinger struct linux_recvfrom_args { 1071c21dee17SSøren Schmidt int s; 10724af27623STim J. Robbins l_uintptr_t buf; 1073c21dee17SSøren Schmidt int len; 1074c21dee17SSøren Schmidt int flags; 10754af27623STim J. Robbins l_uintptr_t from; 10764af27623STim J. Robbins l_uintptr_t fromlen; 1077c21dee17SSøren Schmidt }; 1078c21dee17SSøren Schmidt 107901e0ffbaSAlexander Leidinger static int 1080b40ce416SJulian Elischer linux_recvfrom(struct thread *td, struct linux_recvfrom_args *args) 1081c21dee17SSøren Schmidt { 1082ef04503dSPeter Wemm struct recvfrom_args /* { 1083c21dee17SSøren Schmidt int s; 1084c21dee17SSøren Schmidt caddr_t buf; 1085c21dee17SSøren Schmidt size_t len; 1086c21dee17SSøren Schmidt int flags; 10873db2a843SBruce Evans struct sockaddr * __restrict from; 10883db2a843SBruce Evans socklen_t * __restrict fromlenaddr; 1089ef04503dSPeter Wemm } */ bsd_args; 10905c8919adSAlexander Leidinger size_t len; 1091c21dee17SSøren Schmidt int error; 1092c21dee17SSøren Schmidt 1093745aaef5SKonstantin Belousov if ((error = copyin(PTRIN(args->fromlen), &len, sizeof(size_t)))) 10943f3a4815SMarcel Moolenaar return (error); 10953f3a4815SMarcel Moolenaar 1096745aaef5SKonstantin Belousov bsd_args.s = args->s; 1097745aaef5SKonstantin Belousov bsd_args.buf = PTRIN(args->buf); 1098745aaef5SKonstantin Belousov bsd_args.len = args->len; 1099745aaef5SKonstantin Belousov bsd_args.flags = linux_to_bsd_msg_flags(args->flags); 11003db2a843SBruce Evans /* XXX: */ 1101745aaef5SKonstantin Belousov bsd_args.from = (struct sockaddr * __restrict)PTRIN(args->from); 1102745aaef5SKonstantin Belousov bsd_args.fromlenaddr = PTRIN(args->fromlen);/* XXX */ 11035c8919adSAlexander Leidinger 11045c8919adSAlexander Leidinger linux_to_bsd_sockaddr((struct sockaddr *)bsd_args.from, len); 11058451d0ddSKip Macy error = sys_recvfrom(td, &bsd_args); 11065c8919adSAlexander Leidinger bsd_to_linux_sockaddr((struct sockaddr *)bsd_args.from); 11075c8919adSAlexander Leidinger 1108ca26842eSHajimu UMEMOTO if (error) 1109ca26842eSHajimu UMEMOTO return (error); 1110745aaef5SKonstantin Belousov if (args->from) { 11114af27623STim J. Robbins error = linux_sa_put((struct osockaddr *) 1112745aaef5SKonstantin Belousov PTRIN(args->from)); 1113ca26842eSHajimu UMEMOTO if (error) 1114ca26842eSHajimu UMEMOTO return (error); 1115ca26842eSHajimu UMEMOTO } 1116ca26842eSHajimu UMEMOTO return (0); 1117ca26842eSHajimu UMEMOTO } 1118ca26842eSHajimu UMEMOTO 111901e0ffbaSAlexander Leidinger struct linux_sendmsg_args { 1120ca26842eSHajimu UMEMOTO int s; 11214af27623STim J. Robbins l_uintptr_t msg; 1122ca26842eSHajimu UMEMOTO int flags; 1123ca26842eSHajimu UMEMOTO }; 1124ca26842eSHajimu UMEMOTO 112501e0ffbaSAlexander Leidinger static int 1126ca26842eSHajimu UMEMOTO linux_sendmsg(struct thread *td, struct linux_sendmsg_args *args) 1127ca26842eSHajimu UMEMOTO { 112874f5d680SKonstantin Belousov struct cmsghdr *cmsg; 1129605da56bSAndriy Gapon struct cmsgcred cmcred; 113074f5d680SKonstantin Belousov struct mbuf *control; 1131ca26842eSHajimu UMEMOTO struct msghdr msg; 113274f5d680SKonstantin Belousov struct l_cmsghdr linux_cmsg; 113374f5d680SKonstantin Belousov struct l_cmsghdr *ptr_cmsg; 113474f5d680SKonstantin Belousov struct l_msghdr linux_msg; 1135552afd9cSPoul-Henning Kamp struct iovec *iov; 113674f5d680SKonstantin Belousov socklen_t datalen; 1137605da56bSAndriy Gapon struct sockaddr *sa; 1138605da56bSAndriy Gapon sa_family_t sa_family; 113974f5d680SKonstantin Belousov void *data; 1140ca26842eSHajimu UMEMOTO int error; 1141ca26842eSHajimu UMEMOTO 114274f5d680SKonstantin Belousov error = copyin(PTRIN(args->msg), &linux_msg, sizeof(linux_msg)); 114374f5d680SKonstantin Belousov if (error) 114474f5d680SKonstantin Belousov return (error); 1145d72a6158SRobert Watson 1146d72a6158SRobert Watson /* 1147d72a6158SRobert Watson * Some Linux applications (ping) define a non-NULL control data 1148d72a6158SRobert Watson * pointer, but a msg_controllen of 0, which is not allowed in the 1149d72a6158SRobert Watson * FreeBSD system call interface. NULL the msg_control pointer in 1150d72a6158SRobert Watson * order to handle this case. This should be checked, but allows the 1151d72a6158SRobert Watson * Linux ping to work. 1152d72a6158SRobert Watson */ 1153605da56bSAndriy Gapon if (PTRIN(linux_msg.msg_control) != NULL && linux_msg.msg_controllen == 0) 1154605da56bSAndriy Gapon linux_msg.msg_control = PTROUT(NULL); 1155605da56bSAndriy Gapon 1156605da56bSAndriy Gapon error = linux_to_bsd_msghdr(&msg, &linux_msg); 1157605da56bSAndriy Gapon if (error) 1158605da56bSAndriy Gapon return (error); 115974f5d680SKonstantin Belousov 116074f5d680SKonstantin Belousov #ifdef COMPAT_LINUX32 116174f5d680SKonstantin Belousov error = linux32_copyiniov(PTRIN(msg.msg_iov), msg.msg_iovlen, 116274f5d680SKonstantin Belousov &iov, EMSGSIZE); 116374f5d680SKonstantin Belousov #else 1164552afd9cSPoul-Henning Kamp error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE); 116574f5d680SKonstantin Belousov #endif 1166552afd9cSPoul-Henning Kamp if (error) 1167552afd9cSPoul-Henning Kamp return (error); 116874f5d680SKonstantin Belousov 1169605da56bSAndriy Gapon control = NULL; 1170605da56bSAndriy Gapon cmsg = NULL; 1171605da56bSAndriy Gapon 1172605da56bSAndriy Gapon if ((ptr_cmsg = LINUX_CMSG_FIRSTHDR(&linux_msg)) != NULL) { 1173605da56bSAndriy Gapon error = kern_getsockname(td, args->s, &sa, &datalen); 1174605da56bSAndriy Gapon if (error) 1175605da56bSAndriy Gapon goto bad; 1176605da56bSAndriy Gapon sa_family = sa->sa_family; 1177605da56bSAndriy Gapon free(sa, M_SONAME); 1178605da56bSAndriy Gapon 117974f5d680SKonstantin Belousov error = ENOBUFS; 118074f5d680SKonstantin Belousov cmsg = malloc(CMSG_HDRSZ, M_TEMP, M_WAITOK | M_ZERO); 118174f5d680SKonstantin Belousov control = m_get(M_WAIT, MT_CONTROL); 118274f5d680SKonstantin Belousov if (control == NULL) 118374f5d680SKonstantin Belousov goto bad; 118474f5d680SKonstantin Belousov 118574f5d680SKonstantin Belousov do { 118674f5d680SKonstantin Belousov error = copyin(ptr_cmsg, &linux_cmsg, 118774f5d680SKonstantin Belousov sizeof(struct l_cmsghdr)); 118874f5d680SKonstantin Belousov if (error) 118974f5d680SKonstantin Belousov goto bad; 119074f5d680SKonstantin Belousov 119174f5d680SKonstantin Belousov error = EINVAL; 119274f5d680SKonstantin Belousov if (linux_cmsg.cmsg_len < sizeof(struct l_cmsghdr)) 119374f5d680SKonstantin Belousov goto bad; 119474f5d680SKonstantin Belousov 119574f5d680SKonstantin Belousov /* 1196605da56bSAndriy Gapon * Now we support only SCM_RIGHTS and SCM_CRED, 1197605da56bSAndriy Gapon * so return EINVAL in any other cmsg_type 119874f5d680SKonstantin Belousov */ 1199605da56bSAndriy Gapon cmsg->cmsg_type = 1200605da56bSAndriy Gapon linux_to_bsd_cmsg_type(linux_cmsg.cmsg_type); 120174f5d680SKonstantin Belousov cmsg->cmsg_level = 120274f5d680SKonstantin Belousov linux_to_bsd_sockopt_level(linux_cmsg.cmsg_level); 1203605da56bSAndriy Gapon if (cmsg->cmsg_type == -1 1204605da56bSAndriy Gapon || cmsg->cmsg_level != SOL_SOCKET) 1205605da56bSAndriy Gapon goto bad; 120674f5d680SKonstantin Belousov 1207605da56bSAndriy Gapon /* 1208605da56bSAndriy Gapon * Some applications (e.g. pulseaudio) attempt to 1209605da56bSAndriy Gapon * send ancillary data even if the underlying protocol 1210605da56bSAndriy Gapon * doesn't support it which is not allowed in the 1211605da56bSAndriy Gapon * FreeBSD system call interface. 1212605da56bSAndriy Gapon */ 1213605da56bSAndriy Gapon if (sa_family != AF_UNIX) 1214605da56bSAndriy Gapon continue; 1215605da56bSAndriy Gapon 121674f5d680SKonstantin Belousov data = LINUX_CMSG_DATA(ptr_cmsg); 1217605da56bSAndriy Gapon datalen = linux_cmsg.cmsg_len - L_CMSG_HDRSZ; 1218605da56bSAndriy Gapon 1219605da56bSAndriy Gapon switch (cmsg->cmsg_type) 1220605da56bSAndriy Gapon { 1221605da56bSAndriy Gapon case SCM_RIGHTS: 1222605da56bSAndriy Gapon break; 1223605da56bSAndriy Gapon 1224605da56bSAndriy Gapon case SCM_CREDS: 1225605da56bSAndriy Gapon data = &cmcred; 1226605da56bSAndriy Gapon datalen = sizeof(cmcred); 1227605da56bSAndriy Gapon 1228605da56bSAndriy Gapon /* 1229605da56bSAndriy Gapon * The lower levels will fill in the structure 1230605da56bSAndriy Gapon */ 1231605da56bSAndriy Gapon bzero(data, datalen); 1232605da56bSAndriy Gapon break; 1233605da56bSAndriy Gapon } 1234605da56bSAndriy Gapon 1235605da56bSAndriy Gapon cmsg->cmsg_len = CMSG_LEN(datalen); 123674f5d680SKonstantin Belousov 123774f5d680SKonstantin Belousov error = ENOBUFS; 123874f5d680SKonstantin Belousov if (!m_append(control, CMSG_HDRSZ, (c_caddr_t) cmsg)) 123974f5d680SKonstantin Belousov goto bad; 124074f5d680SKonstantin Belousov if (!m_append(control, datalen, (c_caddr_t) data)) 124174f5d680SKonstantin Belousov goto bad; 1242605da56bSAndriy Gapon } while ((ptr_cmsg = LINUX_CMSG_NXTHDR(&linux_msg, ptr_cmsg))); 1243605da56bSAndriy Gapon 1244605da56bSAndriy Gapon if (m_length(control, NULL) == 0) { 1245605da56bSAndriy Gapon m_freem(control); 124674f5d680SKonstantin Belousov control = NULL; 1247605da56bSAndriy Gapon } 124874f5d680SKonstantin Belousov } 124974f5d680SKonstantin Belousov 12505a8a13e0SDavid Malone msg.msg_iov = iov; 12515a8a13e0SDavid Malone msg.msg_flags = 0; 125274f5d680SKonstantin Belousov error = linux_sendit(td, args->s, &msg, args->flags, control, 125374f5d680SKonstantin Belousov UIO_USERSPACE); 125474f5d680SKonstantin Belousov 125574f5d680SKonstantin Belousov bad: 1256552afd9cSPoul-Henning Kamp free(iov, M_IOV); 125774f5d680SKonstantin Belousov if (cmsg) 125874f5d680SKonstantin Belousov free(cmsg, M_TEMP); 1259ca26842eSHajimu UMEMOTO return (error); 1260c21dee17SSøren Schmidt } 1261c21dee17SSøren Schmidt 126201e0ffbaSAlexander Leidinger struct linux_recvmsg_args { 126340dbba57SAssar Westerlund int s; 12644af27623STim J. Robbins l_uintptr_t msg; 126540dbba57SAssar Westerlund int flags; 126640dbba57SAssar Westerlund }; 126740dbba57SAssar Westerlund 126801e0ffbaSAlexander Leidinger static int 1269b40ce416SJulian Elischer linux_recvmsg(struct thread *td, struct linux_recvmsg_args *args) 127040dbba57SAssar Westerlund { 127174f5d680SKonstantin Belousov struct cmsghdr *cm; 1272605da56bSAndriy Gapon struct cmsgcred *cmcred; 1273ca26842eSHajimu UMEMOTO struct msghdr msg; 127474f5d680SKonstantin Belousov struct l_cmsghdr *linux_cmsg = NULL; 1275605da56bSAndriy Gapon struct l_ucred linux_ucred; 1276605da56bSAndriy Gapon socklen_t datalen, outlen; 127774f5d680SKonstantin Belousov struct l_msghdr linux_msg; 127874f5d680SKonstantin Belousov struct iovec *iov, *uiov; 127974f5d680SKonstantin Belousov struct mbuf *control = NULL; 128074f5d680SKonstantin Belousov struct mbuf **controlp; 128174f5d680SKonstantin Belousov caddr_t outbuf; 128274f5d680SKonstantin Belousov void *data; 12833a72bf04SDmitry Chagin int error, i, fd, fds, *fdp; 128440dbba57SAssar Westerlund 128574f5d680SKonstantin Belousov error = copyin(PTRIN(args->msg), &linux_msg, sizeof(linux_msg)); 1286ca26842eSHajimu UMEMOTO if (error) 1287ca26842eSHajimu UMEMOTO return (error); 1288ca26842eSHajimu UMEMOTO 128974f5d680SKonstantin Belousov error = linux_to_bsd_msghdr(&msg, &linux_msg); 129074f5d680SKonstantin Belousov if (error) 129174f5d680SKonstantin Belousov return (error); 129274f5d680SKonstantin Belousov 129374f5d680SKonstantin Belousov #ifdef COMPAT_LINUX32 129474f5d680SKonstantin Belousov error = linux32_copyiniov(PTRIN(msg.msg_iov), msg.msg_iovlen, 129574f5d680SKonstantin Belousov &iov, EMSGSIZE); 129674f5d680SKonstantin Belousov #else 129774f5d680SKonstantin Belousov error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE); 129874f5d680SKonstantin Belousov #endif 129974f5d680SKonstantin Belousov if (error) 130074f5d680SKonstantin Belousov return (error); 130174f5d680SKonstantin Belousov 130274f5d680SKonstantin Belousov if (msg.msg_name) { 130374f5d680SKonstantin Belousov error = linux_to_bsd_sockaddr((struct sockaddr *)msg.msg_name, 130474f5d680SKonstantin Belousov msg.msg_namelen); 130574f5d680SKonstantin Belousov if (error) 130674f5d680SKonstantin Belousov goto bad; 130784b11cd8SMitsuru IWASAKI } 130884b11cd8SMitsuru IWASAKI 130974f5d680SKonstantin Belousov uiov = msg.msg_iov; 131074f5d680SKonstantin Belousov msg.msg_iov = iov; 131174f5d680SKonstantin Belousov controlp = (msg.msg_control != NULL) ? &control : NULL; 131274f5d680SKonstantin Belousov error = kern_recvit(td, args->s, &msg, UIO_USERSPACE, controlp); 131374f5d680SKonstantin Belousov msg.msg_iov = uiov; 1314ca26842eSHajimu UMEMOTO if (error) 131574f5d680SKonstantin Belousov goto bad; 131674f5d680SKonstantin Belousov 131774f5d680SKonstantin Belousov error = bsd_to_linux_msghdr(&msg, &linux_msg); 131874f5d680SKonstantin Belousov if (error) 131974f5d680SKonstantin Belousov goto bad; 132074f5d680SKonstantin Belousov 132174f5d680SKonstantin Belousov if (linux_msg.msg_name) { 132274f5d680SKonstantin Belousov error = bsd_to_linux_sockaddr((struct sockaddr *) 132374f5d680SKonstantin Belousov PTRIN(linux_msg.msg_name)); 132474f5d680SKonstantin Belousov if (error) 132574f5d680SKonstantin Belousov goto bad; 132674f5d680SKonstantin Belousov } 132774f5d680SKonstantin Belousov if (linux_msg.msg_name && linux_msg.msg_namelen > 2) { 132874f5d680SKonstantin Belousov error = linux_sa_put(PTRIN(linux_msg.msg_name)); 132974f5d680SKonstantin Belousov if (error) 133074f5d680SKonstantin Belousov goto bad; 133174f5d680SKonstantin Belousov } 133274f5d680SKonstantin Belousov 133374f5d680SKonstantin Belousov outbuf = PTRIN(linux_msg.msg_control); 133474f5d680SKonstantin Belousov outlen = 0; 1335605da56bSAndriy Gapon 1336605da56bSAndriy Gapon if (control) { 1337605da56bSAndriy Gapon linux_cmsg = malloc(L_CMSG_HDRSZ, M_TEMP, M_WAITOK | M_ZERO); 1338605da56bSAndriy Gapon 1339605da56bSAndriy Gapon msg.msg_control = mtod(control, struct cmsghdr *); 1340605da56bSAndriy Gapon msg.msg_controllen = control->m_len; 1341605da56bSAndriy Gapon 1342605da56bSAndriy Gapon cm = CMSG_FIRSTHDR(&msg); 134374f5d680SKonstantin Belousov 134474f5d680SKonstantin Belousov while (cm != NULL) { 1345605da56bSAndriy Gapon linux_cmsg->cmsg_type = 1346605da56bSAndriy Gapon bsd_to_linux_cmsg_type(cm->cmsg_type); 1347605da56bSAndriy Gapon linux_cmsg->cmsg_level = 1348605da56bSAndriy Gapon bsd_to_linux_sockopt_level(cm->cmsg_level); 1349605da56bSAndriy Gapon if (linux_cmsg->cmsg_type == -1 1350605da56bSAndriy Gapon || cm->cmsg_level != SOL_SOCKET) 135174f5d680SKonstantin Belousov { 135274f5d680SKonstantin Belousov error = EINVAL; 135374f5d680SKonstantin Belousov goto bad; 135474f5d680SKonstantin Belousov } 1355605da56bSAndriy Gapon 135674f5d680SKonstantin Belousov data = CMSG_DATA(cm); 135774f5d680SKonstantin Belousov datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data; 135874f5d680SKonstantin Belousov 1359605da56bSAndriy Gapon switch (cm->cmsg_type) 13603a72bf04SDmitry Chagin { 1361605da56bSAndriy Gapon case SCM_RIGHTS: 1362605da56bSAndriy Gapon if (args->flags & LINUX_MSG_CMSG_CLOEXEC) { 1363605da56bSAndriy Gapon fds = datalen / sizeof(int); 1364605da56bSAndriy Gapon fdp = data; 1365605da56bSAndriy Gapon for (i = 0; i < fds; i++) { 1366605da56bSAndriy Gapon fd = *fdp++; 1367605da56bSAndriy Gapon (void)kern_fcntl(td, fd, 1368605da56bSAndriy Gapon F_SETFD, FD_CLOEXEC); 1369605da56bSAndriy Gapon } 1370605da56bSAndriy Gapon } 1371605da56bSAndriy Gapon break; 1372605da56bSAndriy Gapon 1373605da56bSAndriy Gapon case SCM_CREDS: 1374605da56bSAndriy Gapon /* 1375605da56bSAndriy Gapon * Currently LOCAL_CREDS is never in 1376605da56bSAndriy Gapon * effect for Linux so no need to worry 1377605da56bSAndriy Gapon * about sockcred 1378605da56bSAndriy Gapon */ 1379605da56bSAndriy Gapon if (datalen != sizeof (*cmcred)) { 1380605da56bSAndriy Gapon error = EMSGSIZE; 1381605da56bSAndriy Gapon goto bad; 1382605da56bSAndriy Gapon } 1383605da56bSAndriy Gapon cmcred = (struct cmsgcred *)data; 1384605da56bSAndriy Gapon bzero(&linux_ucred, sizeof(linux_ucred)); 1385605da56bSAndriy Gapon linux_ucred.pid = cmcred->cmcred_pid; 1386605da56bSAndriy Gapon linux_ucred.uid = cmcred->cmcred_uid; 1387605da56bSAndriy Gapon linux_ucred.gid = cmcred->cmcred_gid; 1388605da56bSAndriy Gapon data = &linux_ucred; 1389605da56bSAndriy Gapon datalen = sizeof(linux_ucred); 1390605da56bSAndriy Gapon break; 1391605da56bSAndriy Gapon } 1392605da56bSAndriy Gapon 139374f5d680SKonstantin Belousov if (outlen + LINUX_CMSG_LEN(datalen) > 139474f5d680SKonstantin Belousov linux_msg.msg_controllen) { 139574f5d680SKonstantin Belousov if (outlen == 0) { 139674f5d680SKonstantin Belousov error = EMSGSIZE; 139774f5d680SKonstantin Belousov goto bad; 139874f5d680SKonstantin Belousov } else { 13993a72bf04SDmitry Chagin linux_msg.msg_flags |= 14003a72bf04SDmitry Chagin LINUX_MSG_CTRUNC; 140174f5d680SKonstantin Belousov goto out; 140274f5d680SKonstantin Belousov } 140374f5d680SKonstantin Belousov } 140474f5d680SKonstantin Belousov 140574f5d680SKonstantin Belousov linux_cmsg->cmsg_len = LINUX_CMSG_LEN(datalen); 140674f5d680SKonstantin Belousov 140774f5d680SKonstantin Belousov error = copyout(linux_cmsg, outbuf, L_CMSG_HDRSZ); 140874f5d680SKonstantin Belousov if (error) 140974f5d680SKonstantin Belousov goto bad; 141074f5d680SKonstantin Belousov outbuf += L_CMSG_HDRSZ; 141174f5d680SKonstantin Belousov 141274f5d680SKonstantin Belousov error = copyout(data, outbuf, datalen); 141374f5d680SKonstantin Belousov if (error) 141474f5d680SKonstantin Belousov goto bad; 141574f5d680SKonstantin Belousov 141674f5d680SKonstantin Belousov outbuf += LINUX_CMSG_ALIGN(datalen); 141774f5d680SKonstantin Belousov outlen += LINUX_CMSG_LEN(datalen); 141874f5d680SKonstantin Belousov 1419605da56bSAndriy Gapon cm = CMSG_NXTHDR(&msg, cm); 142074f5d680SKonstantin Belousov } 142174f5d680SKonstantin Belousov } 142274f5d680SKonstantin Belousov 142374f5d680SKonstantin Belousov out: 1424605da56bSAndriy Gapon linux_msg.msg_controllen = outlen; 142574f5d680SKonstantin Belousov error = copyout(&linux_msg, PTRIN(args->msg), sizeof(linux_msg)); 142674f5d680SKonstantin Belousov 142774f5d680SKonstantin Belousov bad: 142874f5d680SKonstantin Belousov free(iov, M_IOV); 142974f5d680SKonstantin Belousov if (control != NULL) 143074f5d680SKonstantin Belousov m_freem(control); 143174f5d680SKonstantin Belousov if (linux_cmsg != NULL) 143274f5d680SKonstantin Belousov free(linux_cmsg, M_TEMP); 143374f5d680SKonstantin Belousov 1434ca26842eSHajimu UMEMOTO return (error); 143540dbba57SAssar Westerlund } 143640dbba57SAssar Westerlund 1437c21dee17SSøren Schmidt struct linux_shutdown_args { 1438c21dee17SSøren Schmidt int s; 1439c21dee17SSøren Schmidt int how; 1440c21dee17SSøren Schmidt }; 1441c21dee17SSøren Schmidt 1442c21dee17SSøren Schmidt static int 1443b40ce416SJulian Elischer linux_shutdown(struct thread *td, struct linux_shutdown_args *args) 1444c21dee17SSøren Schmidt { 1445ef04503dSPeter Wemm struct shutdown_args /* { 1446c21dee17SSøren Schmidt int s; 1447c21dee17SSøren Schmidt int how; 1448ef04503dSPeter Wemm } */ bsd_args; 1449c21dee17SSøren Schmidt 1450745aaef5SKonstantin Belousov bsd_args.s = args->s; 1451745aaef5SKonstantin Belousov bsd_args.how = args->how; 14528451d0ddSKip Macy return (sys_shutdown(td, &bsd_args)); 1453c21dee17SSøren Schmidt } 1454c21dee17SSøren Schmidt 1455c21dee17SSøren Schmidt struct linux_setsockopt_args { 1456c21dee17SSøren Schmidt int s; 1457c21dee17SSøren Schmidt int level; 1458c21dee17SSøren Schmidt int optname; 14594af27623STim J. Robbins l_uintptr_t optval; 1460c21dee17SSøren Schmidt int optlen; 1461c21dee17SSøren Schmidt }; 1462c21dee17SSøren Schmidt 1463c21dee17SSøren Schmidt static int 1464b40ce416SJulian Elischer linux_setsockopt(struct thread *td, struct linux_setsockopt_args *args) 1465c21dee17SSøren Schmidt { 1466ef04503dSPeter Wemm struct setsockopt_args /* { 1467c21dee17SSøren Schmidt int s; 1468c21dee17SSøren Schmidt int level; 1469c21dee17SSøren Schmidt int name; 1470c21dee17SSøren Schmidt caddr_t val; 1471c21dee17SSøren Schmidt int valsize; 1472ef04503dSPeter Wemm } */ bsd_args; 147303cc95d2SDmitry Chagin l_timeval linux_tv; 147403cc95d2SDmitry Chagin struct timeval tv; 1475c21dee17SSøren Schmidt int error, name; 1476c21dee17SSøren Schmidt 1477745aaef5SKonstantin Belousov bsd_args.s = args->s; 1478745aaef5SKonstantin Belousov bsd_args.level = linux_to_bsd_sockopt_level(args->level); 1479c21dee17SSøren Schmidt switch (bsd_args.level) { 1480c21dee17SSøren Schmidt case SOL_SOCKET: 1481745aaef5SKonstantin Belousov name = linux_to_bsd_so_sockopt(args->optname); 148203cc95d2SDmitry Chagin switch (name) { 148303cc95d2SDmitry Chagin case SO_RCVTIMEO: 148403cc95d2SDmitry Chagin /* FALLTHROUGH */ 148503cc95d2SDmitry Chagin case SO_SNDTIMEO: 148603cc95d2SDmitry Chagin error = copyin(PTRIN(args->optval), &linux_tv, 148703cc95d2SDmitry Chagin sizeof(linux_tv)); 148803cc95d2SDmitry Chagin if (error) 148903cc95d2SDmitry Chagin return (error); 149003cc95d2SDmitry Chagin tv.tv_sec = linux_tv.tv_sec; 149103cc95d2SDmitry Chagin tv.tv_usec = linux_tv.tv_usec; 149203cc95d2SDmitry Chagin return (kern_setsockopt(td, args->s, bsd_args.level, 149303cc95d2SDmitry Chagin name, &tv, UIO_SYSSPACE, sizeof(tv))); 149403cc95d2SDmitry Chagin /* NOTREACHED */ 149503cc95d2SDmitry Chagin break; 149603cc95d2SDmitry Chagin default: 149703cc95d2SDmitry Chagin break; 149803cc95d2SDmitry Chagin } 1499c21dee17SSøren Schmidt break; 1500c21dee17SSøren Schmidt case IPPROTO_IP: 1501745aaef5SKonstantin Belousov name = linux_to_bsd_ip_sockopt(args->optname); 1502c21dee17SSøren Schmidt break; 1503dad3b88aSMike Smith case IPPROTO_TCP: 1504dad3b88aSMike Smith /* Linux TCP option values match BSD's */ 1505745aaef5SKonstantin Belousov name = args->optname; 1506dad3b88aSMike Smith break; 1507c21dee17SSøren Schmidt default: 15083f3a4815SMarcel Moolenaar name = -1; 15093f3a4815SMarcel Moolenaar break; 1510c21dee17SSøren Schmidt } 1511c21dee17SSøren Schmidt if (name == -1) 1512d4b7423fSAlexander Leidinger return (ENOPROTOOPT); 15133f3a4815SMarcel Moolenaar 1514c21dee17SSøren Schmidt bsd_args.name = name; 1515745aaef5SKonstantin Belousov bsd_args.val = PTRIN(args->optval); 1516745aaef5SKonstantin Belousov bsd_args.valsize = args->optlen; 15175c8919adSAlexander Leidinger 15185c8919adSAlexander Leidinger if (name == IPV6_NEXTHOP) { 15195c8919adSAlexander Leidinger linux_to_bsd_sockaddr((struct sockaddr *)bsd_args.val, 15205c8919adSAlexander Leidinger bsd_args.valsize); 15218451d0ddSKip Macy error = sys_setsockopt(td, &bsd_args); 15225c8919adSAlexander Leidinger bsd_to_linux_sockaddr((struct sockaddr *)bsd_args.val); 15235c8919adSAlexander Leidinger } else 15248451d0ddSKip Macy error = sys_setsockopt(td, &bsd_args); 15255c8919adSAlexander Leidinger 15265c8919adSAlexander Leidinger return (error); 1527c21dee17SSøren Schmidt } 1528c21dee17SSøren Schmidt 1529c21dee17SSøren Schmidt struct linux_getsockopt_args { 1530c21dee17SSøren Schmidt int s; 1531c21dee17SSøren Schmidt int level; 1532c21dee17SSøren Schmidt int optname; 15334af27623STim J. Robbins l_uintptr_t optval; 15344af27623STim J. Robbins l_uintptr_t optlen; 1535c21dee17SSøren Schmidt }; 1536c21dee17SSøren Schmidt 1537c21dee17SSøren Schmidt static int 1538b40ce416SJulian Elischer linux_getsockopt(struct thread *td, struct linux_getsockopt_args *args) 1539c21dee17SSøren Schmidt { 1540ef04503dSPeter Wemm struct getsockopt_args /* { 1541c21dee17SSøren Schmidt int s; 1542c21dee17SSøren Schmidt int level; 1543c21dee17SSøren Schmidt int name; 1544c21dee17SSøren Schmidt caddr_t val; 1545c21dee17SSøren Schmidt int *avalsize; 1546ef04503dSPeter Wemm } */ bsd_args; 154703cc95d2SDmitry Chagin l_timeval linux_tv; 154803cc95d2SDmitry Chagin struct timeval tv; 1549d4dd69c4SDmitry Chagin socklen_t tv_len, xulen; 1550d4dd69c4SDmitry Chagin struct xucred xu; 1551d4dd69c4SDmitry Chagin struct l_ucred lxu; 1552c21dee17SSøren Schmidt int error, name; 1553c21dee17SSøren Schmidt 1554745aaef5SKonstantin Belousov bsd_args.s = args->s; 1555745aaef5SKonstantin Belousov bsd_args.level = linux_to_bsd_sockopt_level(args->level); 1556c21dee17SSøren Schmidt switch (bsd_args.level) { 1557c21dee17SSøren Schmidt case SOL_SOCKET: 1558745aaef5SKonstantin Belousov name = linux_to_bsd_so_sockopt(args->optname); 155903cc95d2SDmitry Chagin switch (name) { 156003cc95d2SDmitry Chagin case SO_RCVTIMEO: 156103cc95d2SDmitry Chagin /* FALLTHROUGH */ 156203cc95d2SDmitry Chagin case SO_SNDTIMEO: 156303cc95d2SDmitry Chagin tv_len = sizeof(tv); 156403cc95d2SDmitry Chagin error = kern_getsockopt(td, args->s, bsd_args.level, 156503cc95d2SDmitry Chagin name, &tv, UIO_SYSSPACE, &tv_len); 156603cc95d2SDmitry Chagin if (error) 156703cc95d2SDmitry Chagin return (error); 156803cc95d2SDmitry Chagin linux_tv.tv_sec = tv.tv_sec; 156903cc95d2SDmitry Chagin linux_tv.tv_usec = tv.tv_usec; 157003cc95d2SDmitry Chagin return (copyout(&linux_tv, PTRIN(args->optval), 157103cc95d2SDmitry Chagin sizeof(linux_tv))); 157203cc95d2SDmitry Chagin /* NOTREACHED */ 157303cc95d2SDmitry Chagin break; 1574d4dd69c4SDmitry Chagin case LOCAL_PEERCRED: 1575d4dd69c4SDmitry Chagin if (args->optlen != sizeof(lxu)) 1576d4dd69c4SDmitry Chagin return (EINVAL); 1577d4dd69c4SDmitry Chagin xulen = sizeof(xu); 1578d4dd69c4SDmitry Chagin error = kern_getsockopt(td, args->s, bsd_args.level, 1579d4dd69c4SDmitry Chagin name, &xu, UIO_SYSSPACE, &xulen); 1580d4dd69c4SDmitry Chagin if (error) 1581d4dd69c4SDmitry Chagin return (error); 1582d4dd69c4SDmitry Chagin /* 1583d4dd69c4SDmitry Chagin * XXX Use 0 for pid as the FreeBSD does not cache peer pid. 1584d4dd69c4SDmitry Chagin */ 1585d4dd69c4SDmitry Chagin lxu.pid = 0; 1586d4dd69c4SDmitry Chagin lxu.uid = xu.cr_uid; 1587d4dd69c4SDmitry Chagin lxu.gid = xu.cr_gid; 1588d4dd69c4SDmitry Chagin return (copyout(&lxu, PTRIN(args->optval), sizeof(lxu))); 1589d4dd69c4SDmitry Chagin /* NOTREACHED */ 1590d4dd69c4SDmitry Chagin break; 159103cc95d2SDmitry Chagin default: 159203cc95d2SDmitry Chagin break; 159303cc95d2SDmitry Chagin } 1594c21dee17SSøren Schmidt break; 1595c21dee17SSøren Schmidt case IPPROTO_IP: 1596745aaef5SKonstantin Belousov name = linux_to_bsd_ip_sockopt(args->optname); 1597c21dee17SSøren Schmidt break; 1598dad3b88aSMike Smith case IPPROTO_TCP: 1599dad3b88aSMike Smith /* Linux TCP option values match BSD's */ 1600745aaef5SKonstantin Belousov name = args->optname; 1601dad3b88aSMike Smith break; 1602c21dee17SSøren Schmidt default: 16033f3a4815SMarcel Moolenaar name = -1; 16043f3a4815SMarcel Moolenaar break; 1605c21dee17SSøren Schmidt } 1606c21dee17SSøren Schmidt if (name == -1) 16073f3a4815SMarcel Moolenaar return (EINVAL); 16083f3a4815SMarcel Moolenaar 1609f2477ae1SMike Smith bsd_args.name = name; 1610745aaef5SKonstantin Belousov bsd_args.val = PTRIN(args->optval); 1611745aaef5SKonstantin Belousov bsd_args.avalsize = PTRIN(args->optlen); 16125c8919adSAlexander Leidinger 16135c8919adSAlexander Leidinger if (name == IPV6_NEXTHOP) { 16148451d0ddSKip Macy error = sys_getsockopt(td, &bsd_args); 16155c8919adSAlexander Leidinger bsd_to_linux_sockaddr((struct sockaddr *)bsd_args.val); 16165c8919adSAlexander Leidinger } else 16178451d0ddSKip Macy error = sys_getsockopt(td, &bsd_args); 16185c8919adSAlexander Leidinger 16195c8919adSAlexander Leidinger return (error); 1620c21dee17SSøren Schmidt } 1621c21dee17SSøren Schmidt 1622ea7b81d2SDmitry Chagin /* Argument list sizes for linux_socketcall */ 1623ea7b81d2SDmitry Chagin 1624ea7b81d2SDmitry Chagin #define LINUX_AL(x) ((x) * sizeof(l_ulong)) 1625ea7b81d2SDmitry Chagin 1626ea7b81d2SDmitry Chagin static const unsigned char lxs_args[] = { 1627ea7b81d2SDmitry Chagin LINUX_AL(0) /* unused*/, LINUX_AL(3) /* socket */, 1628ea7b81d2SDmitry Chagin LINUX_AL(3) /* bind */, LINUX_AL(3) /* connect */, 1629ea7b81d2SDmitry Chagin LINUX_AL(2) /* listen */, LINUX_AL(3) /* accept */, 1630ea7b81d2SDmitry Chagin LINUX_AL(3) /* getsockname */, LINUX_AL(3) /* getpeername */, 1631ea7b81d2SDmitry Chagin LINUX_AL(4) /* socketpair */, LINUX_AL(4) /* send */, 1632ea7b81d2SDmitry Chagin LINUX_AL(4) /* recv */, LINUX_AL(6) /* sendto */, 1633ea7b81d2SDmitry Chagin LINUX_AL(6) /* recvfrom */, LINUX_AL(2) /* shutdown */, 1634ea7b81d2SDmitry Chagin LINUX_AL(5) /* setsockopt */, LINUX_AL(5) /* getsockopt */, 1635f8cd0af2SDmitry Chagin LINUX_AL(3) /* sendmsg */, LINUX_AL(3) /* recvmsg */, 1636f8cd0af2SDmitry Chagin LINUX_AL(4) /* accept4 */ 1637ea7b81d2SDmitry Chagin }; 1638ea7b81d2SDmitry Chagin 1639ea7b81d2SDmitry Chagin #define LINUX_AL_SIZE sizeof(lxs_args) / sizeof(lxs_args[0]) - 1 1640ea7b81d2SDmitry Chagin 1641c21dee17SSøren Schmidt int 1642b40ce416SJulian Elischer linux_socketcall(struct thread *td, struct linux_socketcall_args *args) 1643c21dee17SSøren Schmidt { 1644ea7b81d2SDmitry Chagin l_ulong a[6]; 1645ea7b81d2SDmitry Chagin void *arg; 1646ea7b81d2SDmitry Chagin int error; 16473f3a4815SMarcel Moolenaar 1648ea7b81d2SDmitry Chagin if (args->what < LINUX_SOCKET || args->what > LINUX_AL_SIZE) 1649ea7b81d2SDmitry Chagin return (EINVAL); 1650ea7b81d2SDmitry Chagin error = copyin(PTRIN(args->args), a, lxs_args[args->what]); 1651ea7b81d2SDmitry Chagin if (error) 1652ea7b81d2SDmitry Chagin return (error); 1653ea7b81d2SDmitry Chagin 1654ea7b81d2SDmitry Chagin arg = a; 1655c21dee17SSøren Schmidt switch (args->what) { 1656c21dee17SSøren Schmidt case LINUX_SOCKET: 1657b40ce416SJulian Elischer return (linux_socket(td, arg)); 1658c21dee17SSøren Schmidt case LINUX_BIND: 1659b40ce416SJulian Elischer return (linux_bind(td, arg)); 1660c21dee17SSøren Schmidt case LINUX_CONNECT: 1661b40ce416SJulian Elischer return (linux_connect(td, arg)); 1662c21dee17SSøren Schmidt case LINUX_LISTEN: 1663b40ce416SJulian Elischer return (linux_listen(td, arg)); 1664c21dee17SSøren Schmidt case LINUX_ACCEPT: 1665b40ce416SJulian Elischer return (linux_accept(td, arg)); 1666c21dee17SSøren Schmidt case LINUX_GETSOCKNAME: 1667b40ce416SJulian Elischer return (linux_getsockname(td, arg)); 1668c21dee17SSøren Schmidt case LINUX_GETPEERNAME: 1669b40ce416SJulian Elischer return (linux_getpeername(td, arg)); 1670c21dee17SSøren Schmidt case LINUX_SOCKETPAIR: 1671b40ce416SJulian Elischer return (linux_socketpair(td, arg)); 1672c21dee17SSøren Schmidt case LINUX_SEND: 1673b40ce416SJulian Elischer return (linux_send(td, arg)); 1674c21dee17SSøren Schmidt case LINUX_RECV: 1675b40ce416SJulian Elischer return (linux_recv(td, arg)); 1676c21dee17SSøren Schmidt case LINUX_SENDTO: 1677b40ce416SJulian Elischer return (linux_sendto(td, arg)); 1678c21dee17SSøren Schmidt case LINUX_RECVFROM: 1679b40ce416SJulian Elischer return (linux_recvfrom(td, arg)); 1680c21dee17SSøren Schmidt case LINUX_SHUTDOWN: 1681b40ce416SJulian Elischer return (linux_shutdown(td, arg)); 1682c21dee17SSøren Schmidt case LINUX_SETSOCKOPT: 1683b40ce416SJulian Elischer return (linux_setsockopt(td, arg)); 1684c21dee17SSøren Schmidt case LINUX_GETSOCKOPT: 1685b40ce416SJulian Elischer return (linux_getsockopt(td, arg)); 1686e76bba09SSøren Schmidt case LINUX_SENDMSG: 1687ca26842eSHajimu UMEMOTO return (linux_sendmsg(td, arg)); 1688e76bba09SSøren Schmidt case LINUX_RECVMSG: 1689b40ce416SJulian Elischer return (linux_recvmsg(td, arg)); 1690f8cd0af2SDmitry Chagin case LINUX_ACCEPT4: 1691f8cd0af2SDmitry Chagin return (linux_accept4(td, arg)); 1692c21dee17SSøren Schmidt } 16933f3a4815SMarcel Moolenaar 16943f3a4815SMarcel Moolenaar uprintf("LINUX: 'socket' typ=%d not implemented\n", args->what); 16953f3a4815SMarcel Moolenaar return (ENOSYS); 1696c21dee17SSøren Schmidt } 1697