xref: /freebsd/sys/compat/linux/linux_socket.c (revision b6f96462ba817862b0b4a07284389b544e1271cd)
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 linux_to_bsd_domain(int);
76ca26842eSHajimu UMEMOTO 
774730796cSBill Fenner /*
78ca26842eSHajimu UMEMOTO  * Reads a linux sockaddr and does any necessary translation.
79ca26842eSHajimu UMEMOTO  * Linux sockaddrs don't have a length field, only a family.
80ca26842eSHajimu UMEMOTO  * Copy the osockaddr structure pointed to by osa to kernel, adjust
81ca26842eSHajimu UMEMOTO  * family and convert to sockaddr.
82ca26842eSHajimu UMEMOTO  */
83ca26842eSHajimu UMEMOTO static int
84*b6f96462SJung-uk Kim linux_getsockaddr(struct sockaddr **sap, const struct osockaddr *osa, int salen)
85ca26842eSHajimu UMEMOTO {
86ca26842eSHajimu UMEMOTO 	struct sockaddr *sa;
87ca26842eSHajimu UMEMOTO 	struct osockaddr *kosa;
88ca26842eSHajimu UMEMOTO #ifdef INET6
89ca26842eSHajimu UMEMOTO 	struct sockaddr_in6 *sin6;
903106d670SJung-uk Kim 	int oldv6size;
91ca26842eSHajimu UMEMOTO #endif
92c02637c7SJung-uk Kim 	char *name;
93*b6f96462SJung-uk Kim 	int bdom, error, hdrlen, namelen;
94ca26842eSHajimu UMEMOTO 
95*b6f96462SJung-uk Kim 	if (salen < 2 || salen > UCHAR_MAX || !osa)
96ca26842eSHajimu UMEMOTO 		return (EINVAL);
97ca26842eSHajimu UMEMOTO 
98ca26842eSHajimu UMEMOTO #ifdef INET6
99ca26842eSHajimu UMEMOTO 	oldv6size = 0;
100ca26842eSHajimu UMEMOTO 	/*
101ca26842eSHajimu UMEMOTO 	 * Check for old (pre-RFC2553) sockaddr_in6. We may accept it
102ca26842eSHajimu UMEMOTO 	 * if it's a v4-mapped address, so reserve the proper space
103ca26842eSHajimu UMEMOTO 	 * for it.
104ca26842eSHajimu UMEMOTO 	 */
105*b6f96462SJung-uk Kim 	if (salen == sizeof(struct sockaddr_in6) - sizeof(uint32_t)) {
106*b6f96462SJung-uk Kim 		salen += sizeof(uint32_t);
107ca26842eSHajimu UMEMOTO 		oldv6size = 1;
108ca26842eSHajimu UMEMOTO 	}
109ca26842eSHajimu UMEMOTO #endif
110ca26842eSHajimu UMEMOTO 
111*b6f96462SJung-uk Kim 	kosa = malloc(salen, M_SONAME, M_WAITOK);
112ca26842eSHajimu UMEMOTO 
113*b6f96462SJung-uk Kim 	if ((error = copyin(osa, kosa, salen)))
114ca26842eSHajimu UMEMOTO 		goto out;
115ca26842eSHajimu UMEMOTO 
116ca26842eSHajimu UMEMOTO 	bdom = linux_to_bsd_domain(kosa->sa_family);
117ca26842eSHajimu UMEMOTO 	if (bdom == -1) {
1185cb9c68cSXin LI 		error = EAFNOSUPPORT;
119ca26842eSHajimu UMEMOTO 		goto out;
120ca26842eSHajimu UMEMOTO 	}
121ca26842eSHajimu UMEMOTO 
122ca26842eSHajimu UMEMOTO #ifdef INET6
123ca26842eSHajimu UMEMOTO 	/*
124ca26842eSHajimu UMEMOTO 	 * Older Linux IPv6 code uses obsolete RFC2133 struct sockaddr_in6,
125ca26842eSHajimu UMEMOTO 	 * which lacks the scope id compared with RFC2553 one. If we detect
126ca26842eSHajimu UMEMOTO 	 * the situation, reject the address and write a message to system log.
127ca26842eSHajimu UMEMOTO 	 *
128ca26842eSHajimu UMEMOTO 	 * Still accept addresses for which the scope id is not used.
129ca26842eSHajimu UMEMOTO 	 */
130f05531a3SJung-uk Kim 	if (oldv6size) {
131f05531a3SJung-uk Kim 		if (bdom == AF_INET6) {
132ca26842eSHajimu UMEMOTO 			sin6 = (struct sockaddr_in6 *)kosa;
133ca26842eSHajimu UMEMOTO 			if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr) ||
134ca26842eSHajimu UMEMOTO 			    (!IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) &&
135ca26842eSHajimu UMEMOTO 			     !IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr) &&
136ca26842eSHajimu UMEMOTO 			     !IN6_IS_ADDR_V4COMPAT(&sin6->sin6_addr) &&
137ca26842eSHajimu UMEMOTO 			     !IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) &&
138ca26842eSHajimu UMEMOTO 			     !IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))) {
139ca26842eSHajimu UMEMOTO 				sin6->sin6_scope_id = 0;
140ca26842eSHajimu UMEMOTO 			} else {
141ca26842eSHajimu UMEMOTO 				log(LOG_DEBUG,
1423c616032SGleb Smirnoff 				    "obsolete pre-RFC2553 sockaddr_in6 rejected\n");
143ca26842eSHajimu UMEMOTO 				error = EINVAL;
144ca26842eSHajimu UMEMOTO 				goto out;
145ca26842eSHajimu UMEMOTO 			}
146ca26842eSHajimu UMEMOTO 		} else
147*b6f96462SJung-uk Kim 			salen -= sizeof(uint32_t);
148f05531a3SJung-uk Kim 	}
149ca26842eSHajimu UMEMOTO #endif
1505cb9c68cSXin LI 	if (bdom == AF_INET) {
151*b6f96462SJung-uk Kim 		if (salen < sizeof(struct sockaddr_in)) {
1525cb9c68cSXin LI 			error = EINVAL;
1535cb9c68cSXin LI 			goto out;
1545cb9c68cSXin LI 		}
155*b6f96462SJung-uk Kim 		salen = sizeof(struct sockaddr_in);
1565cb9c68cSXin LI 	}
157ca26842eSHajimu UMEMOTO 
158*b6f96462SJung-uk Kim 	if (bdom == AF_LOCAL && salen > sizeof(struct sockaddr_un)) {
159c15cdbf2SJung-uk Kim 		hdrlen = offsetof(struct sockaddr_un, sun_path);
160c02637c7SJung-uk Kim 		name = ((struct sockaddr_un *)kosa)->sun_path;
161c02637c7SJung-uk Kim 		if (*name == '\0') {
162c02637c7SJung-uk Kim 			/*
163c02637c7SJung-uk Kim 		 	 * Linux abstract namespace starts with a NULL byte.
164c02637c7SJung-uk Kim 			 * XXX We do not support abstract namespace yet.
165c02637c7SJung-uk Kim 			 */
166*b6f96462SJung-uk Kim 			namelen = strnlen(name + 1, salen - hdrlen - 1) + 1;
167c02637c7SJung-uk Kim 		} else
168*b6f96462SJung-uk Kim 			namelen = strnlen(name, salen - hdrlen);
169c15cdbf2SJung-uk Kim 		if (hdrlen + namelen > sizeof(struct sockaddr_un)) {
17043399111SJung-uk Kim 			error = ENAMETOOLONG;
1715da3eb94SColin Percival 			goto out;
1725da3eb94SColin Percival 		}
173*b6f96462SJung-uk Kim 		salen = sizeof(struct sockaddr_un);
1745da3eb94SColin Percival 	}
1755da3eb94SColin Percival 
176ca26842eSHajimu UMEMOTO 	sa = (struct sockaddr *)kosa;
177ca26842eSHajimu UMEMOTO 	sa->sa_family = bdom;
178*b6f96462SJung-uk Kim 	sa->sa_len = salen;
179ca26842eSHajimu UMEMOTO 
180ca26842eSHajimu UMEMOTO 	*sap = sa;
181ca26842eSHajimu UMEMOTO 	return (0);
182ca26842eSHajimu UMEMOTO 
183ca26842eSHajimu UMEMOTO out:
1840007f669SJung-uk Kim 	free(kosa, M_SONAME);
185ca26842eSHajimu UMEMOTO 	return (error);
186ca26842eSHajimu UMEMOTO }
187ca26842eSHajimu UMEMOTO 
188c21dee17SSøren Schmidt static int
189c21dee17SSøren Schmidt linux_to_bsd_domain(int domain)
190c21dee17SSøren Schmidt {
1913f3a4815SMarcel Moolenaar 
192c21dee17SSøren Schmidt 	switch (domain) {
193c21dee17SSøren Schmidt 	case LINUX_AF_UNSPEC:
1943f3a4815SMarcel Moolenaar 		return (AF_UNSPEC);
195c21dee17SSøren Schmidt 	case LINUX_AF_UNIX:
1963f3a4815SMarcel Moolenaar 		return (AF_LOCAL);
197c21dee17SSøren Schmidt 	case LINUX_AF_INET:
1983f3a4815SMarcel Moolenaar 		return (AF_INET);
199ca26842eSHajimu UMEMOTO 	case LINUX_AF_INET6:
200ca26842eSHajimu UMEMOTO 		return (AF_INET6);
201c21dee17SSøren Schmidt 	case LINUX_AF_AX25:
2023f3a4815SMarcel Moolenaar 		return (AF_CCITT);
203c21dee17SSøren Schmidt 	case LINUX_AF_IPX:
2043f3a4815SMarcel Moolenaar 		return (AF_IPX);
205c21dee17SSøren Schmidt 	case LINUX_AF_APPLETALK:
2063f3a4815SMarcel Moolenaar 		return (AF_APPLETALK);
207c21dee17SSøren Schmidt 	}
2083f3a4815SMarcel Moolenaar 	return (-1);
209c21dee17SSøren Schmidt }
210c21dee17SSøren Schmidt 
211ca26842eSHajimu UMEMOTO static int
212ca26842eSHajimu UMEMOTO bsd_to_linux_domain(int domain)
213ca26842eSHajimu UMEMOTO {
214ca26842eSHajimu UMEMOTO 
215ca26842eSHajimu UMEMOTO 	switch (domain) {
216ca26842eSHajimu UMEMOTO 	case AF_UNSPEC:
217ca26842eSHajimu UMEMOTO 		return (LINUX_AF_UNSPEC);
218ca26842eSHajimu UMEMOTO 	case AF_LOCAL:
219ca26842eSHajimu UMEMOTO 		return (LINUX_AF_UNIX);
220ca26842eSHajimu UMEMOTO 	case AF_INET:
221ca26842eSHajimu UMEMOTO 		return (LINUX_AF_INET);
222ca26842eSHajimu UMEMOTO 	case AF_INET6:
223ca26842eSHajimu UMEMOTO 		return (LINUX_AF_INET6);
224ca26842eSHajimu UMEMOTO 	case AF_CCITT:
225ca26842eSHajimu UMEMOTO 		return (LINUX_AF_AX25);
226ca26842eSHajimu UMEMOTO 	case AF_IPX:
227ca26842eSHajimu UMEMOTO 		return (LINUX_AF_IPX);
228ca26842eSHajimu UMEMOTO 	case AF_APPLETALK:
229ca26842eSHajimu UMEMOTO 		return (LINUX_AF_APPLETALK);
230ca26842eSHajimu UMEMOTO 	}
231ca26842eSHajimu UMEMOTO 	return (-1);
232ca26842eSHajimu UMEMOTO }
233ca26842eSHajimu UMEMOTO 
234c21dee17SSøren Schmidt static int
235c21dee17SSøren Schmidt linux_to_bsd_sockopt_level(int level)
236c21dee17SSøren Schmidt {
2373f3a4815SMarcel Moolenaar 
238c21dee17SSøren Schmidt 	switch (level) {
239c21dee17SSøren Schmidt 	case LINUX_SOL_SOCKET:
2403f3a4815SMarcel Moolenaar 		return (SOL_SOCKET);
241c21dee17SSøren Schmidt 	}
2423f3a4815SMarcel Moolenaar 	return (level);
243c21dee17SSøren Schmidt }
244c21dee17SSøren Schmidt 
2453f3a4815SMarcel Moolenaar static int
24684b11cd8SMitsuru IWASAKI bsd_to_linux_sockopt_level(int level)
24784b11cd8SMitsuru IWASAKI {
24884b11cd8SMitsuru IWASAKI 
24984b11cd8SMitsuru IWASAKI 	switch (level) {
25084b11cd8SMitsuru IWASAKI 	case SOL_SOCKET:
25184b11cd8SMitsuru IWASAKI 		return (LINUX_SOL_SOCKET);
25284b11cd8SMitsuru IWASAKI 	}
25384b11cd8SMitsuru IWASAKI 	return (level);
25484b11cd8SMitsuru IWASAKI }
25584b11cd8SMitsuru IWASAKI 
25684b11cd8SMitsuru IWASAKI static int
2573f3a4815SMarcel Moolenaar linux_to_bsd_ip_sockopt(int opt)
258c21dee17SSøren Schmidt {
2593f3a4815SMarcel Moolenaar 
260c21dee17SSøren Schmidt 	switch (opt) {
261c21dee17SSøren Schmidt 	case LINUX_IP_TOS:
2623f3a4815SMarcel Moolenaar 		return (IP_TOS);
263c21dee17SSøren Schmidt 	case LINUX_IP_TTL:
2643f3a4815SMarcel Moolenaar 		return (IP_TTL);
26566ff6a3cSBill Fenner 	case LINUX_IP_OPTIONS:
2663f3a4815SMarcel Moolenaar 		return (IP_OPTIONS);
26766ff6a3cSBill Fenner 	case LINUX_IP_MULTICAST_IF:
2683f3a4815SMarcel Moolenaar 		return (IP_MULTICAST_IF);
26966ff6a3cSBill Fenner 	case LINUX_IP_MULTICAST_TTL:
2703f3a4815SMarcel Moolenaar 		return (IP_MULTICAST_TTL);
27166ff6a3cSBill Fenner 	case LINUX_IP_MULTICAST_LOOP:
2723f3a4815SMarcel Moolenaar 		return (IP_MULTICAST_LOOP);
27366ff6a3cSBill Fenner 	case LINUX_IP_ADD_MEMBERSHIP:
2743f3a4815SMarcel Moolenaar 		return (IP_ADD_MEMBERSHIP);
27566ff6a3cSBill Fenner 	case LINUX_IP_DROP_MEMBERSHIP:
2763f3a4815SMarcel Moolenaar 		return (IP_DROP_MEMBERSHIP);
27766ff6a3cSBill Fenner 	case LINUX_IP_HDRINCL:
2783f3a4815SMarcel Moolenaar 		return (IP_HDRINCL);
279c21dee17SSøren Schmidt 	}
2803f3a4815SMarcel Moolenaar 	return (-1);
281c21dee17SSøren Schmidt }
282c21dee17SSøren Schmidt 
283c21dee17SSøren Schmidt static int
284c21dee17SSøren Schmidt linux_to_bsd_so_sockopt(int opt)
285c21dee17SSøren Schmidt {
2863f3a4815SMarcel Moolenaar 
287c21dee17SSøren Schmidt 	switch (opt) {
288c21dee17SSøren Schmidt 	case LINUX_SO_DEBUG:
2893f3a4815SMarcel Moolenaar 		return (SO_DEBUG);
290c21dee17SSøren Schmidt 	case LINUX_SO_REUSEADDR:
2913f3a4815SMarcel Moolenaar 		return (SO_REUSEADDR);
292c21dee17SSøren Schmidt 	case LINUX_SO_TYPE:
2933f3a4815SMarcel Moolenaar 		return (SO_TYPE);
294c21dee17SSøren Schmidt 	case LINUX_SO_ERROR:
2953f3a4815SMarcel Moolenaar 		return (SO_ERROR);
296c21dee17SSøren Schmidt 	case LINUX_SO_DONTROUTE:
2973f3a4815SMarcel Moolenaar 		return (SO_DONTROUTE);
298c21dee17SSøren Schmidt 	case LINUX_SO_BROADCAST:
2993f3a4815SMarcel Moolenaar 		return (SO_BROADCAST);
300c21dee17SSøren Schmidt 	case LINUX_SO_SNDBUF:
3013f3a4815SMarcel Moolenaar 		return (SO_SNDBUF);
302c21dee17SSøren Schmidt 	case LINUX_SO_RCVBUF:
3033f3a4815SMarcel Moolenaar 		return (SO_RCVBUF);
304c21dee17SSøren Schmidt 	case LINUX_SO_KEEPALIVE:
3053f3a4815SMarcel Moolenaar 		return (SO_KEEPALIVE);
306c21dee17SSøren Schmidt 	case LINUX_SO_OOBINLINE:
3073f3a4815SMarcel Moolenaar 		return (SO_OOBINLINE);
308c21dee17SSøren Schmidt 	case LINUX_SO_LINGER:
3093f3a4815SMarcel Moolenaar 		return (SO_LINGER);
310d0b2365eSKonstantin Belousov 	case LINUX_SO_PEERCRED:
311d0b2365eSKonstantin Belousov 		return (LOCAL_PEERCRED);
312d0b2365eSKonstantin Belousov 	case LINUX_SO_RCVLOWAT:
313d0b2365eSKonstantin Belousov 		return (SO_RCVLOWAT);
314d0b2365eSKonstantin Belousov 	case LINUX_SO_SNDLOWAT:
315d0b2365eSKonstantin Belousov 		return (SO_SNDLOWAT);
316d0b2365eSKonstantin Belousov 	case LINUX_SO_RCVTIMEO:
317d0b2365eSKonstantin Belousov 		return (SO_RCVTIMEO);
318d0b2365eSKonstantin Belousov 	case LINUX_SO_SNDTIMEO:
319d0b2365eSKonstantin Belousov 		return (SO_SNDTIMEO);
320d0b2365eSKonstantin Belousov 	case LINUX_SO_TIMESTAMP:
321d0b2365eSKonstantin Belousov 		return (SO_TIMESTAMP);
322d0b2365eSKonstantin Belousov 	case LINUX_SO_ACCEPTCONN:
323d0b2365eSKonstantin Belousov 		return (SO_ACCEPTCONN);
324c21dee17SSøren Schmidt 	}
3253f3a4815SMarcel Moolenaar 	return (-1);
326c21dee17SSøren Schmidt }
327c21dee17SSøren Schmidt 
32840dbba57SAssar Westerlund static int
32940dbba57SAssar Westerlund linux_to_bsd_msg_flags(int flags)
33040dbba57SAssar Westerlund {
33140dbba57SAssar Westerlund 	int ret_flags = 0;
33240dbba57SAssar Westerlund 
33340dbba57SAssar Westerlund 	if (flags & LINUX_MSG_OOB)
33440dbba57SAssar Westerlund 		ret_flags |= MSG_OOB;
33540dbba57SAssar Westerlund 	if (flags & LINUX_MSG_PEEK)
33640dbba57SAssar Westerlund 		ret_flags |= MSG_PEEK;
33740dbba57SAssar Westerlund 	if (flags & LINUX_MSG_DONTROUTE)
33840dbba57SAssar Westerlund 		ret_flags |= MSG_DONTROUTE;
33940dbba57SAssar Westerlund 	if (flags & LINUX_MSG_CTRUNC)
34040dbba57SAssar Westerlund 		ret_flags |= MSG_CTRUNC;
34140dbba57SAssar Westerlund 	if (flags & LINUX_MSG_TRUNC)
34240dbba57SAssar Westerlund 		ret_flags |= MSG_TRUNC;
34340dbba57SAssar Westerlund 	if (flags & LINUX_MSG_DONTWAIT)
34440dbba57SAssar Westerlund 		ret_flags |= MSG_DONTWAIT;
34540dbba57SAssar Westerlund 	if (flags & LINUX_MSG_EOR)
34640dbba57SAssar Westerlund 		ret_flags |= MSG_EOR;
34740dbba57SAssar Westerlund 	if (flags & LINUX_MSG_WAITALL)
34840dbba57SAssar Westerlund 		ret_flags |= MSG_WAITALL;
3498d6e40c3SMaxim Sobolev 	if (flags & LINUX_MSG_NOSIGNAL)
3508d6e40c3SMaxim Sobolev 		ret_flags |= MSG_NOSIGNAL;
35140dbba57SAssar Westerlund #if 0 /* not handled */
35240dbba57SAssar Westerlund 	if (flags & LINUX_MSG_PROXY)
35340dbba57SAssar Westerlund 		;
35440dbba57SAssar Westerlund 	if (flags & LINUX_MSG_FIN)
35540dbba57SAssar Westerlund 		;
35640dbba57SAssar Westerlund 	if (flags & LINUX_MSG_SYN)
35740dbba57SAssar Westerlund 		;
35840dbba57SAssar Westerlund 	if (flags & LINUX_MSG_CONFIRM)
35940dbba57SAssar Westerlund 		;
36040dbba57SAssar Westerlund 	if (flags & LINUX_MSG_RST)
36140dbba57SAssar Westerlund 		;
36240dbba57SAssar Westerlund 	if (flags & LINUX_MSG_ERRQUEUE)
36340dbba57SAssar Westerlund 		;
36440dbba57SAssar Westerlund #endif
36540dbba57SAssar Westerlund 	return ret_flags;
36640dbba57SAssar Westerlund }
36740dbba57SAssar Westerlund 
3685c8919adSAlexander Leidinger /*
3695c8919adSAlexander Leidinger * If bsd_to_linux_sockaddr() or linux_to_bsd_sockaddr() faults, then the
3705c8919adSAlexander Leidinger * native syscall will fault.  Thus, we don't really need to check the
3715c8919adSAlexander Leidinger * return values for these functions.
3725c8919adSAlexander Leidinger */
3735c8919adSAlexander Leidinger 
3745c8919adSAlexander Leidinger static int
3755c8919adSAlexander Leidinger bsd_to_linux_sockaddr(struct sockaddr *arg)
3765c8919adSAlexander Leidinger {
3775c8919adSAlexander Leidinger 	struct sockaddr sa;
3785c8919adSAlexander Leidinger 	size_t sa_len = sizeof(struct sockaddr);
3795c8919adSAlexander Leidinger 	int error;
3805c8919adSAlexander Leidinger 
3815c8919adSAlexander Leidinger 	if ((error = copyin(arg, &sa, sa_len)))
3825c8919adSAlexander Leidinger 		return (error);
3835c8919adSAlexander Leidinger 
3845c8919adSAlexander Leidinger 	*(u_short *)&sa = sa.sa_family;
3855c8919adSAlexander Leidinger 
3865c8919adSAlexander Leidinger 	error = copyout(&sa, arg, sa_len);
3875c8919adSAlexander Leidinger 
3885c8919adSAlexander Leidinger 	return (error);
3895c8919adSAlexander Leidinger }
3905c8919adSAlexander Leidinger 
3915c8919adSAlexander Leidinger static int
3925c8919adSAlexander Leidinger linux_to_bsd_sockaddr(struct sockaddr *arg, int len)
3935c8919adSAlexander Leidinger {
3945c8919adSAlexander Leidinger 	struct sockaddr sa;
3955c8919adSAlexander Leidinger 	size_t sa_len = sizeof(struct sockaddr);
3965c8919adSAlexander Leidinger 	int error;
3975c8919adSAlexander Leidinger 
3985c8919adSAlexander Leidinger 	if ((error = copyin(arg, &sa, sa_len)))
3995c8919adSAlexander Leidinger 		return (error);
4005c8919adSAlexander Leidinger 
4015c8919adSAlexander Leidinger 	sa.sa_family = *(sa_family_t *)&sa;
4025c8919adSAlexander Leidinger 	sa.sa_len = len;
4035c8919adSAlexander Leidinger 
4045c8919adSAlexander Leidinger 	error = copyout(&sa, arg, sa_len);
4055c8919adSAlexander Leidinger 
4065c8919adSAlexander Leidinger 	return (error);
4075c8919adSAlexander Leidinger }
4085c8919adSAlexander Leidinger 
4095c8919adSAlexander Leidinger 
410ca26842eSHajimu UMEMOTO static int
411ca26842eSHajimu UMEMOTO linux_sa_put(struct osockaddr *osa)
412ca26842eSHajimu UMEMOTO {
413ca26842eSHajimu UMEMOTO 	struct osockaddr sa;
414ca26842eSHajimu UMEMOTO 	int error, bdom;
415ca26842eSHajimu UMEMOTO 
416ca26842eSHajimu UMEMOTO 	/*
417ca26842eSHajimu UMEMOTO 	 * Only read/write the osockaddr family part, the rest is
418ca26842eSHajimu UMEMOTO 	 * not changed.
419ca26842eSHajimu UMEMOTO 	 */
4204b7ef73dSDag-Erling Smørgrav 	error = copyin(osa, &sa, sizeof(sa.sa_family));
421ca26842eSHajimu UMEMOTO 	if (error)
422ca26842eSHajimu UMEMOTO 		return (error);
423ca26842eSHajimu UMEMOTO 
424ca26842eSHajimu UMEMOTO 	bdom = bsd_to_linux_domain(sa.sa_family);
425ca26842eSHajimu UMEMOTO 	if (bdom == -1)
426ca26842eSHajimu UMEMOTO 		return (EINVAL);
427ca26842eSHajimu UMEMOTO 
428ca26842eSHajimu UMEMOTO 	sa.sa_family = bdom;
429ca26842eSHajimu UMEMOTO 	error = copyout(&sa, osa, sizeof(sa.sa_family));
430ca26842eSHajimu UMEMOTO 	if (error)
431ca26842eSHajimu UMEMOTO 		return (error);
432ca26842eSHajimu UMEMOTO 
433ca26842eSHajimu UMEMOTO 	return (0);
434ca26842eSHajimu UMEMOTO }
435ca26842eSHajimu UMEMOTO 
4365a8a13e0SDavid Malone static int
43774f5d680SKonstantin Belousov linux_to_bsd_cmsg_type(int cmsg_type)
4385a8a13e0SDavid Malone {
43974f5d680SKonstantin Belousov 
44074f5d680SKonstantin Belousov 	switch (cmsg_type) {
44174f5d680SKonstantin Belousov 	case LINUX_SCM_RIGHTS:
44274f5d680SKonstantin Belousov 		return (SCM_RIGHTS);
443605da56bSAndriy Gapon 	case LINUX_SCM_CREDENTIALS:
444605da56bSAndriy Gapon 		return (SCM_CREDS);
44574f5d680SKonstantin Belousov 	}
44674f5d680SKonstantin Belousov 	return (-1);
44774f5d680SKonstantin Belousov }
44874f5d680SKonstantin Belousov 
44974f5d680SKonstantin Belousov static int
45074f5d680SKonstantin Belousov bsd_to_linux_cmsg_type(int cmsg_type)
45174f5d680SKonstantin Belousov {
45274f5d680SKonstantin Belousov 
45374f5d680SKonstantin Belousov 	switch (cmsg_type) {
45474f5d680SKonstantin Belousov 	case SCM_RIGHTS:
45574f5d680SKonstantin Belousov 		return (LINUX_SCM_RIGHTS);
456605da56bSAndriy Gapon 	case SCM_CREDS:
457605da56bSAndriy Gapon 		return (LINUX_SCM_CREDENTIALS);
45874f5d680SKonstantin Belousov 	}
45974f5d680SKonstantin Belousov 	return (-1);
46074f5d680SKonstantin Belousov }
46174f5d680SKonstantin Belousov 
46274f5d680SKonstantin Belousov static int
46374f5d680SKonstantin Belousov linux_to_bsd_msghdr(struct msghdr *bhdr, const struct l_msghdr *lhdr)
46474f5d680SKonstantin Belousov {
46574f5d680SKonstantin Belousov 	if (lhdr->msg_controllen > INT_MAX)
46674f5d680SKonstantin Belousov 		return (ENOBUFS);
46774f5d680SKonstantin Belousov 
46874f5d680SKonstantin Belousov 	bhdr->msg_name		= PTRIN(lhdr->msg_name);
46974f5d680SKonstantin Belousov 	bhdr->msg_namelen	= lhdr->msg_namelen;
47074f5d680SKonstantin Belousov 	bhdr->msg_iov		= PTRIN(lhdr->msg_iov);
47174f5d680SKonstantin Belousov 	bhdr->msg_iovlen	= lhdr->msg_iovlen;
47274f5d680SKonstantin Belousov 	bhdr->msg_control	= PTRIN(lhdr->msg_control);
473605da56bSAndriy Gapon 
474605da56bSAndriy Gapon 	/*
475605da56bSAndriy Gapon 	 * msg_controllen is skipped since BSD and LINUX control messages
476605da56bSAndriy Gapon 	 * are potentially different sizes (e.g. the cred structure used
477605da56bSAndriy Gapon 	 * by SCM_CREDS is different between the two operating system).
478605da56bSAndriy Gapon 	 *
479605da56bSAndriy Gapon 	 * The caller can set it (if necessary) after converting all the
480605da56bSAndriy Gapon 	 * control messages.
481605da56bSAndriy Gapon 	 */
482605da56bSAndriy Gapon 
48374f5d680SKonstantin Belousov 	bhdr->msg_flags		= linux_to_bsd_msg_flags(lhdr->msg_flags);
48474f5d680SKonstantin Belousov 	return (0);
48574f5d680SKonstantin Belousov }
48674f5d680SKonstantin Belousov 
48774f5d680SKonstantin Belousov static int
48874f5d680SKonstantin Belousov bsd_to_linux_msghdr(const struct msghdr *bhdr, struct l_msghdr *lhdr)
48974f5d680SKonstantin Belousov {
49074f5d680SKonstantin Belousov 	lhdr->msg_name		= PTROUT(bhdr->msg_name);
49174f5d680SKonstantin Belousov 	lhdr->msg_namelen	= bhdr->msg_namelen;
49274f5d680SKonstantin Belousov 	lhdr->msg_iov		= PTROUT(bhdr->msg_iov);
49374f5d680SKonstantin Belousov 	lhdr->msg_iovlen	= bhdr->msg_iovlen;
49474f5d680SKonstantin Belousov 	lhdr->msg_control	= PTROUT(bhdr->msg_control);
495605da56bSAndriy Gapon 
496605da56bSAndriy Gapon 	/*
497605da56bSAndriy Gapon 	 * msg_controllen is skipped since BSD and LINUX control messages
498605da56bSAndriy Gapon 	 * are potentially different sizes (e.g. the cred structure used
499605da56bSAndriy Gapon 	 * by SCM_CREDS is different between the two operating system).
500605da56bSAndriy Gapon 	 *
501605da56bSAndriy Gapon 	 * The caller can set it (if necessary) after converting all the
502605da56bSAndriy Gapon 	 * control messages.
503605da56bSAndriy Gapon 	 */
504605da56bSAndriy Gapon 
50574f5d680SKonstantin Belousov 	/* msg_flags skipped */
50674f5d680SKonstantin Belousov 	return (0);
50774f5d680SKonstantin Belousov }
50874f5d680SKonstantin Belousov 
50974f5d680SKonstantin Belousov static int
51038a18e97SDmitry Chagin linux_set_socket_flags(struct thread *td, int s, int flags)
51138a18e97SDmitry Chagin {
51238a18e97SDmitry Chagin 	int error;
51338a18e97SDmitry Chagin 
51438a18e97SDmitry Chagin 	if (flags & LINUX_SOCK_NONBLOCK) {
51538a18e97SDmitry Chagin 		error = kern_fcntl(td, s, F_SETFL, O_NONBLOCK);
51638a18e97SDmitry Chagin 		if (error)
51738a18e97SDmitry Chagin 			return (error);
51838a18e97SDmitry Chagin 	}
51938a18e97SDmitry Chagin 	if (flags & LINUX_SOCK_CLOEXEC) {
52038a18e97SDmitry Chagin 		error = kern_fcntl(td, s, F_SETFD, FD_CLOEXEC);
52138a18e97SDmitry Chagin 		if (error)
52238a18e97SDmitry Chagin 			return (error);
52338a18e97SDmitry Chagin 	}
52438a18e97SDmitry Chagin 	return (0);
52538a18e97SDmitry Chagin }
52638a18e97SDmitry Chagin 
52738a18e97SDmitry Chagin static int
52874f5d680SKonstantin Belousov linux_sendit(struct thread *td, int s, struct msghdr *mp, int flags,
52974f5d680SKonstantin Belousov     struct mbuf *control, enum uio_seg segflg)
53074f5d680SKonstantin Belousov {
5315a8a13e0SDavid Malone 	struct sockaddr *to;
5325a8a13e0SDavid Malone 	int error;
5335a8a13e0SDavid Malone 
5345a8a13e0SDavid Malone 	if (mp->msg_name != NULL) {
5355a8a13e0SDavid Malone 		error = linux_getsockaddr(&to, mp->msg_name, mp->msg_namelen);
5365a8a13e0SDavid Malone 		if (error)
5375a8a13e0SDavid Malone 			return (error);
5385a8a13e0SDavid Malone 		mp->msg_name = to;
5395a8a13e0SDavid Malone 	} else
5405a8a13e0SDavid Malone 		to = NULL;
5415a8a13e0SDavid Malone 
542a6886ef1SMaxim Sobolev 	error = kern_sendit(td, s, mp, linux_to_bsd_msg_flags(flags), control,
543a6886ef1SMaxim Sobolev 	    segflg);
5445a8a13e0SDavid Malone 
5455a8a13e0SDavid Malone 	if (to)
5461ede983cSDag-Erling Smørgrav 		free(to, M_SONAME);
5475a8a13e0SDavid Malone 	return (error);
5485a8a13e0SDavid Malone }
5495a8a13e0SDavid Malone 
5503f3a4815SMarcel Moolenaar /* Return 0 if IP_HDRINCL is set for the given socket. */
551f2477ae1SMike Smith static int
552e140eb43SDavid Malone linux_check_hdrincl(struct thread *td, int s)
553f2477ae1SMike Smith {
5543db2a843SBruce Evans 	int error, optval, size_val;
555f2477ae1SMike Smith 
556e140eb43SDavid Malone 	size_val = sizeof(optval);
557e140eb43SDavid Malone 	error = kern_getsockopt(td, s, IPPROTO_IP, IP_HDRINCL,
558e140eb43SDavid Malone 	    &optval, UIO_SYSSPACE, &size_val);
559e140eb43SDavid Malone 	if (error)
5603f3a4815SMarcel Moolenaar 		return (error);
5613f3a4815SMarcel Moolenaar 
5623f3a4815SMarcel Moolenaar 	return (optval == 0);
563f2477ae1SMike Smith }
564f2477ae1SMike Smith 
5655a8a13e0SDavid Malone struct linux_sendto_args {
5665a8a13e0SDavid Malone 	int s;
5674af27623STim J. Robbins 	l_uintptr_t msg;
5685a8a13e0SDavid Malone 	int len;
5695a8a13e0SDavid Malone 	int flags;
5704af27623STim J. Robbins 	l_uintptr_t to;
5715a8a13e0SDavid Malone 	int tolen;
5725a8a13e0SDavid Malone };
5735a8a13e0SDavid Malone 
574f2477ae1SMike Smith /*
575f2477ae1SMike Smith  * Updated sendto() when IP_HDRINCL is set:
576f2477ae1SMike Smith  * tweak endian-dependent fields in the IP packet.
577f2477ae1SMike Smith  */
578f2477ae1SMike Smith static int
57938da2381SRobert Watson linux_sendto_hdrincl(struct thread *td, struct linux_sendto_args *linux_args)
580f2477ae1SMike Smith {
581f2477ae1SMike Smith /*
582f2477ae1SMike Smith  * linux_ip_copysize defines how many bytes we should copy
583f2477ae1SMike Smith  * from the beginning of the IP packet before we customize it for BSD.
584a6886ef1SMaxim Sobolev  * It should include all the fields we modify (ip_len and ip_off).
585f2477ae1SMike Smith  */
586f2477ae1SMike Smith #define linux_ip_copysize	8
587f2477ae1SMike Smith 
588f2477ae1SMike Smith 	struct ip *packet;
5895a8a13e0SDavid Malone 	struct msghdr msg;
590a6886ef1SMaxim Sobolev 	struct iovec aiov[1];
591f2477ae1SMike Smith 	int error;
592f2477ae1SMike Smith 
593aa675b57SDavid Schultz 	/* Check that the packet isn't too big or too small. */
594aa675b57SDavid Schultz 	if (linux_args->len < linux_ip_copysize ||
595aa675b57SDavid Schultz 	    linux_args->len > IP_MAXPACKET)
5963f3a4815SMarcel Moolenaar 		return (EINVAL);
597f2477ae1SMike Smith 
598a6886ef1SMaxim Sobolev 	packet = (struct ip *)malloc(linux_args->len, M_TEMP, M_WAITOK);
599f2477ae1SMike Smith 
600a6886ef1SMaxim Sobolev 	/* Make kernel copy of the packet to be sent */
6014af27623STim J. Robbins 	if ((error = copyin(PTRIN(linux_args->msg), packet,
602a6886ef1SMaxim Sobolev 	    linux_args->len)))
603a6886ef1SMaxim Sobolev 		goto goout;
604f2477ae1SMike Smith 
605f2477ae1SMike Smith 	/* Convert fields from Linux to BSD raw IP socket format */
6065a8a13e0SDavid Malone 	packet->ip_len = linux_args->len;
607f2477ae1SMike Smith 	packet->ip_off = ntohs(packet->ip_off);
608f2477ae1SMike Smith 
609f2477ae1SMike Smith 	/* Prepare the msghdr and iovec structures describing the new packet */
6104af27623STim J. Robbins 	msg.msg_name = PTRIN(linux_args->to);
6115a8a13e0SDavid Malone 	msg.msg_namelen = linux_args->tolen;
6125a8a13e0SDavid Malone 	msg.msg_iov = aiov;
613a6886ef1SMaxim Sobolev 	msg.msg_iovlen = 1;
6145a8a13e0SDavid Malone 	msg.msg_control = NULL;
6155a8a13e0SDavid Malone 	msg.msg_flags = 0;
6165a8a13e0SDavid Malone 	aiov[0].iov_base = (char *)packet;
617a6886ef1SMaxim Sobolev 	aiov[0].iov_len = linux_args->len;
618a6886ef1SMaxim Sobolev 	error = linux_sendit(td, linux_args->s, &msg, linux_args->flags,
61974f5d680SKonstantin Belousov 	    NULL, UIO_SYSSPACE);
620a6886ef1SMaxim Sobolev goout:
621a6886ef1SMaxim Sobolev 	free(packet, M_TEMP);
6225a8a13e0SDavid Malone 	return (error);
623f2477ae1SMike Smith }
624f2477ae1SMike Smith 
625c21dee17SSøren Schmidt struct linux_socket_args {
626c21dee17SSøren Schmidt 	int domain;
627c21dee17SSøren Schmidt 	int type;
628c21dee17SSøren Schmidt 	int protocol;
629c21dee17SSøren Schmidt };
630c21dee17SSøren Schmidt 
631c21dee17SSøren Schmidt static int
632b40ce416SJulian Elischer linux_socket(struct thread *td, struct linux_socket_args *args)
633c21dee17SSøren Schmidt {
634ef04503dSPeter Wemm 	struct socket_args /* {
635c21dee17SSøren Schmidt 		int domain;
636c21dee17SSøren Schmidt 		int type;
637c21dee17SSøren Schmidt 		int protocol;
638ef04503dSPeter Wemm 	} */ bsd_args;
6393933bde2SDmitry Chagin 	int retval_socket, socket_flags;
640c21dee17SSøren Schmidt 
641745aaef5SKonstantin Belousov 	bsd_args.protocol = args->protocol;
6423933bde2SDmitry Chagin 	socket_flags = args->type & ~LINUX_SOCK_TYPE_MASK;
6433933bde2SDmitry Chagin 	if (socket_flags & ~(LINUX_SOCK_CLOEXEC | LINUX_SOCK_NONBLOCK))
6443933bde2SDmitry Chagin 		return (EINVAL);
6453933bde2SDmitry Chagin 	bsd_args.type = args->type & LINUX_SOCK_TYPE_MASK;
646eeb63e51SDmitry Chagin 	if (bsd_args.type < 0 || bsd_args.type > LINUX_SOCK_MAX)
647eeb63e51SDmitry Chagin 		return (EINVAL);
648745aaef5SKonstantin Belousov 	bsd_args.domain = linux_to_bsd_domain(args->domain);
649c21dee17SSøren Schmidt 	if (bsd_args.domain == -1)
650d9b063ccSDmitry Chagin 		return (EAFNOSUPPORT);
651f2477ae1SMike Smith 
6528451d0ddSKip Macy 	retval_socket = sys_socket(td, &bsd_args);
6536994ea54SDmitry Chagin 	if (retval_socket)
6546994ea54SDmitry Chagin 		return (retval_socket);
6556994ea54SDmitry Chagin 
65638a18e97SDmitry Chagin 	retval_socket = linux_set_socket_flags(td, td->td_retval[0],
65738a18e97SDmitry Chagin 	    socket_flags);
6583933bde2SDmitry Chagin 	if (retval_socket) {
6593933bde2SDmitry Chagin 		(void)kern_close(td, td->td_retval[0]);
6603933bde2SDmitry Chagin 		goto out;
6613933bde2SDmitry Chagin 	}
6623933bde2SDmitry Chagin 
663f2477ae1SMike Smith 	if (bsd_args.type == SOCK_RAW
664f2477ae1SMike Smith 	    && (bsd_args.protocol == IPPROTO_RAW || bsd_args.protocol == 0)
6656994ea54SDmitry Chagin 	    && bsd_args.domain == PF_INET) {
666f2477ae1SMike Smith 		/* It's a raw IP socket: set the IP_HDRINCL option. */
667e140eb43SDavid Malone 		int hdrincl;
668f2477ae1SMike Smith 
669e140eb43SDavid Malone 		hdrincl = 1;
670e140eb43SDavid Malone 		/* We ignore any error returned by kern_setsockopt() */
671e140eb43SDavid Malone 		kern_setsockopt(td, td->td_retval[0], IPPROTO_IP, IP_HDRINCL,
672e140eb43SDavid Malone 		    &hdrincl, UIO_SYSSPACE, sizeof(hdrincl));
673f2477ae1SMike Smith 	}
674ca26842eSHajimu UMEMOTO #ifdef INET6
675ca26842eSHajimu UMEMOTO 	/*
676d97bee3eSBjoern A. Zeeb 	 * Linux AF_INET6 socket has IPV6_V6ONLY setsockopt set to 0 by default
677d97bee3eSBjoern A. Zeeb 	 * and some apps depend on this. So, set V6ONLY to 0 for Linux apps.
678d97bee3eSBjoern A. Zeeb 	 * For simplicity we do this unconditionally of the net.inet6.ip6.v6only
679d97bee3eSBjoern A. Zeeb 	 * sysctl value.
680ca26842eSHajimu UMEMOTO 	 */
681d97bee3eSBjoern A. Zeeb 	if (bsd_args.domain == PF_INET6) {
682e140eb43SDavid Malone 		int v6only;
683ca26842eSHajimu UMEMOTO 
684e140eb43SDavid Malone 		v6only = 0;
685ca26842eSHajimu UMEMOTO 		/* We ignore any error returned by setsockopt() */
686e140eb43SDavid Malone 		kern_setsockopt(td, td->td_retval[0], IPPROTO_IPV6, IPV6_V6ONLY,
687e140eb43SDavid Malone 		    &v6only, UIO_SYSSPACE, sizeof(v6only));
688ca26842eSHajimu UMEMOTO 	}
689ca26842eSHajimu UMEMOTO #endif
6903f3a4815SMarcel Moolenaar 
6913933bde2SDmitry Chagin out:
6923f3a4815SMarcel Moolenaar 	return (retval_socket);
693c21dee17SSøren Schmidt }
694c21dee17SSøren Schmidt 
695c21dee17SSøren Schmidt struct linux_bind_args {
696c21dee17SSøren Schmidt 	int s;
6974af27623STim J. Robbins 	l_uintptr_t name;
698c21dee17SSøren Schmidt 	int namelen;
699c21dee17SSøren Schmidt };
700c21dee17SSøren Schmidt 
701c21dee17SSøren Schmidt static int
702b40ce416SJulian Elischer linux_bind(struct thread *td, struct linux_bind_args *args)
703c21dee17SSøren Schmidt {
704ca26842eSHajimu UMEMOTO 	struct sockaddr *sa;
705c21dee17SSøren Schmidt 	int error;
706c21dee17SSøren Schmidt 
707745aaef5SKonstantin Belousov 	error = linux_getsockaddr(&sa, PTRIN(args->name),
708745aaef5SKonstantin Belousov 	    args->namelen);
709ca26842eSHajimu UMEMOTO 	if (error)
710ca26842eSHajimu UMEMOTO 		return (error);
711ca26842eSHajimu UMEMOTO 
712745aaef5SKonstantin Belousov 	error = kern_bind(td, args->s, sa);
713b33887eaSJohn Baldwin 	free(sa, M_SONAME);
714745aaef5SKonstantin Belousov 	if (error == EADDRNOTAVAIL && args->namelen != sizeof(struct sockaddr_in))
715d4b7423fSAlexander Leidinger 	   	return (EINVAL);
716b33887eaSJohn Baldwin 	return (error);
717c21dee17SSøren Schmidt }
718c21dee17SSøren Schmidt 
71901e0ffbaSAlexander Leidinger struct linux_connect_args {
720c21dee17SSøren Schmidt 	int s;
7214af27623STim J. Robbins 	l_uintptr_t name;
722c21dee17SSøren Schmidt 	int namelen;
723c21dee17SSøren Schmidt };
724b40ce416SJulian Elischer int linux_connect(struct thread *, struct linux_connect_args *);
725c21dee17SSøren Schmidt 
726930a65feSAndrew Gallatin int
727b40ce416SJulian Elischer linux_connect(struct thread *td, struct linux_connect_args *args)
728c21dee17SSøren Schmidt {
7290bf301c0SJonathan Lemon 	struct socket *so;
730ca26842eSHajimu UMEMOTO 	struct sockaddr *sa;
73139c95b83SMatthew Dillon 	u_int fflag;
732c21dee17SSøren Schmidt 	int error;
733c21dee17SSøren Schmidt 
734745aaef5SKonstantin Belousov 	error = linux_getsockaddr(&sa, (struct osockaddr *)PTRIN(args->name),
735745aaef5SKonstantin Belousov 	    args->namelen);
736ca26842eSHajimu UMEMOTO 	if (error)
737ca26842eSHajimu UMEMOTO 		return (error);
738ca26842eSHajimu UMEMOTO 
739745aaef5SKonstantin Belousov 	error = kern_connect(td, args->s, sa);
740b33887eaSJohn Baldwin 	free(sa, M_SONAME);
7410bf301c0SJonathan Lemon 	if (error != EISCONN)
7420bf301c0SJonathan Lemon 		return (error);
7430bf301c0SJonathan Lemon 
744dad3b88aSMike Smith 	/*
745dad3b88aSMike Smith 	 * Linux doesn't return EISCONN the first time it occurs,
746dad3b88aSMike Smith 	 * when on a non-blocking socket. Instead it returns the
747dad3b88aSMike Smith 	 * error getsockopt(SOL_SOCKET, SO_ERROR) would return on BSD.
748f7f45ac8SRobert Watson 	 *
749f7f45ac8SRobert Watson 	 * XXXRW: Instead of using fgetsock(), check that it is a
750f7f45ac8SRobert Watson 	 * socket and use the file descriptor reference instead of
751f7f45ac8SRobert Watson 	 * creating a new one.
752dad3b88aSMike Smith 	 */
753a9d2f8d8SRobert Watson 	error = fgetsock(td, args->s, CAP_CONNECT, &so, &fflag);
7544641373fSJohn Baldwin 	if (error == 0) {
7550bf301c0SJonathan Lemon 		error = EISCONN;
75639c95b83SMatthew Dillon 		if (fflag & FNONBLOCK) {
7574641373fSJohn Baldwin 			SOCK_LOCK(so);
7585002a60fSMarcel Moolenaar 			if (so->so_emuldata == 0)
7590bf301c0SJonathan Lemon 				error = so->so_error;
7600bf301c0SJonathan Lemon 			so->so_emuldata = (void *)1;
7614641373fSJohn Baldwin 			SOCK_UNLOCK(so);
762dad3b88aSMike Smith 		}
76339c95b83SMatthew Dillon 		fputsock(so);
7644641373fSJohn Baldwin 	}
7653f3a4815SMarcel Moolenaar 	return (error);
766c21dee17SSøren Schmidt }
767c21dee17SSøren Schmidt 
768c21dee17SSøren Schmidt struct linux_listen_args {
769c21dee17SSøren Schmidt 	int s;
770c21dee17SSøren Schmidt 	int backlog;
771c21dee17SSøren Schmidt };
772c21dee17SSøren Schmidt 
773c21dee17SSøren Schmidt static int
774b40ce416SJulian Elischer linux_listen(struct thread *td, struct linux_listen_args *args)
775c21dee17SSøren Schmidt {
776ef04503dSPeter Wemm 	struct listen_args /* {
777c21dee17SSøren Schmidt 		int s;
778c21dee17SSøren Schmidt 		int backlog;
779ef04503dSPeter Wemm 	} */ bsd_args;
780c21dee17SSøren Schmidt 
781745aaef5SKonstantin Belousov 	bsd_args.s = args->s;
782745aaef5SKonstantin Belousov 	bsd_args.backlog = args->backlog;
7838451d0ddSKip Macy 	return (sys_listen(td, &bsd_args));
784c21dee17SSøren Schmidt }
785c21dee17SSøren Schmidt 
78601e0ffbaSAlexander Leidinger static int
787c8f37d61SDmitry Chagin linux_accept_common(struct thread *td, int s, l_uintptr_t addr,
788f83427b8SDmitry Chagin     l_uintptr_t namelen, int flags)
789c21dee17SSøren Schmidt {
790ef04503dSPeter Wemm 	struct accept_args /* {
791c21dee17SSøren Schmidt 		int	s;
7923db2a843SBruce Evans 		struct sockaddr * __restrict name;
7933db2a843SBruce Evans 		socklen_t * __restrict anamelen;
794ef04503dSPeter Wemm 	} */ bsd_args;
79593e694c9SDmitry Chagin 	int error;
79693e694c9SDmitry Chagin 
79793e694c9SDmitry Chagin 	if (flags & ~(LINUX_SOCK_CLOEXEC | LINUX_SOCK_NONBLOCK))
79893e694c9SDmitry Chagin 		return (EINVAL);
799c21dee17SSøren Schmidt 
800c8f37d61SDmitry Chagin 	bsd_args.s = s;
8013db2a843SBruce Evans 	/* XXX: */
802c8f37d61SDmitry Chagin 	bsd_args.name = (struct sockaddr * __restrict)PTRIN(addr);
803c8f37d61SDmitry Chagin 	bsd_args.anamelen = PTRIN(namelen);/* XXX */
8048451d0ddSKip Macy 	error = sys_accept(td, &bsd_args);
8055c8919adSAlexander Leidinger 	bsd_to_linux_sockaddr((struct sockaddr *)bsd_args.name);
806d4b7423fSAlexander Leidinger 	if (error) {
807c8f37d61SDmitry Chagin 		if (error == EFAULT && namelen != sizeof(struct sockaddr_in))
808d4b7423fSAlexander Leidinger 			return (EINVAL);
809dba5ab66SMarcel Moolenaar 		return (error);
810d4b7423fSAlexander Leidinger 	}
811dba5ab66SMarcel Moolenaar 
812dba5ab66SMarcel Moolenaar 	/*
813dba5ab66SMarcel Moolenaar 	 * linux appears not to copy flags from the parent socket to the
81493e694c9SDmitry Chagin 	 * accepted one, so we must clear the flags in the new descriptor
81593e694c9SDmitry Chagin 	 * and apply the requested flags.
816dba5ab66SMarcel Moolenaar 	 */
81793e694c9SDmitry Chagin 	error = kern_fcntl(td, td->td_retval[0], F_SETFL, 0);
81893e694c9SDmitry Chagin 	if (error)
81993e694c9SDmitry Chagin 		goto out;
82093e694c9SDmitry Chagin 	error = linux_set_socket_flags(td, td->td_retval[0], flags);
82193e694c9SDmitry Chagin 	if (error)
82293e694c9SDmitry Chagin 		goto out;
82393e694c9SDmitry Chagin 	if (addr)
82493e694c9SDmitry Chagin 		error = linux_sa_put(PTRIN(addr));
82593e694c9SDmitry Chagin 
82693e694c9SDmitry Chagin out:
82793e694c9SDmitry Chagin 	if (error) {
82893e694c9SDmitry Chagin 		(void)kern_close(td, td->td_retval[0]);
82993e694c9SDmitry Chagin 		td->td_retval[0] = 0;
83093e694c9SDmitry Chagin 	}
83193e694c9SDmitry Chagin 	return (error);
832c21dee17SSøren Schmidt }
833c21dee17SSøren Schmidt 
834c8f37d61SDmitry Chagin struct linux_accept_args {
835c8f37d61SDmitry Chagin 	int s;
836c8f37d61SDmitry Chagin 	l_uintptr_t addr;
837c8f37d61SDmitry Chagin 	l_uintptr_t namelen;
838c8f37d61SDmitry Chagin };
839c8f37d61SDmitry Chagin 
840c8f37d61SDmitry Chagin static int
841c8f37d61SDmitry Chagin linux_accept(struct thread *td, struct linux_accept_args *args)
842c8f37d61SDmitry Chagin {
843c8f37d61SDmitry Chagin 
844c8f37d61SDmitry Chagin 	return (linux_accept_common(td, args->s, args->addr,
845f83427b8SDmitry Chagin 	    args->namelen, 0));
846c8f37d61SDmitry Chagin }
847c8f37d61SDmitry Chagin 
848f8cd0af2SDmitry Chagin struct linux_accept4_args {
849f8cd0af2SDmitry Chagin 	int s;
850f8cd0af2SDmitry Chagin 	l_uintptr_t addr;
851f8cd0af2SDmitry Chagin 	l_uintptr_t namelen;
852f8cd0af2SDmitry Chagin 	int flags;
853f8cd0af2SDmitry Chagin };
854f8cd0af2SDmitry Chagin 
855f8cd0af2SDmitry Chagin static int
856f8cd0af2SDmitry Chagin linux_accept4(struct thread *td, struct linux_accept4_args *args)
857f8cd0af2SDmitry Chagin {
858f8cd0af2SDmitry Chagin 
859f8cd0af2SDmitry Chagin 	return (linux_accept_common(td, args->s, args->addr,
860f8cd0af2SDmitry Chagin 	    args->namelen, args->flags));
861f8cd0af2SDmitry Chagin }
862f8cd0af2SDmitry Chagin 
86301e0ffbaSAlexander Leidinger struct linux_getsockname_args {
864c21dee17SSøren Schmidt 	int s;
8654af27623STim J. Robbins 	l_uintptr_t addr;
8664af27623STim J. Robbins 	l_uintptr_t namelen;
867c21dee17SSøren Schmidt };
868c21dee17SSøren Schmidt 
86901e0ffbaSAlexander Leidinger static int
870b40ce416SJulian Elischer linux_getsockname(struct thread *td, struct linux_getsockname_args *args)
871c21dee17SSøren Schmidt {
872ef04503dSPeter Wemm 	struct getsockname_args /* {
873c21dee17SSøren Schmidt 		int	fdes;
8743db2a843SBruce Evans 		struct sockaddr * __restrict asa;
8753db2a843SBruce Evans 		socklen_t * __restrict alen;
876ef04503dSPeter Wemm 	} */ bsd_args;
877c21dee17SSøren Schmidt 	int error;
878c21dee17SSøren Schmidt 
879745aaef5SKonstantin Belousov 	bsd_args.fdes = args->s;
8803db2a843SBruce Evans 	/* XXX: */
881745aaef5SKonstantin Belousov 	bsd_args.asa = (struct sockaddr * __restrict)PTRIN(args->addr);
882745aaef5SKonstantin Belousov 	bsd_args.alen = PTRIN(args->namelen);	/* XXX */
8838451d0ddSKip Macy 	error = sys_getsockname(td, &bsd_args);
8845c8919adSAlexander Leidinger 	bsd_to_linux_sockaddr((struct sockaddr *)bsd_args.asa);
885ca26842eSHajimu UMEMOTO 	if (error)
886ca26842eSHajimu UMEMOTO 		return (error);
887745aaef5SKonstantin Belousov 	error = linux_sa_put(PTRIN(args->addr));
888ca26842eSHajimu UMEMOTO 	if (error)
889ca26842eSHajimu UMEMOTO 		return (error);
890ca26842eSHajimu UMEMOTO 	return (0);
891c21dee17SSøren Schmidt }
892c21dee17SSøren Schmidt 
89301e0ffbaSAlexander Leidinger struct linux_getpeername_args {
894c21dee17SSøren Schmidt 	int s;
8954af27623STim J. Robbins 	l_uintptr_t addr;
8964af27623STim J. Robbins 	l_uintptr_t namelen;
897c21dee17SSøren Schmidt };
898c21dee17SSøren Schmidt 
89901e0ffbaSAlexander Leidinger static int
900b40ce416SJulian Elischer linux_getpeername(struct thread *td, struct linux_getpeername_args *args)
901c21dee17SSøren Schmidt {
9025c8919adSAlexander Leidinger 	struct getpeername_args /* {
903c21dee17SSøren Schmidt 		int fdes;
904c21dee17SSøren Schmidt 		caddr_t asa;
905c21dee17SSøren Schmidt 		int *alen;
906ef04503dSPeter Wemm 	} */ bsd_args;
907c21dee17SSøren Schmidt 	int error;
908c21dee17SSøren Schmidt 
909745aaef5SKonstantin Belousov 	bsd_args.fdes = args->s;
910745aaef5SKonstantin Belousov 	bsd_args.asa = (struct sockaddr *)PTRIN(args->addr);
911745aaef5SKonstantin Belousov 	bsd_args.alen = (int *)PTRIN(args->namelen);
9128451d0ddSKip Macy 	error = sys_getpeername(td, &bsd_args);
9135c8919adSAlexander Leidinger 	bsd_to_linux_sockaddr((struct sockaddr *)bsd_args.asa);
914ca26842eSHajimu UMEMOTO 	if (error)
915ca26842eSHajimu UMEMOTO 		return (error);
916745aaef5SKonstantin Belousov 	error = linux_sa_put(PTRIN(args->addr));
917ca26842eSHajimu UMEMOTO 	if (error)
918ca26842eSHajimu UMEMOTO 		return (error);
919ca26842eSHajimu UMEMOTO 	return (0);
920c21dee17SSøren Schmidt }
921c21dee17SSøren Schmidt 
92201e0ffbaSAlexander Leidinger struct linux_socketpair_args {
923c21dee17SSøren Schmidt 	int domain;
924c21dee17SSøren Schmidt 	int type;
925c21dee17SSøren Schmidt 	int protocol;
9264af27623STim J. Robbins 	l_uintptr_t rsv;
927c21dee17SSøren Schmidt };
928c21dee17SSøren Schmidt 
92901e0ffbaSAlexander Leidinger static int
930b40ce416SJulian Elischer linux_socketpair(struct thread *td, struct linux_socketpair_args *args)
931c21dee17SSøren Schmidt {
932ef04503dSPeter Wemm 	struct socketpair_args /* {
933c21dee17SSøren Schmidt 		int domain;
934c21dee17SSøren Schmidt 		int type;
935c21dee17SSøren Schmidt 		int protocol;
936c21dee17SSøren Schmidt 		int *rsv;
937ef04503dSPeter Wemm 	} */ bsd_args;
93839253cf9SDmitry Chagin 	int error, socket_flags;
93939253cf9SDmitry Chagin 	int sv[2];
940c21dee17SSøren Schmidt 
941745aaef5SKonstantin Belousov 	bsd_args.domain = linux_to_bsd_domain(args->domain);
9421a52a4abSDmitry Chagin 	if (bsd_args.domain != PF_LOCAL)
9431a52a4abSDmitry Chagin 		return (EAFNOSUPPORT);
9443f3a4815SMarcel Moolenaar 
94539253cf9SDmitry Chagin 	socket_flags = args->type & ~LINUX_SOCK_TYPE_MASK;
94639253cf9SDmitry Chagin 	if (socket_flags & ~(LINUX_SOCK_CLOEXEC | LINUX_SOCK_NONBLOCK))
94739253cf9SDmitry Chagin 		return (EINVAL);
94839253cf9SDmitry Chagin 	bsd_args.type = args->type & LINUX_SOCK_TYPE_MASK;
94939253cf9SDmitry Chagin 	if (bsd_args.type < 0 || bsd_args.type > LINUX_SOCK_MAX)
95039253cf9SDmitry Chagin 		return (EINVAL);
95139253cf9SDmitry Chagin 
9521a52a4abSDmitry Chagin 	if (args->protocol != 0 && args->protocol != PF_UNIX)
9531a52a4abSDmitry Chagin 
9541a52a4abSDmitry Chagin 		/*
9551a52a4abSDmitry Chagin 		 * Use of PF_UNIX as protocol argument is not right,
9561a52a4abSDmitry Chagin 		 * but Linux does it.
9571a52a4abSDmitry Chagin 		 * Do not map PF_UNIX as its Linux value is identical
9581a52a4abSDmitry Chagin 		 * to FreeBSD one.
9591a52a4abSDmitry Chagin 		 */
9601a52a4abSDmitry Chagin 		return (EPROTONOSUPPORT);
96140092d93SDmitry Chagin 	else
9621a52a4abSDmitry Chagin 		bsd_args.protocol = 0;
963745aaef5SKonstantin Belousov 	bsd_args.rsv = (int *)PTRIN(args->rsv);
96439253cf9SDmitry Chagin 	error = kern_socketpair(td, bsd_args.domain, bsd_args.type,
96539253cf9SDmitry Chagin 	    bsd_args.protocol, sv);
96639253cf9SDmitry Chagin 	if (error)
96739253cf9SDmitry Chagin 		return (error);
96839253cf9SDmitry Chagin 	error = linux_set_socket_flags(td, sv[0], socket_flags);
96939253cf9SDmitry Chagin 	if (error)
97039253cf9SDmitry Chagin 		goto out;
97139253cf9SDmitry Chagin 	error = linux_set_socket_flags(td, sv[1], socket_flags);
97239253cf9SDmitry Chagin 	if (error)
97339253cf9SDmitry Chagin 		goto out;
97439253cf9SDmitry Chagin 
97539253cf9SDmitry Chagin 	error = copyout(sv, bsd_args.rsv, 2 * sizeof(int));
97639253cf9SDmitry Chagin 
97739253cf9SDmitry Chagin out:
97839253cf9SDmitry Chagin 	if (error) {
97939253cf9SDmitry Chagin 		(void)kern_close(td, sv[0]);
98039253cf9SDmitry Chagin 		(void)kern_close(td, sv[1]);
98139253cf9SDmitry Chagin 	}
98239253cf9SDmitry Chagin 	return (error);
983c21dee17SSøren Schmidt }
984c21dee17SSøren Schmidt 
98501e0ffbaSAlexander Leidinger struct linux_send_args {
986c21dee17SSøren Schmidt 	int s;
9874af27623STim J. Robbins 	l_uintptr_t msg;
988c21dee17SSøren Schmidt 	int len;
989c21dee17SSøren Schmidt 	int flags;
990c21dee17SSøren Schmidt };
991c21dee17SSøren Schmidt 
99201e0ffbaSAlexander Leidinger static int
993b40ce416SJulian Elischer linux_send(struct thread *td, struct linux_send_args *args)
994c21dee17SSøren Schmidt {
99587d72a8fSPoul-Henning Kamp 	struct sendto_args /* {
996c21dee17SSøren Schmidt 		int s;
997c21dee17SSøren Schmidt 		caddr_t buf;
998044af7c3SJonathan Mini 		int len;
999c21dee17SSøren Schmidt 		int flags;
100087d72a8fSPoul-Henning Kamp 		caddr_t to;
100187d72a8fSPoul-Henning Kamp 		int tolen;
1002ef04503dSPeter Wemm 	} */ bsd_args;
1003c21dee17SSøren Schmidt 
1004745aaef5SKonstantin Belousov 	bsd_args.s = args->s;
1005745aaef5SKonstantin Belousov 	bsd_args.buf = (caddr_t)PTRIN(args->msg);
1006745aaef5SKonstantin Belousov 	bsd_args.len = args->len;
1007745aaef5SKonstantin Belousov 	bsd_args.flags = args->flags;
100887d72a8fSPoul-Henning Kamp 	bsd_args.to = NULL;
100987d72a8fSPoul-Henning Kamp 	bsd_args.tolen = 0;
10108451d0ddSKip Macy 	return sys_sendto(td, &bsd_args);
1011c21dee17SSøren Schmidt }
1012c21dee17SSøren Schmidt 
101301e0ffbaSAlexander Leidinger struct linux_recv_args {
1014c21dee17SSøren Schmidt 	int s;
10154af27623STim J. Robbins 	l_uintptr_t msg;
1016c21dee17SSøren Schmidt 	int len;
1017c21dee17SSøren Schmidt 	int flags;
1018c21dee17SSøren Schmidt };
1019c21dee17SSøren Schmidt 
102001e0ffbaSAlexander Leidinger static int
1021b40ce416SJulian Elischer linux_recv(struct thread *td, struct linux_recv_args *args)
1022c21dee17SSøren Schmidt {
102387d72a8fSPoul-Henning Kamp 	struct recvfrom_args /* {
1024c21dee17SSøren Schmidt 		int s;
1025c21dee17SSøren Schmidt 		caddr_t buf;
1026c21dee17SSøren Schmidt 		int len;
1027c21dee17SSøren Schmidt 		int flags;
102887d72a8fSPoul-Henning Kamp 		struct sockaddr *from;
102987d72a8fSPoul-Henning Kamp 		socklen_t fromlenaddr;
1030ef04503dSPeter Wemm 	} */ bsd_args;
1031c21dee17SSøren Schmidt 
1032745aaef5SKonstantin Belousov 	bsd_args.s = args->s;
1033745aaef5SKonstantin Belousov 	bsd_args.buf = (caddr_t)PTRIN(args->msg);
1034745aaef5SKonstantin Belousov 	bsd_args.len = args->len;
10353980a435SDmitry Chagin 	bsd_args.flags = linux_to_bsd_msg_flags(args->flags);
103687d72a8fSPoul-Henning Kamp 	bsd_args.from = NULL;
103787d72a8fSPoul-Henning Kamp 	bsd_args.fromlenaddr = 0;
10388451d0ddSKip Macy 	return (sys_recvfrom(td, &bsd_args));
1039c21dee17SSøren Schmidt }
1040c21dee17SSøren Schmidt 
1041c21dee17SSøren Schmidt static int
1042b40ce416SJulian Elischer linux_sendto(struct thread *td, struct linux_sendto_args *args)
1043c21dee17SSøren Schmidt {
10445a8a13e0SDavid Malone 	struct msghdr msg;
10455a8a13e0SDavid Malone 	struct iovec aiov;
10465a8a13e0SDavid Malone 	int error;
1047c21dee17SSøren Schmidt 
1048745aaef5SKonstantin Belousov 	if (linux_check_hdrincl(td, args->s) == 0)
1049f2477ae1SMike Smith 		/* IP_HDRINCL set, tweak the packet before sending */
1050745aaef5SKonstantin Belousov 		return (linux_sendto_hdrincl(td, args));
1051f2477ae1SMike Smith 
1052745aaef5SKonstantin Belousov 	msg.msg_name = PTRIN(args->to);
1053745aaef5SKonstantin Belousov 	msg.msg_namelen = args->tolen;
10545a8a13e0SDavid Malone 	msg.msg_iov = &aiov;
10555a8a13e0SDavid Malone 	msg.msg_iovlen = 1;
10565a8a13e0SDavid Malone 	msg.msg_control = NULL;
10575a8a13e0SDavid Malone 	msg.msg_flags = 0;
1058745aaef5SKonstantin Belousov 	aiov.iov_base = PTRIN(args->msg);
1059745aaef5SKonstantin Belousov 	aiov.iov_len = args->len;
106074f5d680SKonstantin Belousov 	error = linux_sendit(td, args->s, &msg, args->flags, NULL,
106174f5d680SKonstantin Belousov 	    UIO_USERSPACE);
10625a8a13e0SDavid Malone 	return (error);
1063c21dee17SSøren Schmidt }
1064c21dee17SSøren Schmidt 
106501e0ffbaSAlexander Leidinger struct linux_recvfrom_args {
1066c21dee17SSøren Schmidt 	int s;
10674af27623STim J. Robbins 	l_uintptr_t buf;
1068c21dee17SSøren Schmidt 	int len;
1069c21dee17SSøren Schmidt 	int flags;
10704af27623STim J. Robbins 	l_uintptr_t from;
10714af27623STim J. Robbins 	l_uintptr_t fromlen;
1072c21dee17SSøren Schmidt };
1073c21dee17SSøren Schmidt 
107401e0ffbaSAlexander Leidinger static int
1075b40ce416SJulian Elischer linux_recvfrom(struct thread *td, struct linux_recvfrom_args *args)
1076c21dee17SSøren Schmidt {
1077ef04503dSPeter Wemm 	struct recvfrom_args /* {
1078c21dee17SSøren Schmidt 		int	s;
1079c21dee17SSøren Schmidt 		caddr_t	buf;
1080c21dee17SSøren Schmidt 		size_t	len;
1081c21dee17SSøren Schmidt 		int	flags;
10823db2a843SBruce Evans 		struct sockaddr * __restrict from;
10833db2a843SBruce Evans 		socklen_t * __restrict fromlenaddr;
1084ef04503dSPeter Wemm 	} */ bsd_args;
10855c8919adSAlexander Leidinger 	size_t len;
1086c21dee17SSøren Schmidt 	int error;
1087c21dee17SSøren Schmidt 
1088745aaef5SKonstantin Belousov 	if ((error = copyin(PTRIN(args->fromlen), &len, sizeof(size_t))))
10893f3a4815SMarcel Moolenaar 		return (error);
10903f3a4815SMarcel Moolenaar 
1091745aaef5SKonstantin Belousov 	bsd_args.s = args->s;
1092745aaef5SKonstantin Belousov 	bsd_args.buf = PTRIN(args->buf);
1093745aaef5SKonstantin Belousov 	bsd_args.len = args->len;
1094745aaef5SKonstantin Belousov 	bsd_args.flags = linux_to_bsd_msg_flags(args->flags);
10953db2a843SBruce Evans 	/* XXX: */
1096745aaef5SKonstantin Belousov 	bsd_args.from = (struct sockaddr * __restrict)PTRIN(args->from);
1097745aaef5SKonstantin Belousov 	bsd_args.fromlenaddr = PTRIN(args->fromlen);/* XXX */
10985c8919adSAlexander Leidinger 
10995c8919adSAlexander Leidinger 	linux_to_bsd_sockaddr((struct sockaddr *)bsd_args.from, len);
11008451d0ddSKip Macy 	error = sys_recvfrom(td, &bsd_args);
11015c8919adSAlexander Leidinger 	bsd_to_linux_sockaddr((struct sockaddr *)bsd_args.from);
11025c8919adSAlexander Leidinger 
1103ca26842eSHajimu UMEMOTO 	if (error)
1104ca26842eSHajimu UMEMOTO 		return (error);
1105745aaef5SKonstantin Belousov 	if (args->from) {
11064af27623STim J. Robbins 		error = linux_sa_put((struct osockaddr *)
1107745aaef5SKonstantin Belousov 		    PTRIN(args->from));
1108ca26842eSHajimu UMEMOTO 		if (error)
1109ca26842eSHajimu UMEMOTO 			return (error);
1110ca26842eSHajimu UMEMOTO 	}
1111ca26842eSHajimu UMEMOTO 	return (0);
1112ca26842eSHajimu UMEMOTO }
1113ca26842eSHajimu UMEMOTO 
111401e0ffbaSAlexander Leidinger struct linux_sendmsg_args {
1115ca26842eSHajimu UMEMOTO 	int s;
11164af27623STim J. Robbins 	l_uintptr_t msg;
1117ca26842eSHajimu UMEMOTO 	int flags;
1118ca26842eSHajimu UMEMOTO };
1119ca26842eSHajimu UMEMOTO 
112001e0ffbaSAlexander Leidinger static int
1121ca26842eSHajimu UMEMOTO linux_sendmsg(struct thread *td, struct linux_sendmsg_args *args)
1122ca26842eSHajimu UMEMOTO {
112374f5d680SKonstantin Belousov 	struct cmsghdr *cmsg;
1124605da56bSAndriy Gapon 	struct cmsgcred cmcred;
112574f5d680SKonstantin Belousov 	struct mbuf *control;
1126ca26842eSHajimu UMEMOTO 	struct msghdr msg;
112774f5d680SKonstantin Belousov 	struct l_cmsghdr linux_cmsg;
112874f5d680SKonstantin Belousov 	struct l_cmsghdr *ptr_cmsg;
112974f5d680SKonstantin Belousov 	struct l_msghdr linux_msg;
1130552afd9cSPoul-Henning Kamp 	struct iovec *iov;
113174f5d680SKonstantin Belousov 	socklen_t datalen;
1132605da56bSAndriy Gapon 	struct sockaddr *sa;
1133605da56bSAndriy Gapon 	sa_family_t sa_family;
113474f5d680SKonstantin Belousov 	void *data;
1135ca26842eSHajimu UMEMOTO 	int error;
1136ca26842eSHajimu UMEMOTO 
113774f5d680SKonstantin Belousov 	error = copyin(PTRIN(args->msg), &linux_msg, sizeof(linux_msg));
113874f5d680SKonstantin Belousov 	if (error)
113974f5d680SKonstantin Belousov 		return (error);
1140d72a6158SRobert Watson 
1141d72a6158SRobert Watson 	/*
1142d72a6158SRobert Watson 	 * Some Linux applications (ping) define a non-NULL control data
1143d72a6158SRobert Watson 	 * pointer, but a msg_controllen of 0, which is not allowed in the
1144d72a6158SRobert Watson 	 * FreeBSD system call interface.  NULL the msg_control pointer in
1145d72a6158SRobert Watson 	 * order to handle this case.  This should be checked, but allows the
1146d72a6158SRobert Watson 	 * Linux ping to work.
1147d72a6158SRobert Watson 	 */
1148605da56bSAndriy Gapon 	if (PTRIN(linux_msg.msg_control) != NULL && linux_msg.msg_controllen == 0)
1149605da56bSAndriy Gapon 		linux_msg.msg_control = PTROUT(NULL);
1150605da56bSAndriy Gapon 
1151605da56bSAndriy Gapon 	error = linux_to_bsd_msghdr(&msg, &linux_msg);
1152605da56bSAndriy Gapon 	if (error)
1153605da56bSAndriy Gapon 		return (error);
115474f5d680SKonstantin Belousov 
115574f5d680SKonstantin Belousov #ifdef COMPAT_LINUX32
115674f5d680SKonstantin Belousov 	error = linux32_copyiniov(PTRIN(msg.msg_iov), msg.msg_iovlen,
115774f5d680SKonstantin Belousov 	    &iov, EMSGSIZE);
115874f5d680SKonstantin Belousov #else
1159552afd9cSPoul-Henning Kamp 	error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
116074f5d680SKonstantin Belousov #endif
1161552afd9cSPoul-Henning Kamp 	if (error)
1162552afd9cSPoul-Henning Kamp 		return (error);
116374f5d680SKonstantin Belousov 
1164605da56bSAndriy Gapon 	control = NULL;
1165605da56bSAndriy Gapon 	cmsg = NULL;
1166605da56bSAndriy Gapon 
1167605da56bSAndriy Gapon 	if ((ptr_cmsg = LINUX_CMSG_FIRSTHDR(&linux_msg)) != NULL) {
1168605da56bSAndriy Gapon 		error = kern_getsockname(td, args->s, &sa, &datalen);
1169605da56bSAndriy Gapon 		if (error)
1170605da56bSAndriy Gapon 			goto bad;
1171605da56bSAndriy Gapon 		sa_family = sa->sa_family;
1172605da56bSAndriy Gapon 		free(sa, M_SONAME);
1173605da56bSAndriy Gapon 
117474f5d680SKonstantin Belousov 		error = ENOBUFS;
117574f5d680SKonstantin Belousov 		cmsg = malloc(CMSG_HDRSZ, M_TEMP, M_WAITOK | M_ZERO);
117674f5d680SKonstantin Belousov 		control = m_get(M_WAIT, MT_CONTROL);
117774f5d680SKonstantin Belousov 		if (control == NULL)
117874f5d680SKonstantin Belousov 			goto bad;
117974f5d680SKonstantin Belousov 
118074f5d680SKonstantin Belousov 		do {
118174f5d680SKonstantin Belousov 			error = copyin(ptr_cmsg, &linux_cmsg,
118274f5d680SKonstantin Belousov 			    sizeof(struct l_cmsghdr));
118374f5d680SKonstantin Belousov 			if (error)
118474f5d680SKonstantin Belousov 				goto bad;
118574f5d680SKonstantin Belousov 
118674f5d680SKonstantin Belousov 			error = EINVAL;
118774f5d680SKonstantin Belousov 			if (linux_cmsg.cmsg_len < sizeof(struct l_cmsghdr))
118874f5d680SKonstantin Belousov 				goto bad;
118974f5d680SKonstantin Belousov 
119074f5d680SKonstantin Belousov 			/*
1191605da56bSAndriy Gapon 			 * Now we support only SCM_RIGHTS and SCM_CRED,
1192605da56bSAndriy Gapon 			 * so return EINVAL in any other cmsg_type
119374f5d680SKonstantin Belousov 			 */
1194605da56bSAndriy Gapon 			cmsg->cmsg_type =
1195605da56bSAndriy Gapon 			    linux_to_bsd_cmsg_type(linux_cmsg.cmsg_type);
119674f5d680SKonstantin Belousov 			cmsg->cmsg_level =
119774f5d680SKonstantin Belousov 			    linux_to_bsd_sockopt_level(linux_cmsg.cmsg_level);
1198605da56bSAndriy Gapon 			if (cmsg->cmsg_type == -1
1199605da56bSAndriy Gapon 			    || cmsg->cmsg_level != SOL_SOCKET)
1200605da56bSAndriy Gapon 				goto bad;
120174f5d680SKonstantin Belousov 
1202605da56bSAndriy Gapon 			/*
1203605da56bSAndriy Gapon 			 * Some applications (e.g. pulseaudio) attempt to
1204605da56bSAndriy Gapon 			 * send ancillary data even if the underlying protocol
1205605da56bSAndriy Gapon 			 * doesn't support it which is not allowed in the
1206605da56bSAndriy Gapon 			 * FreeBSD system call interface.
1207605da56bSAndriy Gapon 			 */
1208605da56bSAndriy Gapon 			if (sa_family != AF_UNIX)
1209605da56bSAndriy Gapon 				continue;
1210605da56bSAndriy Gapon 
121174f5d680SKonstantin Belousov 			data = LINUX_CMSG_DATA(ptr_cmsg);
1212605da56bSAndriy Gapon 			datalen = linux_cmsg.cmsg_len - L_CMSG_HDRSZ;
1213605da56bSAndriy Gapon 
1214605da56bSAndriy Gapon 			switch (cmsg->cmsg_type)
1215605da56bSAndriy Gapon 			{
1216605da56bSAndriy Gapon 			case SCM_RIGHTS:
1217605da56bSAndriy Gapon 				break;
1218605da56bSAndriy Gapon 
1219605da56bSAndriy Gapon 			case SCM_CREDS:
1220605da56bSAndriy Gapon 				data = &cmcred;
1221605da56bSAndriy Gapon 				datalen = sizeof(cmcred);
1222605da56bSAndriy Gapon 
1223605da56bSAndriy Gapon 				/*
1224605da56bSAndriy Gapon 				 * The lower levels will fill in the structure
1225605da56bSAndriy Gapon 				 */
1226605da56bSAndriy Gapon 				bzero(data, datalen);
1227605da56bSAndriy Gapon 				break;
1228605da56bSAndriy Gapon 			}
1229605da56bSAndriy Gapon 
1230605da56bSAndriy Gapon 			cmsg->cmsg_len = CMSG_LEN(datalen);
123174f5d680SKonstantin Belousov 
123274f5d680SKonstantin Belousov 			error = ENOBUFS;
123374f5d680SKonstantin Belousov 			if (!m_append(control, CMSG_HDRSZ, (c_caddr_t)cmsg))
123474f5d680SKonstantin Belousov 				goto bad;
123574f5d680SKonstantin Belousov 			if (!m_append(control, datalen, (c_caddr_t)data))
123674f5d680SKonstantin Belousov 				goto bad;
1237605da56bSAndriy Gapon 		} while ((ptr_cmsg = LINUX_CMSG_NXTHDR(&linux_msg, ptr_cmsg)));
1238605da56bSAndriy Gapon 
1239605da56bSAndriy Gapon 		if (m_length(control, NULL) == 0) {
1240605da56bSAndriy Gapon 			m_freem(control);
124174f5d680SKonstantin Belousov 			control = NULL;
1242605da56bSAndriy Gapon 		}
124374f5d680SKonstantin Belousov 	}
124474f5d680SKonstantin Belousov 
12455a8a13e0SDavid Malone 	msg.msg_iov = iov;
12465a8a13e0SDavid Malone 	msg.msg_flags = 0;
124774f5d680SKonstantin Belousov 	error = linux_sendit(td, args->s, &msg, args->flags, control,
124874f5d680SKonstantin Belousov 	    UIO_USERSPACE);
124974f5d680SKonstantin Belousov 
125074f5d680SKonstantin Belousov bad:
1251552afd9cSPoul-Henning Kamp 	free(iov, M_IOV);
125274f5d680SKonstantin Belousov 	if (cmsg)
125374f5d680SKonstantin Belousov 		free(cmsg, M_TEMP);
1254ca26842eSHajimu UMEMOTO 	return (error);
1255c21dee17SSøren Schmidt }
1256c21dee17SSøren Schmidt 
125701e0ffbaSAlexander Leidinger struct linux_recvmsg_args {
125840dbba57SAssar Westerlund 	int s;
12594af27623STim J. Robbins 	l_uintptr_t msg;
126040dbba57SAssar Westerlund 	int flags;
126140dbba57SAssar Westerlund };
126240dbba57SAssar Westerlund 
126301e0ffbaSAlexander Leidinger static int
1264b40ce416SJulian Elischer linux_recvmsg(struct thread *td, struct linux_recvmsg_args *args)
126540dbba57SAssar Westerlund {
126674f5d680SKonstantin Belousov 	struct cmsghdr *cm;
1267605da56bSAndriy Gapon 	struct cmsgcred *cmcred;
1268ca26842eSHajimu UMEMOTO 	struct msghdr msg;
126974f5d680SKonstantin Belousov 	struct l_cmsghdr *linux_cmsg = NULL;
1270605da56bSAndriy Gapon 	struct l_ucred linux_ucred;
1271605da56bSAndriy Gapon 	socklen_t datalen, outlen;
127274f5d680SKonstantin Belousov 	struct l_msghdr linux_msg;
127374f5d680SKonstantin Belousov 	struct iovec *iov, *uiov;
127474f5d680SKonstantin Belousov 	struct mbuf *control = NULL;
127574f5d680SKonstantin Belousov 	struct mbuf **controlp;
127674f5d680SKonstantin Belousov 	caddr_t outbuf;
127774f5d680SKonstantin Belousov 	void *data;
12783a72bf04SDmitry Chagin 	int error, i, fd, fds, *fdp;
127940dbba57SAssar Westerlund 
128074f5d680SKonstantin Belousov 	error = copyin(PTRIN(args->msg), &linux_msg, sizeof(linux_msg));
1281ca26842eSHajimu UMEMOTO 	if (error)
1282ca26842eSHajimu UMEMOTO 		return (error);
1283ca26842eSHajimu UMEMOTO 
128474f5d680SKonstantin Belousov 	error = linux_to_bsd_msghdr(&msg, &linux_msg);
128574f5d680SKonstantin Belousov 	if (error)
128674f5d680SKonstantin Belousov 		return (error);
128774f5d680SKonstantin Belousov 
128874f5d680SKonstantin Belousov #ifdef COMPAT_LINUX32
128974f5d680SKonstantin Belousov 	error = linux32_copyiniov(PTRIN(msg.msg_iov), msg.msg_iovlen,
129074f5d680SKonstantin Belousov 	    &iov, EMSGSIZE);
129174f5d680SKonstantin Belousov #else
129274f5d680SKonstantin Belousov 	error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
129374f5d680SKonstantin Belousov #endif
129474f5d680SKonstantin Belousov 	if (error)
129574f5d680SKonstantin Belousov 		return (error);
129674f5d680SKonstantin Belousov 
129774f5d680SKonstantin Belousov 	if (msg.msg_name) {
129874f5d680SKonstantin Belousov 		error = linux_to_bsd_sockaddr((struct sockaddr *)msg.msg_name,
129974f5d680SKonstantin Belousov 		    msg.msg_namelen);
130074f5d680SKonstantin Belousov 		if (error)
130174f5d680SKonstantin Belousov 			goto bad;
130284b11cd8SMitsuru IWASAKI 	}
130384b11cd8SMitsuru IWASAKI 
130474f5d680SKonstantin Belousov 	uiov = msg.msg_iov;
130574f5d680SKonstantin Belousov 	msg.msg_iov = iov;
130674f5d680SKonstantin Belousov 	controlp = (msg.msg_control != NULL) ? &control : NULL;
130774f5d680SKonstantin Belousov 	error = kern_recvit(td, args->s, &msg, UIO_USERSPACE, controlp);
130874f5d680SKonstantin Belousov 	msg.msg_iov = uiov;
1309ca26842eSHajimu UMEMOTO 	if (error)
131074f5d680SKonstantin Belousov 		goto bad;
131174f5d680SKonstantin Belousov 
131274f5d680SKonstantin Belousov 	error = bsd_to_linux_msghdr(&msg, &linux_msg);
131374f5d680SKonstantin Belousov 	if (error)
131474f5d680SKonstantin Belousov 		goto bad;
131574f5d680SKonstantin Belousov 
131674f5d680SKonstantin Belousov 	if (linux_msg.msg_name) {
131774f5d680SKonstantin Belousov 		error = bsd_to_linux_sockaddr((struct sockaddr *)
131874f5d680SKonstantin Belousov 		    PTRIN(linux_msg.msg_name));
131974f5d680SKonstantin Belousov 		if (error)
132074f5d680SKonstantin Belousov 			goto bad;
132174f5d680SKonstantin Belousov 	}
132274f5d680SKonstantin Belousov 	if (linux_msg.msg_name && linux_msg.msg_namelen > 2) {
132374f5d680SKonstantin Belousov 		error = linux_sa_put(PTRIN(linux_msg.msg_name));
132474f5d680SKonstantin Belousov 		if (error)
132574f5d680SKonstantin Belousov 			goto bad;
132674f5d680SKonstantin Belousov 	}
132774f5d680SKonstantin Belousov 
132874f5d680SKonstantin Belousov 	outbuf = PTRIN(linux_msg.msg_control);
132974f5d680SKonstantin Belousov 	outlen = 0;
1330605da56bSAndriy Gapon 
1331605da56bSAndriy Gapon 	if (control) {
1332605da56bSAndriy Gapon 		linux_cmsg = malloc(L_CMSG_HDRSZ, M_TEMP, M_WAITOK | M_ZERO);
1333605da56bSAndriy Gapon 
1334605da56bSAndriy Gapon 		msg.msg_control = mtod(control, struct cmsghdr *);
1335605da56bSAndriy Gapon 		msg.msg_controllen = control->m_len;
1336605da56bSAndriy Gapon 
1337605da56bSAndriy Gapon 		cm = CMSG_FIRSTHDR(&msg);
133874f5d680SKonstantin Belousov 
133974f5d680SKonstantin Belousov 		while (cm != NULL) {
1340605da56bSAndriy Gapon 			linux_cmsg->cmsg_type =
1341605da56bSAndriy Gapon 			    bsd_to_linux_cmsg_type(cm->cmsg_type);
1342605da56bSAndriy Gapon 			linux_cmsg->cmsg_level =
1343605da56bSAndriy Gapon 			    bsd_to_linux_sockopt_level(cm->cmsg_level);
1344605da56bSAndriy Gapon 			if (linux_cmsg->cmsg_type == -1
1345605da56bSAndriy Gapon 			    || cm->cmsg_level != SOL_SOCKET)
134674f5d680SKonstantin Belousov 			{
134774f5d680SKonstantin Belousov 				error = EINVAL;
134874f5d680SKonstantin Belousov 				goto bad;
134974f5d680SKonstantin Belousov 			}
1350605da56bSAndriy Gapon 
135174f5d680SKonstantin Belousov 			data = CMSG_DATA(cm);
135274f5d680SKonstantin Belousov 			datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
135374f5d680SKonstantin Belousov 
1354605da56bSAndriy Gapon 			switch (cm->cmsg_type)
13553a72bf04SDmitry Chagin 			{
1356605da56bSAndriy Gapon 			case SCM_RIGHTS:
1357605da56bSAndriy Gapon 				if (args->flags & LINUX_MSG_CMSG_CLOEXEC) {
1358605da56bSAndriy Gapon 					fds = datalen / sizeof(int);
1359605da56bSAndriy Gapon 					fdp = data;
1360605da56bSAndriy Gapon 					for (i = 0; i < fds; i++) {
1361605da56bSAndriy Gapon 						fd = *fdp++;
1362605da56bSAndriy Gapon 						(void)kern_fcntl(td, fd,
1363605da56bSAndriy Gapon 						    F_SETFD, FD_CLOEXEC);
1364605da56bSAndriy Gapon 					}
1365605da56bSAndriy Gapon 				}
1366605da56bSAndriy Gapon 				break;
1367605da56bSAndriy Gapon 
1368605da56bSAndriy Gapon 			case SCM_CREDS:
1369605da56bSAndriy Gapon 				/*
1370605da56bSAndriy Gapon 				 * Currently LOCAL_CREDS is never in
1371605da56bSAndriy Gapon 				 * effect for Linux so no need to worry
1372605da56bSAndriy Gapon 				 * about sockcred
1373605da56bSAndriy Gapon 				 */
1374605da56bSAndriy Gapon 				if (datalen != sizeof(*cmcred)) {
1375605da56bSAndriy Gapon 					error = EMSGSIZE;
1376605da56bSAndriy Gapon 					goto bad;
1377605da56bSAndriy Gapon 				}
1378605da56bSAndriy Gapon 				cmcred = (struct cmsgcred *)data;
1379605da56bSAndriy Gapon 				bzero(&linux_ucred, sizeof(linux_ucred));
1380605da56bSAndriy Gapon 				linux_ucred.pid = cmcred->cmcred_pid;
1381605da56bSAndriy Gapon 				linux_ucred.uid = cmcred->cmcred_uid;
1382605da56bSAndriy Gapon 				linux_ucred.gid = cmcred->cmcred_gid;
1383605da56bSAndriy Gapon 				data = &linux_ucred;
1384605da56bSAndriy Gapon 				datalen = sizeof(linux_ucred);
1385605da56bSAndriy Gapon 				break;
1386605da56bSAndriy Gapon 			}
1387605da56bSAndriy Gapon 
138874f5d680SKonstantin Belousov 			if (outlen + LINUX_CMSG_LEN(datalen) >
138974f5d680SKonstantin Belousov 			    linux_msg.msg_controllen) {
139074f5d680SKonstantin Belousov 				if (outlen == 0) {
139174f5d680SKonstantin Belousov 					error = EMSGSIZE;
139274f5d680SKonstantin Belousov 					goto bad;
139374f5d680SKonstantin Belousov 				} else {
13943a72bf04SDmitry Chagin 					linux_msg.msg_flags |=
13953a72bf04SDmitry Chagin 					    LINUX_MSG_CTRUNC;
139674f5d680SKonstantin Belousov 					goto out;
139774f5d680SKonstantin Belousov 				}
139874f5d680SKonstantin Belousov 			}
139974f5d680SKonstantin Belousov 
140074f5d680SKonstantin Belousov 			linux_cmsg->cmsg_len = LINUX_CMSG_LEN(datalen);
140174f5d680SKonstantin Belousov 
140274f5d680SKonstantin Belousov 			error = copyout(linux_cmsg, outbuf, L_CMSG_HDRSZ);
140374f5d680SKonstantin Belousov 			if (error)
140474f5d680SKonstantin Belousov 				goto bad;
140574f5d680SKonstantin Belousov 			outbuf += L_CMSG_HDRSZ;
140674f5d680SKonstantin Belousov 
140774f5d680SKonstantin Belousov 			error = copyout(data, outbuf, datalen);
140874f5d680SKonstantin Belousov 			if (error)
140974f5d680SKonstantin Belousov 				goto bad;
141074f5d680SKonstantin Belousov 
141174f5d680SKonstantin Belousov 			outbuf += LINUX_CMSG_ALIGN(datalen);
141274f5d680SKonstantin Belousov 			outlen += LINUX_CMSG_LEN(datalen);
141374f5d680SKonstantin Belousov 
1414605da56bSAndriy Gapon 			cm = CMSG_NXTHDR(&msg, cm);
141574f5d680SKonstantin Belousov 		}
141674f5d680SKonstantin Belousov 	}
141774f5d680SKonstantin Belousov 
141874f5d680SKonstantin Belousov out:
1419605da56bSAndriy Gapon 	linux_msg.msg_controllen = outlen;
142074f5d680SKonstantin Belousov 	error = copyout(&linux_msg, PTRIN(args->msg), sizeof(linux_msg));
142174f5d680SKonstantin Belousov 
142274f5d680SKonstantin Belousov bad:
142374f5d680SKonstantin Belousov 	free(iov, M_IOV);
142474f5d680SKonstantin Belousov 	if (control != NULL)
142574f5d680SKonstantin Belousov 		m_freem(control);
142674f5d680SKonstantin Belousov 	if (linux_cmsg != NULL)
142774f5d680SKonstantin Belousov 		free(linux_cmsg, M_TEMP);
142874f5d680SKonstantin Belousov 
1429ca26842eSHajimu UMEMOTO 	return (error);
143040dbba57SAssar Westerlund }
143140dbba57SAssar Westerlund 
1432c21dee17SSøren Schmidt struct linux_shutdown_args {
1433c21dee17SSøren Schmidt 	int s;
1434c21dee17SSøren Schmidt 	int how;
1435c21dee17SSøren Schmidt };
1436c21dee17SSøren Schmidt 
1437c21dee17SSøren Schmidt static int
1438b40ce416SJulian Elischer linux_shutdown(struct thread *td, struct linux_shutdown_args *args)
1439c21dee17SSøren Schmidt {
1440ef04503dSPeter Wemm 	struct shutdown_args /* {
1441c21dee17SSøren Schmidt 		int s;
1442c21dee17SSøren Schmidt 		int how;
1443ef04503dSPeter Wemm 	} */ bsd_args;
1444c21dee17SSøren Schmidt 
1445745aaef5SKonstantin Belousov 	bsd_args.s = args->s;
1446745aaef5SKonstantin Belousov 	bsd_args.how = args->how;
14478451d0ddSKip Macy 	return (sys_shutdown(td, &bsd_args));
1448c21dee17SSøren Schmidt }
1449c21dee17SSøren Schmidt 
1450c21dee17SSøren Schmidt struct linux_setsockopt_args {
1451c21dee17SSøren Schmidt 	int s;
1452c21dee17SSøren Schmidt 	int level;
1453c21dee17SSøren Schmidt 	int optname;
14544af27623STim J. Robbins 	l_uintptr_t optval;
1455c21dee17SSøren Schmidt 	int optlen;
1456c21dee17SSøren Schmidt };
1457c21dee17SSøren Schmidt 
1458c21dee17SSøren Schmidt static int
1459b40ce416SJulian Elischer linux_setsockopt(struct thread *td, struct linux_setsockopt_args *args)
1460c21dee17SSøren Schmidt {
1461ef04503dSPeter Wemm 	struct setsockopt_args /* {
1462c21dee17SSøren Schmidt 		int s;
1463c21dee17SSøren Schmidt 		int level;
1464c21dee17SSøren Schmidt 		int name;
1465c21dee17SSøren Schmidt 		caddr_t val;
1466c21dee17SSøren Schmidt 		int valsize;
1467ef04503dSPeter Wemm 	} */ bsd_args;
146803cc95d2SDmitry Chagin 	l_timeval linux_tv;
146903cc95d2SDmitry Chagin 	struct timeval tv;
1470c21dee17SSøren Schmidt 	int error, name;
1471c21dee17SSøren Schmidt 
1472745aaef5SKonstantin Belousov 	bsd_args.s = args->s;
1473745aaef5SKonstantin Belousov 	bsd_args.level = linux_to_bsd_sockopt_level(args->level);
1474c21dee17SSøren Schmidt 	switch (bsd_args.level) {
1475c21dee17SSøren Schmidt 	case SOL_SOCKET:
1476745aaef5SKonstantin Belousov 		name = linux_to_bsd_so_sockopt(args->optname);
147703cc95d2SDmitry Chagin 		switch (name) {
147803cc95d2SDmitry Chagin 		case SO_RCVTIMEO:
147903cc95d2SDmitry Chagin 			/* FALLTHROUGH */
148003cc95d2SDmitry Chagin 		case SO_SNDTIMEO:
148103cc95d2SDmitry Chagin 			error = copyin(PTRIN(args->optval), &linux_tv,
148203cc95d2SDmitry Chagin 			    sizeof(linux_tv));
148303cc95d2SDmitry Chagin 			if (error)
148403cc95d2SDmitry Chagin 				return (error);
148503cc95d2SDmitry Chagin 			tv.tv_sec = linux_tv.tv_sec;
148603cc95d2SDmitry Chagin 			tv.tv_usec = linux_tv.tv_usec;
148703cc95d2SDmitry Chagin 			return (kern_setsockopt(td, args->s, bsd_args.level,
148803cc95d2SDmitry Chagin 			    name, &tv, UIO_SYSSPACE, sizeof(tv)));
148903cc95d2SDmitry Chagin 			/* NOTREACHED */
149003cc95d2SDmitry Chagin 			break;
149103cc95d2SDmitry Chagin 		default:
149203cc95d2SDmitry Chagin 			break;
149303cc95d2SDmitry Chagin 		}
1494c21dee17SSøren Schmidt 		break;
1495c21dee17SSøren Schmidt 	case IPPROTO_IP:
1496745aaef5SKonstantin Belousov 		name = linux_to_bsd_ip_sockopt(args->optname);
1497c21dee17SSøren Schmidt 		break;
1498dad3b88aSMike Smith 	case IPPROTO_TCP:
1499dad3b88aSMike Smith 		/* Linux TCP option values match BSD's */
1500745aaef5SKonstantin Belousov 		name = args->optname;
1501dad3b88aSMike Smith 		break;
1502c21dee17SSøren Schmidt 	default:
15033f3a4815SMarcel Moolenaar 		name = -1;
15043f3a4815SMarcel Moolenaar 		break;
1505c21dee17SSøren Schmidt 	}
1506c21dee17SSøren Schmidt 	if (name == -1)
1507d4b7423fSAlexander Leidinger 		return (ENOPROTOOPT);
15083f3a4815SMarcel Moolenaar 
1509c21dee17SSøren Schmidt 	bsd_args.name = name;
1510745aaef5SKonstantin Belousov 	bsd_args.val = PTRIN(args->optval);
1511745aaef5SKonstantin Belousov 	bsd_args.valsize = args->optlen;
15125c8919adSAlexander Leidinger 
15135c8919adSAlexander Leidinger 	if (name == IPV6_NEXTHOP) {
15145c8919adSAlexander Leidinger 		linux_to_bsd_sockaddr((struct sockaddr *)bsd_args.val,
15155c8919adSAlexander Leidinger 			bsd_args.valsize);
15168451d0ddSKip Macy 		error = sys_setsockopt(td, &bsd_args);
15175c8919adSAlexander Leidinger 		bsd_to_linux_sockaddr((struct sockaddr *)bsd_args.val);
15185c8919adSAlexander Leidinger 	} else
15198451d0ddSKip Macy 		error = sys_setsockopt(td, &bsd_args);
15205c8919adSAlexander Leidinger 
15215c8919adSAlexander Leidinger 	return (error);
1522c21dee17SSøren Schmidt }
1523c21dee17SSøren Schmidt 
1524c21dee17SSøren Schmidt struct linux_getsockopt_args {
1525c21dee17SSøren Schmidt 	int s;
1526c21dee17SSøren Schmidt 	int level;
1527c21dee17SSøren Schmidt 	int optname;
15284af27623STim J. Robbins 	l_uintptr_t optval;
15294af27623STim J. Robbins 	l_uintptr_t optlen;
1530c21dee17SSøren Schmidt };
1531c21dee17SSøren Schmidt 
1532c21dee17SSøren Schmidt static int
1533b40ce416SJulian Elischer linux_getsockopt(struct thread *td, struct linux_getsockopt_args *args)
1534c21dee17SSøren Schmidt {
1535ef04503dSPeter Wemm 	struct getsockopt_args /* {
1536c21dee17SSøren Schmidt 		int s;
1537c21dee17SSøren Schmidt 		int level;
1538c21dee17SSøren Schmidt 		int name;
1539c21dee17SSøren Schmidt 		caddr_t val;
1540c21dee17SSøren Schmidt 		int *avalsize;
1541ef04503dSPeter Wemm 	} */ bsd_args;
154203cc95d2SDmitry Chagin 	l_timeval linux_tv;
154303cc95d2SDmitry Chagin 	struct timeval tv;
1544d4dd69c4SDmitry Chagin 	socklen_t tv_len, xulen;
1545d4dd69c4SDmitry Chagin 	struct xucred xu;
1546d4dd69c4SDmitry Chagin 	struct l_ucred lxu;
1547c21dee17SSøren Schmidt 	int error, name;
1548c21dee17SSøren Schmidt 
1549745aaef5SKonstantin Belousov 	bsd_args.s = args->s;
1550745aaef5SKonstantin Belousov 	bsd_args.level = linux_to_bsd_sockopt_level(args->level);
1551c21dee17SSøren Schmidt 	switch (bsd_args.level) {
1552c21dee17SSøren Schmidt 	case SOL_SOCKET:
1553745aaef5SKonstantin Belousov 		name = linux_to_bsd_so_sockopt(args->optname);
155403cc95d2SDmitry Chagin 		switch (name) {
155503cc95d2SDmitry Chagin 		case SO_RCVTIMEO:
155603cc95d2SDmitry Chagin 			/* FALLTHROUGH */
155703cc95d2SDmitry Chagin 		case SO_SNDTIMEO:
155803cc95d2SDmitry Chagin 			tv_len = sizeof(tv);
155903cc95d2SDmitry Chagin 			error = kern_getsockopt(td, args->s, bsd_args.level,
156003cc95d2SDmitry Chagin 			    name, &tv, UIO_SYSSPACE, &tv_len);
156103cc95d2SDmitry Chagin 			if (error)
156203cc95d2SDmitry Chagin 				return (error);
156303cc95d2SDmitry Chagin 			linux_tv.tv_sec = tv.tv_sec;
156403cc95d2SDmitry Chagin 			linux_tv.tv_usec = tv.tv_usec;
156503cc95d2SDmitry Chagin 			return (copyout(&linux_tv, PTRIN(args->optval),
156603cc95d2SDmitry Chagin 			    sizeof(linux_tv)));
156703cc95d2SDmitry Chagin 			/* NOTREACHED */
156803cc95d2SDmitry Chagin 			break;
1569d4dd69c4SDmitry Chagin 		case LOCAL_PEERCRED:
1570d4dd69c4SDmitry Chagin 			if (args->optlen != sizeof(lxu))
1571d4dd69c4SDmitry Chagin 				return (EINVAL);
1572d4dd69c4SDmitry Chagin 			xulen = sizeof(xu);
1573d4dd69c4SDmitry Chagin 			error = kern_getsockopt(td, args->s, bsd_args.level,
1574d4dd69c4SDmitry Chagin 			    name, &xu, UIO_SYSSPACE, &xulen);
1575d4dd69c4SDmitry Chagin 			if (error)
1576d4dd69c4SDmitry Chagin 				return (error);
1577d4dd69c4SDmitry Chagin 			/*
1578d4dd69c4SDmitry Chagin 			 * XXX Use 0 for pid as the FreeBSD does not cache peer pid.
1579d4dd69c4SDmitry Chagin 			 */
1580d4dd69c4SDmitry Chagin 			lxu.pid = 0;
1581d4dd69c4SDmitry Chagin 			lxu.uid = xu.cr_uid;
1582d4dd69c4SDmitry Chagin 			lxu.gid = xu.cr_gid;
1583d4dd69c4SDmitry Chagin 			return (copyout(&lxu, PTRIN(args->optval), sizeof(lxu)));
1584d4dd69c4SDmitry Chagin 			/* NOTREACHED */
1585d4dd69c4SDmitry Chagin 			break;
158603cc95d2SDmitry Chagin 		default:
158703cc95d2SDmitry Chagin 			break;
158803cc95d2SDmitry Chagin 		}
1589c21dee17SSøren Schmidt 		break;
1590c21dee17SSøren Schmidt 	case IPPROTO_IP:
1591745aaef5SKonstantin Belousov 		name = linux_to_bsd_ip_sockopt(args->optname);
1592c21dee17SSøren Schmidt 		break;
1593dad3b88aSMike Smith 	case IPPROTO_TCP:
1594dad3b88aSMike Smith 		/* Linux TCP option values match BSD's */
1595745aaef5SKonstantin Belousov 		name = args->optname;
1596dad3b88aSMike Smith 		break;
1597c21dee17SSøren Schmidt 	default:
15983f3a4815SMarcel Moolenaar 		name = -1;
15993f3a4815SMarcel Moolenaar 		break;
1600c21dee17SSøren Schmidt 	}
1601c21dee17SSøren Schmidt 	if (name == -1)
16023f3a4815SMarcel Moolenaar 		return (EINVAL);
16033f3a4815SMarcel Moolenaar 
1604f2477ae1SMike Smith 	bsd_args.name = name;
1605745aaef5SKonstantin Belousov 	bsd_args.val = PTRIN(args->optval);
1606745aaef5SKonstantin Belousov 	bsd_args.avalsize = PTRIN(args->optlen);
16075c8919adSAlexander Leidinger 
16085c8919adSAlexander Leidinger 	if (name == IPV6_NEXTHOP) {
16098451d0ddSKip Macy 		error = sys_getsockopt(td, &bsd_args);
16105c8919adSAlexander Leidinger 		bsd_to_linux_sockaddr((struct sockaddr *)bsd_args.val);
16115c8919adSAlexander Leidinger 	} else
16128451d0ddSKip Macy 		error = sys_getsockopt(td, &bsd_args);
16135c8919adSAlexander Leidinger 
16145c8919adSAlexander Leidinger 	return (error);
1615c21dee17SSøren Schmidt }
1616c21dee17SSøren Schmidt 
1617ea7b81d2SDmitry Chagin /* Argument list sizes for linux_socketcall */
1618ea7b81d2SDmitry Chagin 
1619ea7b81d2SDmitry Chagin #define LINUX_AL(x) ((x) * sizeof(l_ulong))
1620ea7b81d2SDmitry Chagin 
1621ea7b81d2SDmitry Chagin static const unsigned char lxs_args[] = {
1622ea7b81d2SDmitry Chagin 	LINUX_AL(0) /* unused*/,	LINUX_AL(3) /* socket */,
1623ea7b81d2SDmitry Chagin 	LINUX_AL(3) /* bind */,		LINUX_AL(3) /* connect */,
1624ea7b81d2SDmitry Chagin 	LINUX_AL(2) /* listen */,	LINUX_AL(3) /* accept */,
1625ea7b81d2SDmitry Chagin 	LINUX_AL(3) /* getsockname */,	LINUX_AL(3) /* getpeername */,
1626ea7b81d2SDmitry Chagin 	LINUX_AL(4) /* socketpair */,	LINUX_AL(4) /* send */,
1627ea7b81d2SDmitry Chagin 	LINUX_AL(4) /* recv */,		LINUX_AL(6) /* sendto */,
1628ea7b81d2SDmitry Chagin 	LINUX_AL(6) /* recvfrom */,	LINUX_AL(2) /* shutdown */,
1629ea7b81d2SDmitry Chagin 	LINUX_AL(5) /* setsockopt */,	LINUX_AL(5) /* getsockopt */,
1630f8cd0af2SDmitry Chagin 	LINUX_AL(3) /* sendmsg */,	LINUX_AL(3) /* recvmsg */,
1631f8cd0af2SDmitry Chagin 	LINUX_AL(4) /* accept4 */
1632ea7b81d2SDmitry Chagin };
1633ea7b81d2SDmitry Chagin 
1634ea7b81d2SDmitry Chagin #define	LINUX_AL_SIZE	sizeof(lxs_args) / sizeof(lxs_args[0]) - 1
1635ea7b81d2SDmitry Chagin 
1636c21dee17SSøren Schmidt int
1637b40ce416SJulian Elischer linux_socketcall(struct thread *td, struct linux_socketcall_args *args)
1638c21dee17SSøren Schmidt {
1639ea7b81d2SDmitry Chagin 	l_ulong a[6];
1640ea7b81d2SDmitry Chagin 	void *arg;
1641ea7b81d2SDmitry Chagin 	int error;
16423f3a4815SMarcel Moolenaar 
1643ea7b81d2SDmitry Chagin 	if (args->what < LINUX_SOCKET || args->what > LINUX_AL_SIZE)
1644ea7b81d2SDmitry Chagin 		return (EINVAL);
1645ea7b81d2SDmitry Chagin 	error = copyin(PTRIN(args->args), a, lxs_args[args->what]);
1646ea7b81d2SDmitry Chagin 	if (error)
1647ea7b81d2SDmitry Chagin 		return (error);
1648ea7b81d2SDmitry Chagin 
1649ea7b81d2SDmitry Chagin 	arg = a;
1650c21dee17SSøren Schmidt 	switch (args->what) {
1651c21dee17SSøren Schmidt 	case LINUX_SOCKET:
1652b40ce416SJulian Elischer 		return (linux_socket(td, arg));
1653c21dee17SSøren Schmidt 	case LINUX_BIND:
1654b40ce416SJulian Elischer 		return (linux_bind(td, arg));
1655c21dee17SSøren Schmidt 	case LINUX_CONNECT:
1656b40ce416SJulian Elischer 		return (linux_connect(td, arg));
1657c21dee17SSøren Schmidt 	case LINUX_LISTEN:
1658b40ce416SJulian Elischer 		return (linux_listen(td, arg));
1659c21dee17SSøren Schmidt 	case LINUX_ACCEPT:
1660b40ce416SJulian Elischer 		return (linux_accept(td, arg));
1661c21dee17SSøren Schmidt 	case LINUX_GETSOCKNAME:
1662b40ce416SJulian Elischer 		return (linux_getsockname(td, arg));
1663c21dee17SSøren Schmidt 	case LINUX_GETPEERNAME:
1664b40ce416SJulian Elischer 		return (linux_getpeername(td, arg));
1665c21dee17SSøren Schmidt 	case LINUX_SOCKETPAIR:
1666b40ce416SJulian Elischer 		return (linux_socketpair(td, arg));
1667c21dee17SSøren Schmidt 	case LINUX_SEND:
1668b40ce416SJulian Elischer 		return (linux_send(td, arg));
1669c21dee17SSøren Schmidt 	case LINUX_RECV:
1670b40ce416SJulian Elischer 		return (linux_recv(td, arg));
1671c21dee17SSøren Schmidt 	case LINUX_SENDTO:
1672b40ce416SJulian Elischer 		return (linux_sendto(td, arg));
1673c21dee17SSøren Schmidt 	case LINUX_RECVFROM:
1674b40ce416SJulian Elischer 		return (linux_recvfrom(td, arg));
1675c21dee17SSøren Schmidt 	case LINUX_SHUTDOWN:
1676b40ce416SJulian Elischer 		return (linux_shutdown(td, arg));
1677c21dee17SSøren Schmidt 	case LINUX_SETSOCKOPT:
1678b40ce416SJulian Elischer 		return (linux_setsockopt(td, arg));
1679c21dee17SSøren Schmidt 	case LINUX_GETSOCKOPT:
1680b40ce416SJulian Elischer 		return (linux_getsockopt(td, arg));
1681e76bba09SSøren Schmidt 	case LINUX_SENDMSG:
1682ca26842eSHajimu UMEMOTO 		return (linux_sendmsg(td, arg));
1683e76bba09SSøren Schmidt 	case LINUX_RECVMSG:
1684b40ce416SJulian Elischer 		return (linux_recvmsg(td, arg));
1685f8cd0af2SDmitry Chagin 	case LINUX_ACCEPT4:
1686f8cd0af2SDmitry Chagin 		return (linux_accept4(td, arg));
1687c21dee17SSøren Schmidt 	}
16883f3a4815SMarcel Moolenaar 
16893f3a4815SMarcel Moolenaar 	uprintf("LINUX: 'socket' typ=%d not implemented\n", args->what);
16903f3a4815SMarcel Moolenaar 	return (ENOSYS);
1691c21dee17SSøren Schmidt }
1692