xref: /freebsd/sys/compat/linux/linux_socket.c (revision d4dd69c46cf0a3513b5ae06bd7f3e579574985d3)
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>
40dad3b88aSMike Smith #include <sys/fcntl.h>
410bf301c0SJonathan Lemon #include <sys/file.h>
42104a9b7eSAlexander Kabaev #include <sys/limits.h>
434641373fSJohn Baldwin #include <sys/lock.h>
44ca26842eSHajimu UMEMOTO #include <sys/malloc.h>
454641373fSJohn Baldwin #include <sys/mutex.h>
465a8a13e0SDavid Malone #include <sys/mbuf.h>
47c21dee17SSøren Schmidt #include <sys/socket.h>
480bf301c0SJonathan Lemon #include <sys/socketvar.h>
49ca26842eSHajimu UMEMOTO #include <sys/syscallsubr.h>
5008637435SBruce Evans #include <sys/uio.h>
51ca26842eSHajimu UMEMOTO #include <sys/syslog.h>
52d0b2365eSKonstantin Belousov #include <sys/un.h>
53603724d3SBjoern A. Zeeb #include <sys/vimage.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>
634b79449eSBjoern A. Zeeb #include <netinet6/vinet6.h>
64ca26842eSHajimu UMEMOTO #endif
65c21dee17SSøren Schmidt 
661997c537SDavid E. O'Brien #ifdef COMPAT_LINUX32
674af27623STim J. Robbins #include <machine/../linux32/linux.h>
684af27623STim J. Robbins #include <machine/../linux32/linux32_proto.h>
691997c537SDavid E. O'Brien #else
701997c537SDavid E. O'Brien #include <machine/../linux/linux.h>
711997c537SDavid E. O'Brien #include <machine/../linux/linux_proto.h>
724af27623STim J. Robbins #endif
7340dbba57SAssar Westerlund #include <compat/linux/linux_socket.h>
74ac951e62SMarcel Moolenaar #include <compat/linux/linux_util.h>
75c21dee17SSøren Schmidt 
76ca26842eSHajimu UMEMOTO static int do_sa_get(struct sockaddr **, const struct osockaddr *, int *,
77ca26842eSHajimu UMEMOTO     struct malloc_type *);
78ca26842eSHajimu UMEMOTO static int linux_to_bsd_domain(int);
79ca26842eSHajimu UMEMOTO 
804730796cSBill Fenner /*
81ca26842eSHajimu UMEMOTO  * Reads a linux sockaddr and does any necessary translation.
82ca26842eSHajimu UMEMOTO  * Linux sockaddrs don't have a length field, only a family.
834730796cSBill Fenner  */
844730796cSBill Fenner static int
85ca26842eSHajimu UMEMOTO linux_getsockaddr(struct sockaddr **sap, const struct osockaddr *osa, int len)
864730796cSBill Fenner {
87ca26842eSHajimu UMEMOTO 	int osalen = len;
884730796cSBill Fenner 
89ca26842eSHajimu UMEMOTO 	return (do_sa_get(sap, osa, &osalen, M_SONAME));
904730796cSBill Fenner }
914730796cSBill Fenner 
92ca26842eSHajimu UMEMOTO /*
93ca26842eSHajimu UMEMOTO  * Copy the osockaddr structure pointed to by osa to kernel, adjust
94ca26842eSHajimu UMEMOTO  * family and convert to sockaddr.
95ca26842eSHajimu UMEMOTO  */
96ca26842eSHajimu UMEMOTO static int
97ca26842eSHajimu UMEMOTO do_sa_get(struct sockaddr **sap, const struct osockaddr *osa, int *osalen,
98ca26842eSHajimu UMEMOTO     struct malloc_type *mtype)
99ca26842eSHajimu UMEMOTO {
100ca26842eSHajimu UMEMOTO 	int error=0, bdom;
101ca26842eSHajimu UMEMOTO 	struct sockaddr *sa;
102ca26842eSHajimu UMEMOTO 	struct osockaddr *kosa;
103ca26842eSHajimu UMEMOTO 	int alloclen;
104ca26842eSHajimu UMEMOTO #ifdef INET6
105ca26842eSHajimu UMEMOTO 	int oldv6size;
106ca26842eSHajimu UMEMOTO 	struct sockaddr_in6 *sin6;
107ca26842eSHajimu UMEMOTO #endif
108ca26842eSHajimu UMEMOTO 
109ca26842eSHajimu UMEMOTO 	if (*osalen < 2 || *osalen > UCHAR_MAX || !osa)
110ca26842eSHajimu UMEMOTO 		return (EINVAL);
111ca26842eSHajimu UMEMOTO 
112ca26842eSHajimu UMEMOTO 	alloclen = *osalen;
113ca26842eSHajimu UMEMOTO #ifdef INET6
114ca26842eSHajimu UMEMOTO 	oldv6size = 0;
115ca26842eSHajimu UMEMOTO 	/*
116ca26842eSHajimu UMEMOTO 	 * Check for old (pre-RFC2553) sockaddr_in6. We may accept it
117ca26842eSHajimu UMEMOTO 	 * if it's a v4-mapped address, so reserve the proper space
118ca26842eSHajimu UMEMOTO 	 * for it.
119ca26842eSHajimu UMEMOTO 	 */
120ca26842eSHajimu UMEMOTO 	if (alloclen == sizeof (struct sockaddr_in6) - sizeof (u_int32_t)) {
121ca26842eSHajimu UMEMOTO 		alloclen = sizeof (struct sockaddr_in6);
122ca26842eSHajimu UMEMOTO 		oldv6size = 1;
123ca26842eSHajimu UMEMOTO 	}
124ca26842eSHajimu UMEMOTO #endif
125ca26842eSHajimu UMEMOTO 
1261ede983cSDag-Erling Smørgrav 	kosa = malloc(alloclen, mtype, M_WAITOK);
127ca26842eSHajimu UMEMOTO 
1284b7ef73dSDag-Erling Smørgrav 	if ((error = copyin(osa, kosa, *osalen)))
129ca26842eSHajimu UMEMOTO 		goto out;
130ca26842eSHajimu UMEMOTO 
131ca26842eSHajimu UMEMOTO 	bdom = linux_to_bsd_domain(kosa->sa_family);
132ca26842eSHajimu UMEMOTO 	if (bdom == -1) {
133ca26842eSHajimu UMEMOTO 		error = EINVAL;
134ca26842eSHajimu UMEMOTO 		goto out;
135ca26842eSHajimu UMEMOTO 	}
136ca26842eSHajimu UMEMOTO 
137ca26842eSHajimu UMEMOTO #ifdef INET6
138ca26842eSHajimu UMEMOTO 	/*
139ca26842eSHajimu UMEMOTO 	 * Older Linux IPv6 code uses obsolete RFC2133 struct sockaddr_in6,
140ca26842eSHajimu UMEMOTO 	 * which lacks the scope id compared with RFC2553 one. If we detect
141ca26842eSHajimu UMEMOTO 	 * the situation, reject the address and write a message to system log.
142ca26842eSHajimu UMEMOTO 	 *
143ca26842eSHajimu UMEMOTO 	 * Still accept addresses for which the scope id is not used.
144ca26842eSHajimu UMEMOTO 	 */
145ca26842eSHajimu UMEMOTO 	if (oldv6size && bdom == AF_INET6) {
146ca26842eSHajimu UMEMOTO 		sin6 = (struct sockaddr_in6 *)kosa;
147ca26842eSHajimu UMEMOTO 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr) ||
148ca26842eSHajimu UMEMOTO 		    (!IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) &&
149ca26842eSHajimu UMEMOTO 		     !IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr) &&
150ca26842eSHajimu UMEMOTO 		     !IN6_IS_ADDR_V4COMPAT(&sin6->sin6_addr) &&
151ca26842eSHajimu UMEMOTO 		     !IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) &&
152ca26842eSHajimu UMEMOTO 		     !IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))) {
153ca26842eSHajimu UMEMOTO 			sin6->sin6_scope_id = 0;
154ca26842eSHajimu UMEMOTO 		} else {
155ca26842eSHajimu UMEMOTO 			log(LOG_DEBUG,
1563c616032SGleb Smirnoff 			    "obsolete pre-RFC2553 sockaddr_in6 rejected\n");
157ca26842eSHajimu UMEMOTO 			error = EINVAL;
158ca26842eSHajimu UMEMOTO 			goto out;
159ca26842eSHajimu UMEMOTO 		}
160ca26842eSHajimu UMEMOTO 	} else
161ca26842eSHajimu UMEMOTO #endif
162ca26842eSHajimu UMEMOTO 	if (bdom == AF_INET)
163ca26842eSHajimu UMEMOTO 		alloclen = sizeof(struct sockaddr_in);
164ca26842eSHajimu UMEMOTO 
165ca26842eSHajimu UMEMOTO 	sa = (struct sockaddr *) kosa;
166ca26842eSHajimu UMEMOTO 	sa->sa_family = bdom;
167ca26842eSHajimu UMEMOTO 	sa->sa_len = alloclen;
168ca26842eSHajimu UMEMOTO 
169ca26842eSHajimu UMEMOTO 	*sap = sa;
170ca26842eSHajimu UMEMOTO 	*osalen = alloclen;
171ca26842eSHajimu UMEMOTO 	return (0);
172ca26842eSHajimu UMEMOTO 
173ca26842eSHajimu UMEMOTO out:
1741ede983cSDag-Erling Smørgrav 	free(kosa, mtype);
175ca26842eSHajimu UMEMOTO 	return (error);
176ca26842eSHajimu UMEMOTO }
177ca26842eSHajimu UMEMOTO 
178c21dee17SSøren Schmidt static int
179c21dee17SSøren Schmidt linux_to_bsd_domain(int domain)
180c21dee17SSøren Schmidt {
1813f3a4815SMarcel Moolenaar 
182c21dee17SSøren Schmidt 	switch (domain) {
183c21dee17SSøren Schmidt 	case LINUX_AF_UNSPEC:
1843f3a4815SMarcel Moolenaar 		return (AF_UNSPEC);
185c21dee17SSøren Schmidt 	case LINUX_AF_UNIX:
1863f3a4815SMarcel Moolenaar 		return (AF_LOCAL);
187c21dee17SSøren Schmidt 	case LINUX_AF_INET:
1883f3a4815SMarcel Moolenaar 		return (AF_INET);
189ca26842eSHajimu UMEMOTO 	case LINUX_AF_INET6:
190ca26842eSHajimu UMEMOTO 		return (AF_INET6);
191c21dee17SSøren Schmidt 	case LINUX_AF_AX25:
1923f3a4815SMarcel Moolenaar 		return (AF_CCITT);
193c21dee17SSøren Schmidt 	case LINUX_AF_IPX:
1943f3a4815SMarcel Moolenaar 		return (AF_IPX);
195c21dee17SSøren Schmidt 	case LINUX_AF_APPLETALK:
1963f3a4815SMarcel Moolenaar 		return (AF_APPLETALK);
197c21dee17SSøren Schmidt 	}
1983f3a4815SMarcel Moolenaar 	return (-1);
199c21dee17SSøren Schmidt }
200c21dee17SSøren Schmidt 
201ca26842eSHajimu UMEMOTO static int
202ca26842eSHajimu UMEMOTO bsd_to_linux_domain(int domain)
203ca26842eSHajimu UMEMOTO {
204ca26842eSHajimu UMEMOTO 
205ca26842eSHajimu UMEMOTO 	switch (domain) {
206ca26842eSHajimu UMEMOTO 	case AF_UNSPEC:
207ca26842eSHajimu UMEMOTO 		return (LINUX_AF_UNSPEC);
208ca26842eSHajimu UMEMOTO 	case AF_LOCAL:
209ca26842eSHajimu UMEMOTO 		return (LINUX_AF_UNIX);
210ca26842eSHajimu UMEMOTO 	case AF_INET:
211ca26842eSHajimu UMEMOTO 		return (LINUX_AF_INET);
212ca26842eSHajimu UMEMOTO 	case AF_INET6:
213ca26842eSHajimu UMEMOTO 		return (LINUX_AF_INET6);
214ca26842eSHajimu UMEMOTO 	case AF_CCITT:
215ca26842eSHajimu UMEMOTO 		return (LINUX_AF_AX25);
216ca26842eSHajimu UMEMOTO 	case AF_IPX:
217ca26842eSHajimu UMEMOTO 		return (LINUX_AF_IPX);
218ca26842eSHajimu UMEMOTO 	case AF_APPLETALK:
219ca26842eSHajimu UMEMOTO 		return (LINUX_AF_APPLETALK);
220ca26842eSHajimu UMEMOTO 	}
221ca26842eSHajimu UMEMOTO 	return (-1);
222ca26842eSHajimu UMEMOTO }
223ca26842eSHajimu UMEMOTO 
224c21dee17SSøren Schmidt static int
225c21dee17SSøren Schmidt linux_to_bsd_sockopt_level(int level)
226c21dee17SSøren Schmidt {
2273f3a4815SMarcel Moolenaar 
228c21dee17SSøren Schmidt 	switch (level) {
229c21dee17SSøren Schmidt 	case LINUX_SOL_SOCKET:
2303f3a4815SMarcel Moolenaar 		return (SOL_SOCKET);
231c21dee17SSøren Schmidt 	}
2323f3a4815SMarcel Moolenaar 	return (level);
233c21dee17SSøren Schmidt }
234c21dee17SSøren Schmidt 
2353f3a4815SMarcel Moolenaar static int
23684b11cd8SMitsuru IWASAKI bsd_to_linux_sockopt_level(int level)
23784b11cd8SMitsuru IWASAKI {
23884b11cd8SMitsuru IWASAKI 
23984b11cd8SMitsuru IWASAKI 	switch (level) {
24084b11cd8SMitsuru IWASAKI 	case SOL_SOCKET:
24184b11cd8SMitsuru IWASAKI 		return (LINUX_SOL_SOCKET);
24284b11cd8SMitsuru IWASAKI 	}
24384b11cd8SMitsuru IWASAKI 	return (level);
24484b11cd8SMitsuru IWASAKI }
24584b11cd8SMitsuru IWASAKI 
24684b11cd8SMitsuru IWASAKI static int
2473f3a4815SMarcel Moolenaar linux_to_bsd_ip_sockopt(int opt)
248c21dee17SSøren Schmidt {
2493f3a4815SMarcel Moolenaar 
250c21dee17SSøren Schmidt 	switch (opt) {
251c21dee17SSøren Schmidt 	case LINUX_IP_TOS:
2523f3a4815SMarcel Moolenaar 		return (IP_TOS);
253c21dee17SSøren Schmidt 	case LINUX_IP_TTL:
2543f3a4815SMarcel Moolenaar 		return (IP_TTL);
25566ff6a3cSBill Fenner 	case LINUX_IP_OPTIONS:
2563f3a4815SMarcel Moolenaar 		return (IP_OPTIONS);
25766ff6a3cSBill Fenner 	case LINUX_IP_MULTICAST_IF:
2583f3a4815SMarcel Moolenaar 		return (IP_MULTICAST_IF);
25966ff6a3cSBill Fenner 	case LINUX_IP_MULTICAST_TTL:
2603f3a4815SMarcel Moolenaar 		return (IP_MULTICAST_TTL);
26166ff6a3cSBill Fenner 	case LINUX_IP_MULTICAST_LOOP:
2623f3a4815SMarcel Moolenaar 		return (IP_MULTICAST_LOOP);
26366ff6a3cSBill Fenner 	case LINUX_IP_ADD_MEMBERSHIP:
2643f3a4815SMarcel Moolenaar 		return (IP_ADD_MEMBERSHIP);
26566ff6a3cSBill Fenner 	case LINUX_IP_DROP_MEMBERSHIP:
2663f3a4815SMarcel Moolenaar 		return (IP_DROP_MEMBERSHIP);
26766ff6a3cSBill Fenner 	case LINUX_IP_HDRINCL:
2683f3a4815SMarcel Moolenaar 		return (IP_HDRINCL);
269c21dee17SSøren Schmidt 	}
2703f3a4815SMarcel Moolenaar 	return (-1);
271c21dee17SSøren Schmidt }
272c21dee17SSøren Schmidt 
273c21dee17SSøren Schmidt static int
274c21dee17SSøren Schmidt linux_to_bsd_so_sockopt(int opt)
275c21dee17SSøren Schmidt {
2763f3a4815SMarcel Moolenaar 
277c21dee17SSøren Schmidt 	switch (opt) {
278c21dee17SSøren Schmidt 	case LINUX_SO_DEBUG:
2793f3a4815SMarcel Moolenaar 		return (SO_DEBUG);
280c21dee17SSøren Schmidt 	case LINUX_SO_REUSEADDR:
2813f3a4815SMarcel Moolenaar 		return (SO_REUSEADDR);
282c21dee17SSøren Schmidt 	case LINUX_SO_TYPE:
2833f3a4815SMarcel Moolenaar 		return (SO_TYPE);
284c21dee17SSøren Schmidt 	case LINUX_SO_ERROR:
2853f3a4815SMarcel Moolenaar 		return (SO_ERROR);
286c21dee17SSøren Schmidt 	case LINUX_SO_DONTROUTE:
2873f3a4815SMarcel Moolenaar 		return (SO_DONTROUTE);
288c21dee17SSøren Schmidt 	case LINUX_SO_BROADCAST:
2893f3a4815SMarcel Moolenaar 		return (SO_BROADCAST);
290c21dee17SSøren Schmidt 	case LINUX_SO_SNDBUF:
2913f3a4815SMarcel Moolenaar 		return (SO_SNDBUF);
292c21dee17SSøren Schmidt 	case LINUX_SO_RCVBUF:
2933f3a4815SMarcel Moolenaar 		return (SO_RCVBUF);
294c21dee17SSøren Schmidt 	case LINUX_SO_KEEPALIVE:
2953f3a4815SMarcel Moolenaar 		return (SO_KEEPALIVE);
296c21dee17SSøren Schmidt 	case LINUX_SO_OOBINLINE:
2973f3a4815SMarcel Moolenaar 		return (SO_OOBINLINE);
298c21dee17SSøren Schmidt 	case LINUX_SO_LINGER:
2993f3a4815SMarcel Moolenaar 		return (SO_LINGER);
300d0b2365eSKonstantin Belousov 	case LINUX_SO_PEERCRED:
301d0b2365eSKonstantin Belousov 		return (LOCAL_PEERCRED);
302d0b2365eSKonstantin Belousov 	case LINUX_SO_RCVLOWAT:
303d0b2365eSKonstantin Belousov 		return (SO_RCVLOWAT);
304d0b2365eSKonstantin Belousov 	case LINUX_SO_SNDLOWAT:
305d0b2365eSKonstantin Belousov 		return (SO_SNDLOWAT);
306d0b2365eSKonstantin Belousov 	case LINUX_SO_RCVTIMEO:
307d0b2365eSKonstantin Belousov 		return (SO_RCVTIMEO);
308d0b2365eSKonstantin Belousov 	case LINUX_SO_SNDTIMEO:
309d0b2365eSKonstantin Belousov 		return (SO_SNDTIMEO);
310d0b2365eSKonstantin Belousov 	case LINUX_SO_TIMESTAMP:
311d0b2365eSKonstantin Belousov 		return (SO_TIMESTAMP);
312d0b2365eSKonstantin Belousov 	case LINUX_SO_ACCEPTCONN:
313d0b2365eSKonstantin Belousov 		return (SO_ACCEPTCONN);
314c21dee17SSøren Schmidt 	}
3153f3a4815SMarcel Moolenaar 	return (-1);
316c21dee17SSøren Schmidt }
317c21dee17SSøren Schmidt 
31840dbba57SAssar Westerlund static int
31940dbba57SAssar Westerlund linux_to_bsd_msg_flags(int flags)
32040dbba57SAssar Westerlund {
32140dbba57SAssar Westerlund 	int ret_flags = 0;
32240dbba57SAssar Westerlund 
32340dbba57SAssar Westerlund 	if (flags & LINUX_MSG_OOB)
32440dbba57SAssar Westerlund 		ret_flags |= MSG_OOB;
32540dbba57SAssar Westerlund 	if (flags & LINUX_MSG_PEEK)
32640dbba57SAssar Westerlund 		ret_flags |= MSG_PEEK;
32740dbba57SAssar Westerlund 	if (flags & LINUX_MSG_DONTROUTE)
32840dbba57SAssar Westerlund 		ret_flags |= MSG_DONTROUTE;
32940dbba57SAssar Westerlund 	if (flags & LINUX_MSG_CTRUNC)
33040dbba57SAssar Westerlund 		ret_flags |= MSG_CTRUNC;
33140dbba57SAssar Westerlund 	if (flags & LINUX_MSG_TRUNC)
33240dbba57SAssar Westerlund 		ret_flags |= MSG_TRUNC;
33340dbba57SAssar Westerlund 	if (flags & LINUX_MSG_DONTWAIT)
33440dbba57SAssar Westerlund 		ret_flags |= MSG_DONTWAIT;
33540dbba57SAssar Westerlund 	if (flags & LINUX_MSG_EOR)
33640dbba57SAssar Westerlund 		ret_flags |= MSG_EOR;
33740dbba57SAssar Westerlund 	if (flags & LINUX_MSG_WAITALL)
33840dbba57SAssar Westerlund 		ret_flags |= MSG_WAITALL;
3398d6e40c3SMaxim Sobolev 	if (flags & LINUX_MSG_NOSIGNAL)
3408d6e40c3SMaxim Sobolev 		ret_flags |= MSG_NOSIGNAL;
34140dbba57SAssar Westerlund #if 0 /* not handled */
34240dbba57SAssar Westerlund 	if (flags & LINUX_MSG_PROXY)
34340dbba57SAssar Westerlund 		;
34440dbba57SAssar Westerlund 	if (flags & LINUX_MSG_FIN)
34540dbba57SAssar Westerlund 		;
34640dbba57SAssar Westerlund 	if (flags & LINUX_MSG_SYN)
34740dbba57SAssar Westerlund 		;
34840dbba57SAssar Westerlund 	if (flags & LINUX_MSG_CONFIRM)
34940dbba57SAssar Westerlund 		;
35040dbba57SAssar Westerlund 	if (flags & LINUX_MSG_RST)
35140dbba57SAssar Westerlund 		;
35240dbba57SAssar Westerlund 	if (flags & LINUX_MSG_ERRQUEUE)
35340dbba57SAssar Westerlund 		;
35440dbba57SAssar Westerlund #endif
35540dbba57SAssar Westerlund 	return ret_flags;
35640dbba57SAssar Westerlund }
35740dbba57SAssar Westerlund 
3585c8919adSAlexander Leidinger /*
3595c8919adSAlexander Leidinger * If bsd_to_linux_sockaddr() or linux_to_bsd_sockaddr() faults, then the
3605c8919adSAlexander Leidinger * native syscall will fault.  Thus, we don't really need to check the
3615c8919adSAlexander Leidinger * return values for these functions.
3625c8919adSAlexander Leidinger */
3635c8919adSAlexander Leidinger 
3645c8919adSAlexander Leidinger static int
3655c8919adSAlexander Leidinger bsd_to_linux_sockaddr(struct sockaddr *arg)
3665c8919adSAlexander Leidinger {
3675c8919adSAlexander Leidinger 	struct sockaddr sa;
3685c8919adSAlexander Leidinger 	size_t sa_len = sizeof(struct sockaddr);
3695c8919adSAlexander Leidinger 	int error;
3705c8919adSAlexander Leidinger 
3715c8919adSAlexander Leidinger 	if ((error = copyin(arg, &sa, sa_len)))
3725c8919adSAlexander Leidinger 		return (error);
3735c8919adSAlexander Leidinger 
3745c8919adSAlexander Leidinger 	*(u_short *)&sa = sa.sa_family;
3755c8919adSAlexander Leidinger 
3765c8919adSAlexander Leidinger 	error = copyout(&sa, arg, sa_len);
3775c8919adSAlexander Leidinger 
3785c8919adSAlexander Leidinger 	return (error);
3795c8919adSAlexander Leidinger }
3805c8919adSAlexander Leidinger 
3815c8919adSAlexander Leidinger static int
3825c8919adSAlexander Leidinger linux_to_bsd_sockaddr(struct sockaddr *arg, int len)
3835c8919adSAlexander Leidinger {
3845c8919adSAlexander Leidinger 	struct sockaddr sa;
3855c8919adSAlexander Leidinger 	size_t sa_len = sizeof(struct sockaddr);
3865c8919adSAlexander Leidinger 	int error;
3875c8919adSAlexander Leidinger 
3885c8919adSAlexander Leidinger 	if ((error = copyin(arg, &sa, sa_len)))
3895c8919adSAlexander Leidinger 		return (error);
3905c8919adSAlexander Leidinger 
3915c8919adSAlexander Leidinger 	sa.sa_family = *(sa_family_t *)&sa;
3925c8919adSAlexander Leidinger 	sa.sa_len = len;
3935c8919adSAlexander Leidinger 
3945c8919adSAlexander Leidinger 	error = copyout(&sa, arg, sa_len);
3955c8919adSAlexander Leidinger 
3965c8919adSAlexander Leidinger 	return (error);
3975c8919adSAlexander Leidinger }
3985c8919adSAlexander Leidinger 
3995c8919adSAlexander Leidinger 
400ca26842eSHajimu UMEMOTO static int
401ca26842eSHajimu UMEMOTO linux_sa_put(struct osockaddr *osa)
402ca26842eSHajimu UMEMOTO {
403ca26842eSHajimu UMEMOTO 	struct osockaddr sa;
404ca26842eSHajimu UMEMOTO 	int error, bdom;
405ca26842eSHajimu UMEMOTO 
406ca26842eSHajimu UMEMOTO 	/*
407ca26842eSHajimu UMEMOTO 	 * Only read/write the osockaddr family part, the rest is
408ca26842eSHajimu UMEMOTO 	 * not changed.
409ca26842eSHajimu UMEMOTO 	 */
4104b7ef73dSDag-Erling Smørgrav 	error = copyin(osa, &sa, sizeof(sa.sa_family));
411ca26842eSHajimu UMEMOTO 	if (error)
412ca26842eSHajimu UMEMOTO 		return (error);
413ca26842eSHajimu UMEMOTO 
414ca26842eSHajimu UMEMOTO 	bdom = bsd_to_linux_domain(sa.sa_family);
415ca26842eSHajimu UMEMOTO 	if (bdom == -1)
416ca26842eSHajimu UMEMOTO 		return (EINVAL);
417ca26842eSHajimu UMEMOTO 
418ca26842eSHajimu UMEMOTO 	sa.sa_family = bdom;
419ca26842eSHajimu UMEMOTO 	error = copyout(&sa, osa, sizeof(sa.sa_family));
420ca26842eSHajimu UMEMOTO 	if (error)
421ca26842eSHajimu UMEMOTO 		return (error);
422ca26842eSHajimu UMEMOTO 
423ca26842eSHajimu UMEMOTO 	return (0);
424ca26842eSHajimu UMEMOTO }
425ca26842eSHajimu UMEMOTO 
4265a8a13e0SDavid Malone static int
42774f5d680SKonstantin Belousov linux_to_bsd_cmsg_type(int cmsg_type)
4285a8a13e0SDavid Malone {
42974f5d680SKonstantin Belousov 
43074f5d680SKonstantin Belousov 	switch (cmsg_type) {
43174f5d680SKonstantin Belousov 	case LINUX_SCM_RIGHTS:
43274f5d680SKonstantin Belousov 		return (SCM_RIGHTS);
43374f5d680SKonstantin Belousov 	}
43474f5d680SKonstantin Belousov 	return (-1);
43574f5d680SKonstantin Belousov }
43674f5d680SKonstantin Belousov 
43774f5d680SKonstantin Belousov static int
43874f5d680SKonstantin Belousov bsd_to_linux_cmsg_type(int cmsg_type)
43974f5d680SKonstantin Belousov {
44074f5d680SKonstantin Belousov 
44174f5d680SKonstantin Belousov 	switch (cmsg_type) {
44274f5d680SKonstantin Belousov 	case SCM_RIGHTS:
44374f5d680SKonstantin Belousov 		return (LINUX_SCM_RIGHTS);
44474f5d680SKonstantin Belousov 	}
44574f5d680SKonstantin Belousov 	return (-1);
44674f5d680SKonstantin Belousov }
44774f5d680SKonstantin Belousov 
44874f5d680SKonstantin Belousov 
44974f5d680SKonstantin Belousov 
45074f5d680SKonstantin Belousov static int
45174f5d680SKonstantin Belousov linux_to_bsd_msghdr(struct msghdr *bhdr, const struct l_msghdr *lhdr)
45274f5d680SKonstantin Belousov {
45374f5d680SKonstantin Belousov 	if (lhdr->msg_controllen > INT_MAX)
45474f5d680SKonstantin Belousov 		return (ENOBUFS);
45574f5d680SKonstantin Belousov 
45674f5d680SKonstantin Belousov 	bhdr->msg_name		= PTRIN(lhdr->msg_name);
45774f5d680SKonstantin Belousov 	bhdr->msg_namelen	= lhdr->msg_namelen;
45874f5d680SKonstantin Belousov 	bhdr->msg_iov		= PTRIN(lhdr->msg_iov);
45974f5d680SKonstantin Belousov 	bhdr->msg_iovlen	= lhdr->msg_iovlen;
46074f5d680SKonstantin Belousov 	bhdr->msg_control	= PTRIN(lhdr->msg_control);
46174f5d680SKonstantin Belousov 	bhdr->msg_controllen	= lhdr->msg_controllen;
46274f5d680SKonstantin Belousov 	bhdr->msg_flags		= linux_to_bsd_msg_flags(lhdr->msg_flags);
46374f5d680SKonstantin Belousov 	return (0);
46474f5d680SKonstantin Belousov }
46574f5d680SKonstantin Belousov 
46674f5d680SKonstantin Belousov static int
46774f5d680SKonstantin Belousov bsd_to_linux_msghdr(const struct msghdr *bhdr, struct l_msghdr *lhdr)
46874f5d680SKonstantin Belousov {
46974f5d680SKonstantin Belousov 	lhdr->msg_name		= PTROUT(bhdr->msg_name);
47074f5d680SKonstantin Belousov 	lhdr->msg_namelen	= bhdr->msg_namelen;
47174f5d680SKonstantin Belousov 	lhdr->msg_iov		= PTROUT(bhdr->msg_iov);
47274f5d680SKonstantin Belousov 	lhdr->msg_iovlen	= bhdr->msg_iovlen;
47374f5d680SKonstantin Belousov 	lhdr->msg_control	= PTROUT(bhdr->msg_control);
47474f5d680SKonstantin Belousov 	lhdr->msg_controllen	= bhdr->msg_controllen;
47574f5d680SKonstantin Belousov 	/* msg_flags skipped */
47674f5d680SKonstantin Belousov 	return (0);
47774f5d680SKonstantin Belousov }
47874f5d680SKonstantin Belousov 
47974f5d680SKonstantin Belousov static int
48074f5d680SKonstantin Belousov linux_sendit(struct thread *td, int s, struct msghdr *mp, int flags,
48174f5d680SKonstantin Belousov     struct mbuf *control, enum uio_seg segflg)
48274f5d680SKonstantin Belousov {
4835a8a13e0SDavid Malone 	struct sockaddr *to;
4845a8a13e0SDavid Malone 	int error;
4855a8a13e0SDavid Malone 
4865a8a13e0SDavid Malone 	if (mp->msg_name != NULL) {
4875a8a13e0SDavid Malone 		error = linux_getsockaddr(&to, mp->msg_name, mp->msg_namelen);
4885a8a13e0SDavid Malone 		if (error)
4895a8a13e0SDavid Malone 			return (error);
4905a8a13e0SDavid Malone 		mp->msg_name = to;
4915a8a13e0SDavid Malone 	} else
4925a8a13e0SDavid Malone 		to = NULL;
4935a8a13e0SDavid Malone 
494a6886ef1SMaxim Sobolev 	error = kern_sendit(td, s, mp, linux_to_bsd_msg_flags(flags), control,
495a6886ef1SMaxim Sobolev 	    segflg);
4965a8a13e0SDavid Malone 
4975a8a13e0SDavid Malone 	if (to)
4981ede983cSDag-Erling Smørgrav 		free(to, M_SONAME);
4995a8a13e0SDavid Malone 	return (error);
5005a8a13e0SDavid Malone }
5015a8a13e0SDavid Malone 
5023f3a4815SMarcel Moolenaar /* Return 0 if IP_HDRINCL is set for the given socket. */
503f2477ae1SMike Smith static int
504e140eb43SDavid Malone linux_check_hdrincl(struct thread *td, int s)
505f2477ae1SMike Smith {
5063db2a843SBruce Evans 	int error, optval, size_val;
507f2477ae1SMike Smith 
508e140eb43SDavid Malone 	size_val = sizeof(optval);
509e140eb43SDavid Malone 	error = kern_getsockopt(td, s, IPPROTO_IP, IP_HDRINCL,
510e140eb43SDavid Malone 	    &optval, UIO_SYSSPACE, &size_val);
511e140eb43SDavid Malone 	if (error)
5123f3a4815SMarcel Moolenaar 		return (error);
5133f3a4815SMarcel Moolenaar 
5143f3a4815SMarcel Moolenaar 	return (optval == 0);
515f2477ae1SMike Smith }
516f2477ae1SMike Smith 
5175a8a13e0SDavid Malone struct linux_sendto_args {
5185a8a13e0SDavid Malone 	int s;
5194af27623STim J. Robbins 	l_uintptr_t msg;
5205a8a13e0SDavid Malone 	int len;
5215a8a13e0SDavid Malone 	int flags;
5224af27623STim J. Robbins 	l_uintptr_t to;
5235a8a13e0SDavid Malone 	int tolen;
5245a8a13e0SDavid Malone };
5255a8a13e0SDavid Malone 
526f2477ae1SMike Smith /*
527f2477ae1SMike Smith  * Updated sendto() when IP_HDRINCL is set:
528f2477ae1SMike Smith  * tweak endian-dependent fields in the IP packet.
529f2477ae1SMike Smith  */
530f2477ae1SMike Smith static int
53138da2381SRobert Watson linux_sendto_hdrincl(struct thread *td, struct linux_sendto_args *linux_args)
532f2477ae1SMike Smith {
533f2477ae1SMike Smith /*
534f2477ae1SMike Smith  * linux_ip_copysize defines how many bytes we should copy
535f2477ae1SMike Smith  * from the beginning of the IP packet before we customize it for BSD.
536a6886ef1SMaxim Sobolev  * It should include all the fields we modify (ip_len and ip_off).
537f2477ae1SMike Smith  */
538f2477ae1SMike Smith #define linux_ip_copysize	8
539f2477ae1SMike Smith 
540f2477ae1SMike Smith 	struct ip *packet;
5415a8a13e0SDavid Malone 	struct msghdr msg;
542a6886ef1SMaxim Sobolev 	struct iovec aiov[1];
543f2477ae1SMike Smith 	int error;
544f2477ae1SMike Smith 
545aa675b57SDavid Schultz 	/* Check that the packet isn't too big or too small. */
546aa675b57SDavid Schultz 	if (linux_args->len < linux_ip_copysize ||
547aa675b57SDavid Schultz 	    linux_args->len > IP_MAXPACKET)
5483f3a4815SMarcel Moolenaar 		return (EINVAL);
549f2477ae1SMike Smith 
550a6886ef1SMaxim Sobolev 	packet = (struct ip *)malloc(linux_args->len, M_TEMP, M_WAITOK);
551f2477ae1SMike Smith 
552a6886ef1SMaxim Sobolev 	/* Make kernel copy of the packet to be sent */
5534af27623STim J. Robbins 	if ((error = copyin(PTRIN(linux_args->msg), packet,
554a6886ef1SMaxim Sobolev 	    linux_args->len)))
555a6886ef1SMaxim Sobolev 		goto goout;
556f2477ae1SMike Smith 
557f2477ae1SMike Smith 	/* Convert fields from Linux to BSD raw IP socket format */
5585a8a13e0SDavid Malone 	packet->ip_len = linux_args->len;
559f2477ae1SMike Smith 	packet->ip_off = ntohs(packet->ip_off);
560f2477ae1SMike Smith 
561f2477ae1SMike Smith 	/* Prepare the msghdr and iovec structures describing the new packet */
5624af27623STim J. Robbins 	msg.msg_name = PTRIN(linux_args->to);
5635a8a13e0SDavid Malone 	msg.msg_namelen = linux_args->tolen;
5645a8a13e0SDavid Malone 	msg.msg_iov = aiov;
565a6886ef1SMaxim Sobolev 	msg.msg_iovlen = 1;
5665a8a13e0SDavid Malone 	msg.msg_control = NULL;
5675a8a13e0SDavid Malone 	msg.msg_flags = 0;
5685a8a13e0SDavid Malone 	aiov[0].iov_base = (char *)packet;
569a6886ef1SMaxim Sobolev 	aiov[0].iov_len = linux_args->len;
570a6886ef1SMaxim Sobolev 	error = linux_sendit(td, linux_args->s, &msg, linux_args->flags,
57174f5d680SKonstantin Belousov 	    NULL, UIO_SYSSPACE);
572a6886ef1SMaxim Sobolev goout:
573a6886ef1SMaxim Sobolev 	free(packet, M_TEMP);
5745a8a13e0SDavid Malone 	return (error);
575f2477ae1SMike Smith }
576f2477ae1SMike Smith 
577c21dee17SSøren Schmidt struct linux_socket_args {
578c21dee17SSøren Schmidt 	int domain;
579c21dee17SSøren Schmidt 	int type;
580c21dee17SSøren Schmidt 	int protocol;
581c21dee17SSøren Schmidt };
582c21dee17SSøren Schmidt 
583c21dee17SSøren Schmidt static int
584b40ce416SJulian Elischer linux_socket(struct thread *td, struct linux_socket_args *args)
585c21dee17SSøren Schmidt {
5868b615593SMarko Zec #ifdef INET6
587093f25f8SMarko Zec #ifndef KLD_MODULE
5888b615593SMarko Zec 	INIT_VNET_INET6(curvnet);
5898b615593SMarko Zec #endif
590093f25f8SMarko Zec #endif
591ef04503dSPeter Wemm 	struct socket_args /* {
592c21dee17SSøren Schmidt 		int domain;
593c21dee17SSøren Schmidt 		int type;
594c21dee17SSøren Schmidt 		int protocol;
595ef04503dSPeter Wemm 	} */ bsd_args;
596f2477ae1SMike Smith 	int retval_socket;
597c21dee17SSøren Schmidt 
598745aaef5SKonstantin Belousov 	bsd_args.protocol = args->protocol;
599745aaef5SKonstantin Belousov 	bsd_args.type = args->type;
600745aaef5SKonstantin Belousov 	bsd_args.domain = linux_to_bsd_domain(args->domain);
601c21dee17SSøren Schmidt 	if (bsd_args.domain == -1)
602d9b063ccSDmitry Chagin 		return (EAFNOSUPPORT);
603f2477ae1SMike Smith 
604b40ce416SJulian Elischer 	retval_socket = socket(td, &bsd_args);
605f2477ae1SMike Smith 	if (bsd_args.type == SOCK_RAW
606f2477ae1SMike Smith 	    && (bsd_args.protocol == IPPROTO_RAW || bsd_args.protocol == 0)
607f2477ae1SMike Smith 	    && bsd_args.domain == AF_INET
608f2477ae1SMike Smith 	    && retval_socket >= 0) {
609f2477ae1SMike Smith 		/* It's a raw IP socket: set the IP_HDRINCL option. */
610e140eb43SDavid Malone 		int hdrincl;
611f2477ae1SMike Smith 
612e140eb43SDavid Malone 		hdrincl = 1;
613e140eb43SDavid Malone 		/* We ignore any error returned by kern_setsockopt() */
614e140eb43SDavid Malone 		kern_setsockopt(td, td->td_retval[0], IPPROTO_IP, IP_HDRINCL,
615e140eb43SDavid Malone 		    &hdrincl, UIO_SYSSPACE, sizeof(hdrincl));
616f2477ae1SMike Smith 	}
617ca26842eSHajimu UMEMOTO #ifdef INET6
618ca26842eSHajimu UMEMOTO 	/*
619ca26842eSHajimu UMEMOTO 	 * Linux AF_INET6 socket has IPV6_V6ONLY setsockopt set to 0 by
620ca26842eSHajimu UMEMOTO 	 * default and some apps depend on this. So, set V6ONLY to 0
621ca26842eSHajimu UMEMOTO 	 * for Linux apps if the sysctl value is set to 1.
622ca26842eSHajimu UMEMOTO 	 */
62337e7a5a2SHajimu UMEMOTO 	if (bsd_args.domain == PF_INET6 && retval_socket >= 0
62437e7a5a2SHajimu UMEMOTO #ifndef KLD_MODULE
62537e7a5a2SHajimu UMEMOTO 	    /*
62637e7a5a2SHajimu UMEMOTO 	     * XXX: Avoid undefined symbol error with an IPv4 only
62737e7a5a2SHajimu UMEMOTO 	     * kernel.
62837e7a5a2SHajimu UMEMOTO 	     */
629603724d3SBjoern A. Zeeb 	    && V_ip6_v6only
63037e7a5a2SHajimu UMEMOTO #endif
63137e7a5a2SHajimu UMEMOTO 	    ) {
632e140eb43SDavid Malone 		int v6only;
633ca26842eSHajimu UMEMOTO 
634e140eb43SDavid Malone 		v6only = 0;
635ca26842eSHajimu UMEMOTO 		/* We ignore any error returned by setsockopt() */
636e140eb43SDavid Malone 		kern_setsockopt(td, td->td_retval[0], IPPROTO_IPV6, IPV6_V6ONLY,
637e140eb43SDavid Malone 		    &v6only, UIO_SYSSPACE, sizeof(v6only));
638ca26842eSHajimu UMEMOTO 	}
639ca26842eSHajimu UMEMOTO #endif
6403f3a4815SMarcel Moolenaar 
6413f3a4815SMarcel Moolenaar 	return (retval_socket);
642c21dee17SSøren Schmidt }
643c21dee17SSøren Schmidt 
644c21dee17SSøren Schmidt struct linux_bind_args {
645c21dee17SSøren Schmidt 	int s;
6464af27623STim J. Robbins 	l_uintptr_t name;
647c21dee17SSøren Schmidt 	int namelen;
648c21dee17SSøren Schmidt };
649c21dee17SSøren Schmidt 
650c21dee17SSøren Schmidt static int
651b40ce416SJulian Elischer linux_bind(struct thread *td, struct linux_bind_args *args)
652c21dee17SSøren Schmidt {
653ca26842eSHajimu UMEMOTO 	struct sockaddr *sa;
654c21dee17SSøren Schmidt 	int error;
655c21dee17SSøren Schmidt 
656745aaef5SKonstantin Belousov 	error = linux_getsockaddr(&sa, PTRIN(args->name),
657745aaef5SKonstantin Belousov 	    args->namelen);
658ca26842eSHajimu UMEMOTO 	if (error)
659ca26842eSHajimu UMEMOTO 		return (error);
660ca26842eSHajimu UMEMOTO 
661745aaef5SKonstantin Belousov 	error = kern_bind(td, args->s, sa);
662b33887eaSJohn Baldwin 	free(sa, M_SONAME);
663745aaef5SKonstantin Belousov 	if (error == EADDRNOTAVAIL && args->namelen != sizeof(struct sockaddr_in))
664d4b7423fSAlexander Leidinger 	   	return (EINVAL);
665b33887eaSJohn Baldwin 	return (error);
666c21dee17SSøren Schmidt }
667c21dee17SSøren Schmidt 
66801e0ffbaSAlexander Leidinger struct linux_connect_args {
669c21dee17SSøren Schmidt 	int s;
6704af27623STim J. Robbins 	l_uintptr_t name;
671c21dee17SSøren Schmidt 	int namelen;
672c21dee17SSøren Schmidt };
673b40ce416SJulian Elischer int linux_connect(struct thread *, struct linux_connect_args *);
674c21dee17SSøren Schmidt 
675930a65feSAndrew Gallatin int
676b40ce416SJulian Elischer linux_connect(struct thread *td, struct linux_connect_args *args)
677c21dee17SSøren Schmidt {
6780bf301c0SJonathan Lemon 	struct socket *so;
679ca26842eSHajimu UMEMOTO 	struct sockaddr *sa;
68039c95b83SMatthew Dillon 	u_int fflag;
681c21dee17SSøren Schmidt 	int error;
682c21dee17SSøren Schmidt 
683745aaef5SKonstantin Belousov 	error = linux_getsockaddr(&sa, (struct osockaddr *)PTRIN(args->name),
684745aaef5SKonstantin Belousov 	    args->namelen);
685ca26842eSHajimu UMEMOTO 	if (error)
686ca26842eSHajimu UMEMOTO 		return (error);
687ca26842eSHajimu UMEMOTO 
688745aaef5SKonstantin Belousov 	error = kern_connect(td, args->s, sa);
689b33887eaSJohn Baldwin 	free(sa, M_SONAME);
6900bf301c0SJonathan Lemon 	if (error != EISCONN)
6910bf301c0SJonathan Lemon 		return (error);
6920bf301c0SJonathan Lemon 
693dad3b88aSMike Smith 	/*
694dad3b88aSMike Smith 	 * Linux doesn't return EISCONN the first time it occurs,
695dad3b88aSMike Smith 	 * when on a non-blocking socket. Instead it returns the
696dad3b88aSMike Smith 	 * error getsockopt(SOL_SOCKET, SO_ERROR) would return on BSD.
697f7f45ac8SRobert Watson 	 *
698f7f45ac8SRobert Watson 	 * XXXRW: Instead of using fgetsock(), check that it is a
699f7f45ac8SRobert Watson 	 * socket and use the file descriptor reference instead of
700f7f45ac8SRobert Watson 	 * creating a new one.
701dad3b88aSMike Smith 	 */
702745aaef5SKonstantin Belousov 	error = fgetsock(td, args->s, &so, &fflag);
7034641373fSJohn Baldwin 	if (error == 0) {
7040bf301c0SJonathan Lemon 		error = EISCONN;
70539c95b83SMatthew Dillon 		if (fflag & FNONBLOCK) {
7064641373fSJohn Baldwin 			SOCK_LOCK(so);
7075002a60fSMarcel Moolenaar 			if (so->so_emuldata == 0)
7080bf301c0SJonathan Lemon 				error = so->so_error;
7090bf301c0SJonathan Lemon 			so->so_emuldata = (void *)1;
7104641373fSJohn Baldwin 			SOCK_UNLOCK(so);
711dad3b88aSMike Smith 		}
71239c95b83SMatthew Dillon 		fputsock(so);
7134641373fSJohn Baldwin 	}
7143f3a4815SMarcel Moolenaar 	return (error);
715c21dee17SSøren Schmidt }
716c21dee17SSøren Schmidt 
717c21dee17SSøren Schmidt struct linux_listen_args {
718c21dee17SSøren Schmidt 	int s;
719c21dee17SSøren Schmidt 	int backlog;
720c21dee17SSøren Schmidt };
721c21dee17SSøren Schmidt 
722c21dee17SSøren Schmidt static int
723b40ce416SJulian Elischer linux_listen(struct thread *td, struct linux_listen_args *args)
724c21dee17SSøren Schmidt {
725ef04503dSPeter Wemm 	struct listen_args /* {
726c21dee17SSøren Schmidt 		int s;
727c21dee17SSøren Schmidt 		int backlog;
728ef04503dSPeter Wemm 	} */ bsd_args;
729c21dee17SSøren Schmidt 
730745aaef5SKonstantin Belousov 	bsd_args.s = args->s;
731745aaef5SKonstantin Belousov 	bsd_args.backlog = args->backlog;
732b40ce416SJulian Elischer 	return (listen(td, &bsd_args));
733c21dee17SSøren Schmidt }
734c21dee17SSøren Schmidt 
73501e0ffbaSAlexander Leidinger struct linux_accept_args {
736c21dee17SSøren Schmidt 	int s;
7374af27623STim J. Robbins 	l_uintptr_t addr;
7384af27623STim J. Robbins 	l_uintptr_t namelen;
739c21dee17SSøren Schmidt };
740c21dee17SSøren Schmidt 
74101e0ffbaSAlexander Leidinger static int
742b40ce416SJulian Elischer linux_accept(struct thread *td, struct linux_accept_args *args)
743c21dee17SSøren Schmidt {
744ef04503dSPeter Wemm 	struct accept_args /* {
745c21dee17SSøren Schmidt 		int	s;
7463db2a843SBruce Evans 		struct sockaddr * __restrict name;
7473db2a843SBruce Evans 		socklen_t * __restrict anamelen;
748ef04503dSPeter Wemm 	} */ bsd_args;
7492ca25ab5SJohn Baldwin 	int error, fd;
750c21dee17SSøren Schmidt 
751745aaef5SKonstantin Belousov 	bsd_args.s = args->s;
7523db2a843SBruce Evans 	/* XXX: */
753745aaef5SKonstantin Belousov 	bsd_args.name = (struct sockaddr * __restrict)PTRIN(args->addr);
754745aaef5SKonstantin Belousov 	bsd_args.anamelen = PTRIN(args->namelen);/* XXX */
7555c8919adSAlexander Leidinger 	error = accept(td, &bsd_args);
7565c8919adSAlexander Leidinger 	bsd_to_linux_sockaddr((struct sockaddr *)bsd_args.name);
757d4b7423fSAlexander Leidinger 	if (error) {
758745aaef5SKonstantin Belousov 		if (error == EFAULT && args->namelen != sizeof(struct sockaddr_in))
759d4b7423fSAlexander Leidinger 			return (EINVAL);
760dba5ab66SMarcel Moolenaar 		return (error);
761d4b7423fSAlexander Leidinger 	}
762745aaef5SKonstantin Belousov 	if (args->addr) {
763745aaef5SKonstantin Belousov 		error = linux_sa_put(PTRIN(args->addr));
764ca26842eSHajimu UMEMOTO 		if (error) {
765c1cccebeSJohn Baldwin 			(void)kern_close(td, td->td_retval[0]);
766ca26842eSHajimu UMEMOTO 			return (error);
767ca26842eSHajimu UMEMOTO 		}
768ca26842eSHajimu UMEMOTO 	}
769dba5ab66SMarcel Moolenaar 
770dba5ab66SMarcel Moolenaar 	/*
771dba5ab66SMarcel Moolenaar 	 * linux appears not to copy flags from the parent socket to the
772dba5ab66SMarcel Moolenaar 	 * accepted one, so we must clear the flags in the new descriptor.
773dba5ab66SMarcel Moolenaar 	 * Ignore any errors, because we already have an open fd.
774dba5ab66SMarcel Moolenaar 	 */
7752ca25ab5SJohn Baldwin 	fd = td->td_retval[0];
7762ca25ab5SJohn Baldwin 	(void)kern_fcntl(td, fd, F_SETFL, 0);
7772ca25ab5SJohn Baldwin 	td->td_retval[0] = fd;
778dba5ab66SMarcel Moolenaar 	return (0);
779c21dee17SSøren Schmidt }
780c21dee17SSøren Schmidt 
78101e0ffbaSAlexander Leidinger struct linux_getsockname_args {
782c21dee17SSøren Schmidt 	int s;
7834af27623STim J. Robbins 	l_uintptr_t addr;
7844af27623STim J. Robbins 	l_uintptr_t namelen;
785c21dee17SSøren Schmidt };
786c21dee17SSøren Schmidt 
78701e0ffbaSAlexander Leidinger static int
788b40ce416SJulian Elischer linux_getsockname(struct thread *td, struct linux_getsockname_args *args)
789c21dee17SSøren Schmidt {
790ef04503dSPeter Wemm 	struct getsockname_args /* {
791c21dee17SSøren Schmidt 		int	fdes;
7923db2a843SBruce Evans 		struct sockaddr * __restrict asa;
7933db2a843SBruce Evans 		socklen_t * __restrict alen;
794ef04503dSPeter Wemm 	} */ bsd_args;
795c21dee17SSøren Schmidt 	int error;
796c21dee17SSøren Schmidt 
797745aaef5SKonstantin Belousov 	bsd_args.fdes = args->s;
7983db2a843SBruce Evans 	/* XXX: */
799745aaef5SKonstantin Belousov 	bsd_args.asa = (struct sockaddr * __restrict)PTRIN(args->addr);
800745aaef5SKonstantin Belousov 	bsd_args.alen = PTRIN(args->namelen);	/* XXX */
8015c8919adSAlexander Leidinger 	error = getsockname(td, &bsd_args);
8025c8919adSAlexander Leidinger 	bsd_to_linux_sockaddr((struct sockaddr *)bsd_args.asa);
803ca26842eSHajimu UMEMOTO 	if (error)
804ca26842eSHajimu UMEMOTO 		return (error);
805745aaef5SKonstantin Belousov 	error = linux_sa_put(PTRIN(args->addr));
806ca26842eSHajimu UMEMOTO 	if (error)
807ca26842eSHajimu UMEMOTO 		return (error);
808ca26842eSHajimu UMEMOTO 	return (0);
809c21dee17SSøren Schmidt }
810c21dee17SSøren Schmidt 
81101e0ffbaSAlexander Leidinger struct linux_getpeername_args {
812c21dee17SSøren Schmidt 	int s;
8134af27623STim J. Robbins 	l_uintptr_t addr;
8144af27623STim J. Robbins 	l_uintptr_t namelen;
815c21dee17SSøren Schmidt };
816c21dee17SSøren Schmidt 
81701e0ffbaSAlexander Leidinger static int
818b40ce416SJulian Elischer linux_getpeername(struct thread *td, struct linux_getpeername_args *args)
819c21dee17SSøren Schmidt {
8205c8919adSAlexander Leidinger 	struct getpeername_args /* {
821c21dee17SSøren Schmidt 		int fdes;
822c21dee17SSøren Schmidt 		caddr_t asa;
823c21dee17SSøren Schmidt 		int *alen;
824ef04503dSPeter Wemm 	} */ bsd_args;
825c21dee17SSøren Schmidt 	int error;
826c21dee17SSøren Schmidt 
827745aaef5SKonstantin Belousov 	bsd_args.fdes = args->s;
828745aaef5SKonstantin Belousov 	bsd_args.asa = (struct sockaddr *)PTRIN(args->addr);
829745aaef5SKonstantin Belousov 	bsd_args.alen = (int *)PTRIN(args->namelen);
8305c8919adSAlexander Leidinger 	error = getpeername(td, &bsd_args);
8315c8919adSAlexander Leidinger 	bsd_to_linux_sockaddr((struct sockaddr *)bsd_args.asa);
832ca26842eSHajimu UMEMOTO 	if (error)
833ca26842eSHajimu UMEMOTO 		return (error);
834745aaef5SKonstantin Belousov 	error = linux_sa_put(PTRIN(args->addr));
835ca26842eSHajimu UMEMOTO 	if (error)
836ca26842eSHajimu UMEMOTO 		return (error);
837ca26842eSHajimu UMEMOTO 	return (0);
838c21dee17SSøren Schmidt }
839c21dee17SSøren Schmidt 
84001e0ffbaSAlexander Leidinger struct linux_socketpair_args {
841c21dee17SSøren Schmidt 	int domain;
842c21dee17SSøren Schmidt 	int type;
843c21dee17SSøren Schmidt 	int protocol;
8444af27623STim J. Robbins 	l_uintptr_t rsv;
845c21dee17SSøren Schmidt };
846c21dee17SSøren Schmidt 
84701e0ffbaSAlexander Leidinger static int
848b40ce416SJulian Elischer linux_socketpair(struct thread *td, struct linux_socketpair_args *args)
849c21dee17SSøren Schmidt {
850ef04503dSPeter Wemm 	struct socketpair_args /* {
851c21dee17SSøren Schmidt 		int domain;
852c21dee17SSøren Schmidt 		int type;
853c21dee17SSøren Schmidt 		int protocol;
854c21dee17SSøren Schmidt 		int *rsv;
855ef04503dSPeter Wemm 	} */ bsd_args;
856c21dee17SSøren Schmidt 
857745aaef5SKonstantin Belousov 	bsd_args.domain = linux_to_bsd_domain(args->domain);
8581a52a4abSDmitry Chagin 	if (bsd_args.domain != PF_LOCAL)
8591a52a4abSDmitry Chagin 		return (EAFNOSUPPORT);
8603f3a4815SMarcel Moolenaar 
861745aaef5SKonstantin Belousov 	bsd_args.type = args->type;
8621a52a4abSDmitry Chagin 	if (args->protocol != 0 && args->protocol != PF_UNIX)
8631a52a4abSDmitry Chagin 
8641a52a4abSDmitry Chagin 		/*
8651a52a4abSDmitry Chagin 		 * Use of PF_UNIX as protocol argument is not right,
8661a52a4abSDmitry Chagin 		 * but Linux does it.
8671a52a4abSDmitry Chagin 		 * Do not map PF_UNIX as its Linux value is identical
8681a52a4abSDmitry Chagin 		 * to FreeBSD one.
8691a52a4abSDmitry Chagin 		 */
8701a52a4abSDmitry Chagin 		return (EPROTONOSUPPORT);
87140092d93SDmitry Chagin 	else
8721a52a4abSDmitry Chagin 		bsd_args.protocol = 0;
873745aaef5SKonstantin Belousov 	bsd_args.rsv = (int *)PTRIN(args->rsv);
874b40ce416SJulian Elischer 	return (socketpair(td, &bsd_args));
875c21dee17SSøren Schmidt }
876c21dee17SSøren Schmidt 
87701e0ffbaSAlexander Leidinger struct linux_send_args {
878c21dee17SSøren Schmidt 	int s;
8794af27623STim J. Robbins 	l_uintptr_t msg;
880c21dee17SSøren Schmidt 	int len;
881c21dee17SSøren Schmidt 	int flags;
882c21dee17SSøren Schmidt };
883c21dee17SSøren Schmidt 
88401e0ffbaSAlexander Leidinger static int
885b40ce416SJulian Elischer linux_send(struct thread *td, struct linux_send_args *args)
886c21dee17SSøren Schmidt {
88787d72a8fSPoul-Henning Kamp 	struct sendto_args /* {
888c21dee17SSøren Schmidt 		int s;
889c21dee17SSøren Schmidt 		caddr_t buf;
890044af7c3SJonathan Mini 		int len;
891c21dee17SSøren Schmidt 		int flags;
89287d72a8fSPoul-Henning Kamp 		caddr_t to;
89387d72a8fSPoul-Henning Kamp 		int tolen;
894ef04503dSPeter Wemm 	} */ bsd_args;
895c21dee17SSøren Schmidt 
896745aaef5SKonstantin Belousov 	bsd_args.s = args->s;
897745aaef5SKonstantin Belousov 	bsd_args.buf = (caddr_t)PTRIN(args->msg);
898745aaef5SKonstantin Belousov 	bsd_args.len = args->len;
899745aaef5SKonstantin Belousov 	bsd_args.flags = args->flags;
90087d72a8fSPoul-Henning Kamp 	bsd_args.to = NULL;
90187d72a8fSPoul-Henning Kamp 	bsd_args.tolen = 0;
9028d6e40c3SMaxim Sobolev 	return sendto(td, &bsd_args);
903c21dee17SSøren Schmidt }
904c21dee17SSøren Schmidt 
90501e0ffbaSAlexander Leidinger struct linux_recv_args {
906c21dee17SSøren Schmidt 	int s;
9074af27623STim J. Robbins 	l_uintptr_t msg;
908c21dee17SSøren Schmidt 	int len;
909c21dee17SSøren Schmidt 	int flags;
910c21dee17SSøren Schmidt };
911c21dee17SSøren Schmidt 
91201e0ffbaSAlexander Leidinger static int
913b40ce416SJulian Elischer linux_recv(struct thread *td, struct linux_recv_args *args)
914c21dee17SSøren Schmidt {
91587d72a8fSPoul-Henning Kamp 	struct recvfrom_args /* {
916c21dee17SSøren Schmidt 		int s;
917c21dee17SSøren Schmidt 		caddr_t buf;
918c21dee17SSøren Schmidt 		int len;
919c21dee17SSøren Schmidt 		int flags;
92087d72a8fSPoul-Henning Kamp 		struct sockaddr *from;
92187d72a8fSPoul-Henning Kamp 		socklen_t fromlenaddr;
922ef04503dSPeter Wemm 	} */ bsd_args;
923c21dee17SSøren Schmidt 
924745aaef5SKonstantin Belousov 	bsd_args.s = args->s;
925745aaef5SKonstantin Belousov 	bsd_args.buf = (caddr_t)PTRIN(args->msg);
926745aaef5SKonstantin Belousov 	bsd_args.len = args->len;
9273980a435SDmitry Chagin 	bsd_args.flags = linux_to_bsd_msg_flags(args->flags);
92887d72a8fSPoul-Henning Kamp 	bsd_args.from = NULL;
92987d72a8fSPoul-Henning Kamp 	bsd_args.fromlenaddr = 0;
93087d72a8fSPoul-Henning Kamp 	return (recvfrom(td, &bsd_args));
931c21dee17SSøren Schmidt }
932c21dee17SSøren Schmidt 
933c21dee17SSøren Schmidt static int
934b40ce416SJulian Elischer linux_sendto(struct thread *td, struct linux_sendto_args *args)
935c21dee17SSøren Schmidt {
9365a8a13e0SDavid Malone 	struct msghdr msg;
9375a8a13e0SDavid Malone 	struct iovec aiov;
9385a8a13e0SDavid Malone 	int error;
939c21dee17SSøren Schmidt 
940745aaef5SKonstantin Belousov 	if (linux_check_hdrincl(td, args->s) == 0)
941f2477ae1SMike Smith 		/* IP_HDRINCL set, tweak the packet before sending */
942745aaef5SKonstantin Belousov 		return (linux_sendto_hdrincl(td, args));
943f2477ae1SMike Smith 
944745aaef5SKonstantin Belousov 	msg.msg_name = PTRIN(args->to);
945745aaef5SKonstantin Belousov 	msg.msg_namelen = args->tolen;
9465a8a13e0SDavid Malone 	msg.msg_iov = &aiov;
9475a8a13e0SDavid Malone 	msg.msg_iovlen = 1;
9485a8a13e0SDavid Malone 	msg.msg_control = NULL;
9495a8a13e0SDavid Malone 	msg.msg_flags = 0;
950745aaef5SKonstantin Belousov 	aiov.iov_base = PTRIN(args->msg);
951745aaef5SKonstantin Belousov 	aiov.iov_len = args->len;
95274f5d680SKonstantin Belousov 	error = linux_sendit(td, args->s, &msg, args->flags, NULL,
95374f5d680SKonstantin Belousov 	    UIO_USERSPACE);
9545a8a13e0SDavid Malone 	return (error);
955c21dee17SSøren Schmidt }
956c21dee17SSøren Schmidt 
95701e0ffbaSAlexander Leidinger struct linux_recvfrom_args {
958c21dee17SSøren Schmidt 	int s;
9594af27623STim J. Robbins 	l_uintptr_t buf;
960c21dee17SSøren Schmidt 	int len;
961c21dee17SSøren Schmidt 	int flags;
9624af27623STim J. Robbins 	l_uintptr_t from;
9634af27623STim J. Robbins 	l_uintptr_t fromlen;
964c21dee17SSøren Schmidt };
965c21dee17SSøren Schmidt 
96601e0ffbaSAlexander Leidinger static int
967b40ce416SJulian Elischer linux_recvfrom(struct thread *td, struct linux_recvfrom_args *args)
968c21dee17SSøren Schmidt {
969ef04503dSPeter Wemm 	struct recvfrom_args /* {
970c21dee17SSøren Schmidt 		int	s;
971c21dee17SSøren Schmidt 		caddr_t	buf;
972c21dee17SSøren Schmidt 		size_t	len;
973c21dee17SSøren Schmidt 		int	flags;
9743db2a843SBruce Evans 		struct sockaddr * __restrict from;
9753db2a843SBruce Evans 		socklen_t * __restrict fromlenaddr;
976ef04503dSPeter Wemm 	} */ bsd_args;
9775c8919adSAlexander Leidinger 	size_t len;
978c21dee17SSøren Schmidt 	int error;
979c21dee17SSøren Schmidt 
980745aaef5SKonstantin Belousov 	if ((error = copyin(PTRIN(args->fromlen), &len, sizeof(size_t))))
9813f3a4815SMarcel Moolenaar 		return (error);
9823f3a4815SMarcel Moolenaar 
983745aaef5SKonstantin Belousov 	bsd_args.s = args->s;
984745aaef5SKonstantin Belousov 	bsd_args.buf = PTRIN(args->buf);
985745aaef5SKonstantin Belousov 	bsd_args.len = args->len;
986745aaef5SKonstantin Belousov 	bsd_args.flags = linux_to_bsd_msg_flags(args->flags);
9873db2a843SBruce Evans 	/* XXX: */
988745aaef5SKonstantin Belousov 	bsd_args.from = (struct sockaddr * __restrict)PTRIN(args->from);
989745aaef5SKonstantin Belousov 	bsd_args.fromlenaddr = PTRIN(args->fromlen);/* XXX */
9905c8919adSAlexander Leidinger 
9915c8919adSAlexander Leidinger 	linux_to_bsd_sockaddr((struct sockaddr *)bsd_args.from, len);
9925c8919adSAlexander Leidinger 	error = recvfrom(td, &bsd_args);
9935c8919adSAlexander Leidinger 	bsd_to_linux_sockaddr((struct sockaddr *)bsd_args.from);
9945c8919adSAlexander Leidinger 
995ca26842eSHajimu UMEMOTO 	if (error)
996ca26842eSHajimu UMEMOTO 		return (error);
997745aaef5SKonstantin Belousov 	if (args->from) {
9984af27623STim J. Robbins 		error = linux_sa_put((struct osockaddr *)
999745aaef5SKonstantin Belousov 		    PTRIN(args->from));
1000ca26842eSHajimu UMEMOTO 		if (error)
1001ca26842eSHajimu UMEMOTO 			return (error);
1002ca26842eSHajimu UMEMOTO 	}
1003ca26842eSHajimu UMEMOTO 	return (0);
1004ca26842eSHajimu UMEMOTO }
1005ca26842eSHajimu UMEMOTO 
100601e0ffbaSAlexander Leidinger struct linux_sendmsg_args {
1007ca26842eSHajimu UMEMOTO 	int s;
10084af27623STim J. Robbins 	l_uintptr_t msg;
1009ca26842eSHajimu UMEMOTO 	int flags;
1010ca26842eSHajimu UMEMOTO };
1011ca26842eSHajimu UMEMOTO 
101201e0ffbaSAlexander Leidinger static int
1013ca26842eSHajimu UMEMOTO linux_sendmsg(struct thread *td, struct linux_sendmsg_args *args)
1014ca26842eSHajimu UMEMOTO {
101574f5d680SKonstantin Belousov 	struct cmsghdr *cmsg;
101674f5d680SKonstantin Belousov 	struct mbuf *control;
1017ca26842eSHajimu UMEMOTO 	struct msghdr msg;
101874f5d680SKonstantin Belousov 	struct l_cmsghdr linux_cmsg;
101974f5d680SKonstantin Belousov 	struct l_cmsghdr *ptr_cmsg;
102074f5d680SKonstantin Belousov 	struct l_msghdr linux_msg;
1021552afd9cSPoul-Henning Kamp 	struct iovec *iov;
102274f5d680SKonstantin Belousov 	socklen_t datalen;
102374f5d680SKonstantin Belousov 	void *data;
1024ca26842eSHajimu UMEMOTO 	int error;
1025ca26842eSHajimu UMEMOTO 
102674f5d680SKonstantin Belousov 	error = copyin(PTRIN(args->msg), &linux_msg, sizeof(linux_msg));
102774f5d680SKonstantin Belousov 	if (error)
102874f5d680SKonstantin Belousov 		return (error);
102974f5d680SKonstantin Belousov 	error = linux_to_bsd_msghdr(&msg, &linux_msg);
1030ca26842eSHajimu UMEMOTO 	if (error)
1031ca26842eSHajimu UMEMOTO 		return (error);
1032d72a6158SRobert Watson 
1033d72a6158SRobert Watson 	/*
1034d72a6158SRobert Watson 	 * Some Linux applications (ping) define a non-NULL control data
1035d72a6158SRobert Watson 	 * pointer, but a msg_controllen of 0, which is not allowed in the
1036d72a6158SRobert Watson 	 * FreeBSD system call interface.  NULL the msg_control pointer in
1037d72a6158SRobert Watson 	 * order to handle this case.  This should be checked, but allows the
1038d72a6158SRobert Watson 	 * Linux ping to work.
1039d72a6158SRobert Watson 	 */
1040d72a6158SRobert Watson 	if (msg.msg_control != NULL && msg.msg_controllen == 0)
1041d72a6158SRobert Watson 		msg.msg_control = NULL;
104274f5d680SKonstantin Belousov 
104374f5d680SKonstantin Belousov #ifdef COMPAT_LINUX32
104474f5d680SKonstantin Belousov 	error = linux32_copyiniov(PTRIN(msg.msg_iov), msg.msg_iovlen,
104574f5d680SKonstantin Belousov 	    &iov, EMSGSIZE);
104674f5d680SKonstantin Belousov #else
1047552afd9cSPoul-Henning Kamp 	error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
104874f5d680SKonstantin Belousov #endif
1049552afd9cSPoul-Henning Kamp 	if (error)
1050552afd9cSPoul-Henning Kamp 		return (error);
105174f5d680SKonstantin Belousov 
105274f5d680SKonstantin Belousov 	if (msg.msg_control != NULL) {
105374f5d680SKonstantin Belousov 		error = ENOBUFS;
105474f5d680SKonstantin Belousov 		cmsg = malloc(CMSG_HDRSZ, M_TEMP, M_WAITOK | M_ZERO);
105574f5d680SKonstantin Belousov 		control = m_get(M_WAIT, MT_CONTROL);
105674f5d680SKonstantin Belousov 		if (control == NULL)
105774f5d680SKonstantin Belousov 			goto bad;
105874f5d680SKonstantin Belousov 		ptr_cmsg = LINUX_CMSG_FIRSTHDR(&msg);
105974f5d680SKonstantin Belousov 
106074f5d680SKonstantin Belousov 		do {
106174f5d680SKonstantin Belousov 			error = copyin(ptr_cmsg, &linux_cmsg,
106274f5d680SKonstantin Belousov 			    sizeof(struct l_cmsghdr));
106374f5d680SKonstantin Belousov 			if (error)
106474f5d680SKonstantin Belousov 				goto bad;
106574f5d680SKonstantin Belousov 
106674f5d680SKonstantin Belousov 			error = EINVAL;
106774f5d680SKonstantin Belousov 			if (linux_cmsg.cmsg_len < sizeof(struct l_cmsghdr))
106874f5d680SKonstantin Belousov 				goto bad;
106974f5d680SKonstantin Belousov 
107074f5d680SKonstantin Belousov 			/*
107174f5d680SKonstantin Belousov 			 * Now we support only SCM_RIGHTS, so return EINVAL
107274f5d680SKonstantin Belousov 			 * in any other cmsg_type
107374f5d680SKonstantin Belousov 			 */
107474f5d680SKonstantin Belousov 			if ((cmsg->cmsg_type =
107574f5d680SKonstantin Belousov 			    linux_to_bsd_cmsg_type(linux_cmsg.cmsg_type)) == -1)
107674f5d680SKonstantin Belousov 				goto bad;
107774f5d680SKonstantin Belousov 			cmsg->cmsg_level =
107874f5d680SKonstantin Belousov 			    linux_to_bsd_sockopt_level(linux_cmsg.cmsg_level);
107974f5d680SKonstantin Belousov 
108074f5d680SKonstantin Belousov 			datalen = linux_cmsg.cmsg_len - L_CMSG_HDRSZ;
108174f5d680SKonstantin Belousov 			cmsg->cmsg_len = CMSG_LEN(datalen);
108274f5d680SKonstantin Belousov 			data = LINUX_CMSG_DATA(ptr_cmsg);
108374f5d680SKonstantin Belousov 
108474f5d680SKonstantin Belousov 			error = ENOBUFS;
108574f5d680SKonstantin Belousov 			if (!m_append(control, CMSG_HDRSZ, (c_caddr_t) cmsg))
108674f5d680SKonstantin Belousov 				goto bad;
108774f5d680SKonstantin Belousov 			if (!m_append(control, datalen, (c_caddr_t) data))
108874f5d680SKonstantin Belousov 				goto bad;
108974f5d680SKonstantin Belousov 		} while ((ptr_cmsg = LINUX_CMSG_NXTHDR(&msg, ptr_cmsg)));
109074f5d680SKonstantin Belousov 	} else {
109174f5d680SKonstantin Belousov 		control = NULL;
109274f5d680SKonstantin Belousov 		cmsg = NULL;
109374f5d680SKonstantin Belousov 	}
109474f5d680SKonstantin Belousov 
10955a8a13e0SDavid Malone 	msg.msg_iov = iov;
10965a8a13e0SDavid Malone 	msg.msg_flags = 0;
109774f5d680SKonstantin Belousov 	error = linux_sendit(td, args->s, &msg, args->flags, control,
109874f5d680SKonstantin Belousov 	    UIO_USERSPACE);
109974f5d680SKonstantin Belousov 
110074f5d680SKonstantin Belousov bad:
1101552afd9cSPoul-Henning Kamp 	free(iov, M_IOV);
110274f5d680SKonstantin Belousov 	if (cmsg)
110374f5d680SKonstantin Belousov 		free(cmsg, M_TEMP);
1104ca26842eSHajimu UMEMOTO 	return (error);
1105c21dee17SSøren Schmidt }
1106c21dee17SSøren Schmidt 
110701e0ffbaSAlexander Leidinger struct linux_recvmsg_args {
110840dbba57SAssar Westerlund 	int s;
11094af27623STim J. Robbins 	l_uintptr_t msg;
111040dbba57SAssar Westerlund 	int flags;
111140dbba57SAssar Westerlund };
111240dbba57SAssar Westerlund 
111301e0ffbaSAlexander Leidinger static int
1114b40ce416SJulian Elischer linux_recvmsg(struct thread *td, struct linux_recvmsg_args *args)
111540dbba57SAssar Westerlund {
111674f5d680SKonstantin Belousov 	struct cmsghdr *cm;
1117ca26842eSHajimu UMEMOTO 	struct msghdr msg;
111874f5d680SKonstantin Belousov 	struct l_cmsghdr *linux_cmsg = NULL;
111974f5d680SKonstantin Belousov 	socklen_t datalen, outlen, clen;
112074f5d680SKonstantin Belousov 	struct l_msghdr linux_msg;
112174f5d680SKonstantin Belousov 	struct iovec *iov, *uiov;
112274f5d680SKonstantin Belousov 	struct mbuf *control = NULL;
112374f5d680SKonstantin Belousov 	struct mbuf **controlp;
112474f5d680SKonstantin Belousov 	caddr_t outbuf;
112574f5d680SKonstantin Belousov 	void *data;
112640dbba57SAssar Westerlund 	int error;
112740dbba57SAssar Westerlund 
112874f5d680SKonstantin Belousov 	error = copyin(PTRIN(args->msg), &linux_msg, sizeof(linux_msg));
1129ca26842eSHajimu UMEMOTO 	if (error)
1130ca26842eSHajimu UMEMOTO 		return (error);
1131ca26842eSHajimu UMEMOTO 
113274f5d680SKonstantin Belousov 	error = linux_to_bsd_msghdr(&msg, &linux_msg);
113374f5d680SKonstantin Belousov 	if (error)
113474f5d680SKonstantin Belousov 		return (error);
113574f5d680SKonstantin Belousov 
113674f5d680SKonstantin Belousov #ifdef COMPAT_LINUX32
113774f5d680SKonstantin Belousov 	error = linux32_copyiniov(PTRIN(msg.msg_iov), msg.msg_iovlen,
113874f5d680SKonstantin Belousov 	    &iov, EMSGSIZE);
113974f5d680SKonstantin Belousov #else
114074f5d680SKonstantin Belousov 	error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
114174f5d680SKonstantin Belousov #endif
114274f5d680SKonstantin Belousov 	if (error)
114374f5d680SKonstantin Belousov 		return (error);
114474f5d680SKonstantin Belousov 
114574f5d680SKonstantin Belousov 	if (msg.msg_name) {
114674f5d680SKonstantin Belousov 		error = linux_to_bsd_sockaddr((struct sockaddr *)msg.msg_name,
114774f5d680SKonstantin Belousov 		    msg.msg_namelen);
114874f5d680SKonstantin Belousov 		if (error)
114974f5d680SKonstantin Belousov 			goto bad;
115084b11cd8SMitsuru IWASAKI 	}
115184b11cd8SMitsuru IWASAKI 
115274f5d680SKonstantin Belousov 	uiov = msg.msg_iov;
115374f5d680SKonstantin Belousov 	msg.msg_iov = iov;
115474f5d680SKonstantin Belousov 	controlp = (msg.msg_control != NULL) ? &control : NULL;
115574f5d680SKonstantin Belousov 	error = kern_recvit(td, args->s, &msg, UIO_USERSPACE, controlp);
115674f5d680SKonstantin Belousov 	msg.msg_iov = uiov;
1157ca26842eSHajimu UMEMOTO 	if (error)
115874f5d680SKonstantin Belousov 		goto bad;
115974f5d680SKonstantin Belousov 
116074f5d680SKonstantin Belousov 	error = bsd_to_linux_msghdr(&msg, &linux_msg);
116174f5d680SKonstantin Belousov 	if (error)
116274f5d680SKonstantin Belousov 		goto bad;
116374f5d680SKonstantin Belousov 
116474f5d680SKonstantin Belousov 	if (linux_msg.msg_name) {
116574f5d680SKonstantin Belousov 		error = bsd_to_linux_sockaddr((struct sockaddr *)
116674f5d680SKonstantin Belousov 		    PTRIN(linux_msg.msg_name));
116774f5d680SKonstantin Belousov 		if (error)
116874f5d680SKonstantin Belousov 			goto bad;
116974f5d680SKonstantin Belousov 	}
117074f5d680SKonstantin Belousov 	if (linux_msg.msg_name && linux_msg.msg_namelen > 2) {
117174f5d680SKonstantin Belousov 		error = linux_sa_put(PTRIN(linux_msg.msg_name));
117274f5d680SKonstantin Belousov 		if (error)
117374f5d680SKonstantin Belousov 			goto bad;
117474f5d680SKonstantin Belousov 	}
117574f5d680SKonstantin Belousov 
117674f5d680SKonstantin Belousov 	if (control) {
117774f5d680SKonstantin Belousov 
117874f5d680SKonstantin Belousov 		linux_cmsg = malloc(L_CMSG_HDRSZ, M_TEMP, M_WAITOK | M_ZERO);
117974f5d680SKonstantin Belousov 		outbuf = PTRIN(linux_msg.msg_control);
118074f5d680SKonstantin Belousov 		cm = mtod(control, struct cmsghdr *);
118174f5d680SKonstantin Belousov 		outlen = 0;
118274f5d680SKonstantin Belousov 		clen = control->m_len;
118374f5d680SKonstantin Belousov 
118474f5d680SKonstantin Belousov 		while (cm != NULL) {
118574f5d680SKonstantin Belousov 
118674f5d680SKonstantin Belousov 			if ((linux_cmsg->cmsg_type =
118774f5d680SKonstantin Belousov 			    bsd_to_linux_cmsg_type(cm->cmsg_type)) == -1)
118874f5d680SKonstantin Belousov 			{
118974f5d680SKonstantin Belousov 				error = EINVAL;
119074f5d680SKonstantin Belousov 				goto bad;
119174f5d680SKonstantin Belousov 			}
119274f5d680SKonstantin Belousov 			data = CMSG_DATA(cm);
119374f5d680SKonstantin Belousov 			datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
119474f5d680SKonstantin Belousov 
119574f5d680SKonstantin Belousov 			if (outlen + LINUX_CMSG_LEN(datalen) >
119674f5d680SKonstantin Belousov 			    linux_msg.msg_controllen) {
119774f5d680SKonstantin Belousov 				if (outlen == 0) {
119874f5d680SKonstantin Belousov 					error = EMSGSIZE;
119974f5d680SKonstantin Belousov 					goto bad;
120074f5d680SKonstantin Belousov 				} else {
120174f5d680SKonstantin Belousov 					linux_msg.msg_flags |= LINUX_MSG_CTRUNC;
120274f5d680SKonstantin Belousov 					goto out;
120374f5d680SKonstantin Belousov 				}
120474f5d680SKonstantin Belousov 			}
120574f5d680SKonstantin Belousov 
120674f5d680SKonstantin Belousov 			linux_cmsg->cmsg_len = LINUX_CMSG_LEN(datalen);
120774f5d680SKonstantin Belousov 			linux_cmsg->cmsg_level =
120874f5d680SKonstantin Belousov 			    bsd_to_linux_sockopt_level(cm->cmsg_level);
120974f5d680SKonstantin Belousov 
121074f5d680SKonstantin Belousov 			error = copyout(linux_cmsg, outbuf, L_CMSG_HDRSZ);
121174f5d680SKonstantin Belousov 			if (error)
121274f5d680SKonstantin Belousov 				goto bad;
121374f5d680SKonstantin Belousov 			outbuf += L_CMSG_HDRSZ;
121474f5d680SKonstantin Belousov 
121574f5d680SKonstantin Belousov 			error = copyout(data, outbuf, datalen);
121674f5d680SKonstantin Belousov 			if (error)
121774f5d680SKonstantin Belousov 				goto bad;
121874f5d680SKonstantin Belousov 
121974f5d680SKonstantin Belousov 			outbuf += LINUX_CMSG_ALIGN(datalen);
122074f5d680SKonstantin Belousov 			outlen += LINUX_CMSG_LEN(datalen);
122174f5d680SKonstantin Belousov 			linux_msg.msg_controllen = outlen;
122274f5d680SKonstantin Belousov 
122374f5d680SKonstantin Belousov 			if (CMSG_SPACE(datalen) < clen) {
122474f5d680SKonstantin Belousov 				clen -= CMSG_SPACE(datalen);
122574f5d680SKonstantin Belousov 				cm = (struct cmsghdr *)
122674f5d680SKonstantin Belousov 				    ((caddr_t)cm + CMSG_SPACE(datalen));
122774f5d680SKonstantin Belousov 			} else
122874f5d680SKonstantin Belousov 				cm = NULL;
122974f5d680SKonstantin Belousov 		}
123074f5d680SKonstantin Belousov 	}
123174f5d680SKonstantin Belousov 
123274f5d680SKonstantin Belousov out:
123374f5d680SKonstantin Belousov 	error = copyout(&linux_msg, PTRIN(args->msg), sizeof(linux_msg));
123474f5d680SKonstantin Belousov 
123574f5d680SKonstantin Belousov bad:
123674f5d680SKonstantin Belousov 	free(iov, M_IOV);
123774f5d680SKonstantin Belousov 	if (control != NULL)
123874f5d680SKonstantin Belousov 		m_freem(control);
123974f5d680SKonstantin Belousov 	if (linux_cmsg != NULL)
124074f5d680SKonstantin Belousov 		free(linux_cmsg, M_TEMP);
124174f5d680SKonstantin Belousov 
1242ca26842eSHajimu UMEMOTO 	return (error);
124340dbba57SAssar Westerlund }
124440dbba57SAssar Westerlund 
1245c21dee17SSøren Schmidt struct linux_shutdown_args {
1246c21dee17SSøren Schmidt 	int s;
1247c21dee17SSøren Schmidt 	int how;
1248c21dee17SSøren Schmidt };
1249c21dee17SSøren Schmidt 
1250c21dee17SSøren Schmidt static int
1251b40ce416SJulian Elischer linux_shutdown(struct thread *td, struct linux_shutdown_args *args)
1252c21dee17SSøren Schmidt {
1253ef04503dSPeter Wemm 	struct shutdown_args /* {
1254c21dee17SSøren Schmidt 		int s;
1255c21dee17SSøren Schmidt 		int how;
1256ef04503dSPeter Wemm 	} */ bsd_args;
1257c21dee17SSøren Schmidt 
1258745aaef5SKonstantin Belousov 	bsd_args.s = args->s;
1259745aaef5SKonstantin Belousov 	bsd_args.how = args->how;
1260b40ce416SJulian Elischer 	return (shutdown(td, &bsd_args));
1261c21dee17SSøren Schmidt }
1262c21dee17SSøren Schmidt 
1263c21dee17SSøren Schmidt struct linux_setsockopt_args {
1264c21dee17SSøren Schmidt 	int s;
1265c21dee17SSøren Schmidt 	int level;
1266c21dee17SSøren Schmidt 	int optname;
12674af27623STim J. Robbins 	l_uintptr_t optval;
1268c21dee17SSøren Schmidt 	int optlen;
1269c21dee17SSøren Schmidt };
1270c21dee17SSøren Schmidt 
1271c21dee17SSøren Schmidt static int
1272b40ce416SJulian Elischer linux_setsockopt(struct thread *td, struct linux_setsockopt_args *args)
1273c21dee17SSøren Schmidt {
1274ef04503dSPeter Wemm 	struct setsockopt_args /* {
1275c21dee17SSøren Schmidt 		int s;
1276c21dee17SSøren Schmidt 		int level;
1277c21dee17SSøren Schmidt 		int name;
1278c21dee17SSøren Schmidt 		caddr_t val;
1279c21dee17SSøren Schmidt 		int valsize;
1280ef04503dSPeter Wemm 	} */ bsd_args;
128103cc95d2SDmitry Chagin 	l_timeval linux_tv;
128203cc95d2SDmitry Chagin 	struct timeval tv;
1283c21dee17SSøren Schmidt 	int error, name;
1284c21dee17SSøren Schmidt 
1285745aaef5SKonstantin Belousov 	bsd_args.s = args->s;
1286745aaef5SKonstantin Belousov 	bsd_args.level = linux_to_bsd_sockopt_level(args->level);
1287c21dee17SSøren Schmidt 	switch (bsd_args.level) {
1288c21dee17SSøren Schmidt 	case SOL_SOCKET:
1289745aaef5SKonstantin Belousov 		name = linux_to_bsd_so_sockopt(args->optname);
129003cc95d2SDmitry Chagin 		switch (name) {
129103cc95d2SDmitry Chagin 		case SO_RCVTIMEO:
129203cc95d2SDmitry Chagin 			/* FALLTHROUGH */
129303cc95d2SDmitry Chagin 		case SO_SNDTIMEO:
129403cc95d2SDmitry Chagin 			error = copyin(PTRIN(args->optval), &linux_tv,
129503cc95d2SDmitry Chagin 			    sizeof(linux_tv));
129603cc95d2SDmitry Chagin 			if (error)
129703cc95d2SDmitry Chagin 				return (error);
129803cc95d2SDmitry Chagin 			tv.tv_sec = linux_tv.tv_sec;
129903cc95d2SDmitry Chagin 			tv.tv_usec = linux_tv.tv_usec;
130003cc95d2SDmitry Chagin 			return (kern_setsockopt(td, args->s, bsd_args.level,
130103cc95d2SDmitry Chagin 			    name, &tv, UIO_SYSSPACE, sizeof(tv)));
130203cc95d2SDmitry Chagin 			/* NOTREACHED */
130303cc95d2SDmitry Chagin 			break;
130403cc95d2SDmitry Chagin 		default:
130503cc95d2SDmitry Chagin 			break;
130603cc95d2SDmitry Chagin 		}
1307c21dee17SSøren Schmidt 		break;
1308c21dee17SSøren Schmidt 	case IPPROTO_IP:
1309745aaef5SKonstantin Belousov 		name = linux_to_bsd_ip_sockopt(args->optname);
1310c21dee17SSøren Schmidt 		break;
1311dad3b88aSMike Smith 	case IPPROTO_TCP:
1312dad3b88aSMike Smith 		/* Linux TCP option values match BSD's */
1313745aaef5SKonstantin Belousov 		name = args->optname;
1314dad3b88aSMike Smith 		break;
1315c21dee17SSøren Schmidt 	default:
13163f3a4815SMarcel Moolenaar 		name = -1;
13173f3a4815SMarcel Moolenaar 		break;
1318c21dee17SSøren Schmidt 	}
1319c21dee17SSøren Schmidt 	if (name == -1)
1320d4b7423fSAlexander Leidinger 		return (ENOPROTOOPT);
13213f3a4815SMarcel Moolenaar 
1322c21dee17SSøren Schmidt 	bsd_args.name = name;
1323745aaef5SKonstantin Belousov 	bsd_args.val = PTRIN(args->optval);
1324745aaef5SKonstantin Belousov 	bsd_args.valsize = args->optlen;
13255c8919adSAlexander Leidinger 
13265c8919adSAlexander Leidinger 	if (name == IPV6_NEXTHOP) {
13275c8919adSAlexander Leidinger 		linux_to_bsd_sockaddr((struct sockaddr *)bsd_args.val,
13285c8919adSAlexander Leidinger 			bsd_args.valsize);
13295c8919adSAlexander Leidinger 		error = setsockopt(td, &bsd_args);
13305c8919adSAlexander Leidinger 		bsd_to_linux_sockaddr((struct sockaddr *)bsd_args.val);
13315c8919adSAlexander Leidinger 	} else
13325c8919adSAlexander Leidinger 		error = setsockopt(td, &bsd_args);
13335c8919adSAlexander Leidinger 
13345c8919adSAlexander Leidinger 	return (error);
1335c21dee17SSøren Schmidt }
1336c21dee17SSøren Schmidt 
1337c21dee17SSøren Schmidt struct linux_getsockopt_args {
1338c21dee17SSøren Schmidt 	int s;
1339c21dee17SSøren Schmidt 	int level;
1340c21dee17SSøren Schmidt 	int optname;
13414af27623STim J. Robbins 	l_uintptr_t optval;
13424af27623STim J. Robbins 	l_uintptr_t optlen;
1343c21dee17SSøren Schmidt };
1344c21dee17SSøren Schmidt 
1345c21dee17SSøren Schmidt static int
1346b40ce416SJulian Elischer linux_getsockopt(struct thread *td, struct linux_getsockopt_args *args)
1347c21dee17SSøren Schmidt {
1348ef04503dSPeter Wemm 	struct getsockopt_args /* {
1349c21dee17SSøren Schmidt 		int s;
1350c21dee17SSøren Schmidt 		int level;
1351c21dee17SSøren Schmidt 		int name;
1352c21dee17SSøren Schmidt 		caddr_t val;
1353c21dee17SSøren Schmidt 		int *avalsize;
1354ef04503dSPeter Wemm 	} */ bsd_args;
135503cc95d2SDmitry Chagin 	l_timeval linux_tv;
135603cc95d2SDmitry Chagin 	struct timeval tv;
1357d4dd69c4SDmitry Chagin 	socklen_t tv_len, xulen;
1358d4dd69c4SDmitry Chagin 	struct xucred xu;
1359d4dd69c4SDmitry Chagin 	struct l_ucred lxu;
1360c21dee17SSøren Schmidt 	int error, name;
1361c21dee17SSøren Schmidt 
1362745aaef5SKonstantin Belousov 	bsd_args.s = args->s;
1363745aaef5SKonstantin Belousov 	bsd_args.level = linux_to_bsd_sockopt_level(args->level);
1364c21dee17SSøren Schmidt 	switch (bsd_args.level) {
1365c21dee17SSøren Schmidt 	case SOL_SOCKET:
1366745aaef5SKonstantin Belousov 		name = linux_to_bsd_so_sockopt(args->optname);
136703cc95d2SDmitry Chagin 		switch (name) {
136803cc95d2SDmitry Chagin 		case SO_RCVTIMEO:
136903cc95d2SDmitry Chagin 			/* FALLTHROUGH */
137003cc95d2SDmitry Chagin 		case SO_SNDTIMEO:
137103cc95d2SDmitry Chagin 			tv_len = sizeof(tv);
137203cc95d2SDmitry Chagin 			error = kern_getsockopt(td, args->s, bsd_args.level,
137303cc95d2SDmitry Chagin 			    name, &tv, UIO_SYSSPACE, &tv_len);
137403cc95d2SDmitry Chagin 			if (error)
137503cc95d2SDmitry Chagin 				return (error);
137603cc95d2SDmitry Chagin 			linux_tv.tv_sec = tv.tv_sec;
137703cc95d2SDmitry Chagin 			linux_tv.tv_usec = tv.tv_usec;
137803cc95d2SDmitry Chagin 			return (copyout(&linux_tv, PTRIN(args->optval),
137903cc95d2SDmitry Chagin 			    sizeof(linux_tv)));
138003cc95d2SDmitry Chagin 			/* NOTREACHED */
138103cc95d2SDmitry Chagin 			break;
1382d4dd69c4SDmitry Chagin 		case LOCAL_PEERCRED:
1383d4dd69c4SDmitry Chagin 			if (args->optlen != sizeof(lxu))
1384d4dd69c4SDmitry Chagin 				return (EINVAL);
1385d4dd69c4SDmitry Chagin 			xulen = sizeof(xu);
1386d4dd69c4SDmitry Chagin 			error = kern_getsockopt(td, args->s, bsd_args.level,
1387d4dd69c4SDmitry Chagin 			    name, &xu, UIO_SYSSPACE, &xulen);
1388d4dd69c4SDmitry Chagin 			if (error)
1389d4dd69c4SDmitry Chagin 				return (error);
1390d4dd69c4SDmitry Chagin 			/*
1391d4dd69c4SDmitry Chagin 			 * XXX Use 0 for pid as the FreeBSD does not cache peer pid.
1392d4dd69c4SDmitry Chagin 			 */
1393d4dd69c4SDmitry Chagin 			lxu.pid = 0;
1394d4dd69c4SDmitry Chagin 			lxu.uid = xu.cr_uid;
1395d4dd69c4SDmitry Chagin 			lxu.gid = xu.cr_gid;
1396d4dd69c4SDmitry Chagin 			return (copyout(&lxu, PTRIN(args->optval), sizeof(lxu)));
1397d4dd69c4SDmitry Chagin 			/* NOTREACHED */
1398d4dd69c4SDmitry Chagin 			break;
139903cc95d2SDmitry Chagin 		default:
140003cc95d2SDmitry Chagin 			break;
140103cc95d2SDmitry Chagin 		}
1402c21dee17SSøren Schmidt 		break;
1403c21dee17SSøren Schmidt 	case IPPROTO_IP:
1404745aaef5SKonstantin Belousov 		name = linux_to_bsd_ip_sockopt(args->optname);
1405c21dee17SSøren Schmidt 		break;
1406dad3b88aSMike Smith 	case IPPROTO_TCP:
1407dad3b88aSMike Smith 		/* Linux TCP option values match BSD's */
1408745aaef5SKonstantin Belousov 		name = args->optname;
1409dad3b88aSMike Smith 		break;
1410c21dee17SSøren Schmidt 	default:
14113f3a4815SMarcel Moolenaar 		name = -1;
14123f3a4815SMarcel Moolenaar 		break;
1413c21dee17SSøren Schmidt 	}
1414c21dee17SSøren Schmidt 	if (name == -1)
14153f3a4815SMarcel Moolenaar 		return (EINVAL);
14163f3a4815SMarcel Moolenaar 
1417f2477ae1SMike Smith 	bsd_args.name = name;
1418745aaef5SKonstantin Belousov 	bsd_args.val = PTRIN(args->optval);
1419745aaef5SKonstantin Belousov 	bsd_args.avalsize = PTRIN(args->optlen);
14205c8919adSAlexander Leidinger 
14215c8919adSAlexander Leidinger 	if (name == IPV6_NEXTHOP) {
14225c8919adSAlexander Leidinger 		error = getsockopt(td, &bsd_args);
14235c8919adSAlexander Leidinger 		bsd_to_linux_sockaddr((struct sockaddr *)bsd_args.val);
14245c8919adSAlexander Leidinger 	} else
14255c8919adSAlexander Leidinger 		error = getsockopt(td, &bsd_args);
14265c8919adSAlexander Leidinger 
14275c8919adSAlexander Leidinger 	return (error);
1428c21dee17SSøren Schmidt }
1429c21dee17SSøren Schmidt 
1430c21dee17SSøren Schmidt int
1431b40ce416SJulian Elischer linux_socketcall(struct thread *td, struct linux_socketcall_args *args)
1432c21dee17SSøren Schmidt {
14334af27623STim J. Robbins 	void *arg = (void *)(intptr_t)args->args;
14343f3a4815SMarcel Moolenaar 
1435c21dee17SSøren Schmidt 	switch (args->what) {
1436c21dee17SSøren Schmidt 	case LINUX_SOCKET:
1437b40ce416SJulian Elischer 		return (linux_socket(td, arg));
1438c21dee17SSøren Schmidt 	case LINUX_BIND:
1439b40ce416SJulian Elischer 		return (linux_bind(td, arg));
1440c21dee17SSøren Schmidt 	case LINUX_CONNECT:
1441b40ce416SJulian Elischer 		return (linux_connect(td, arg));
1442c21dee17SSøren Schmidt 	case LINUX_LISTEN:
1443b40ce416SJulian Elischer 		return (linux_listen(td, arg));
1444c21dee17SSøren Schmidt 	case LINUX_ACCEPT:
1445b40ce416SJulian Elischer 		return (linux_accept(td, arg));
1446c21dee17SSøren Schmidt 	case LINUX_GETSOCKNAME:
1447b40ce416SJulian Elischer 		return (linux_getsockname(td, arg));
1448c21dee17SSøren Schmidt 	case LINUX_GETPEERNAME:
1449b40ce416SJulian Elischer 		return (linux_getpeername(td, arg));
1450c21dee17SSøren Schmidt 	case LINUX_SOCKETPAIR:
1451b40ce416SJulian Elischer 		return (linux_socketpair(td, arg));
1452c21dee17SSøren Schmidt 	case LINUX_SEND:
1453b40ce416SJulian Elischer 		return (linux_send(td, arg));
1454c21dee17SSøren Schmidt 	case LINUX_RECV:
1455b40ce416SJulian Elischer 		return (linux_recv(td, arg));
1456c21dee17SSøren Schmidt 	case LINUX_SENDTO:
1457b40ce416SJulian Elischer 		return (linux_sendto(td, arg));
1458c21dee17SSøren Schmidt 	case LINUX_RECVFROM:
1459b40ce416SJulian Elischer 		return (linux_recvfrom(td, arg));
1460c21dee17SSøren Schmidt 	case LINUX_SHUTDOWN:
1461b40ce416SJulian Elischer 		return (linux_shutdown(td, arg));
1462c21dee17SSøren Schmidt 	case LINUX_SETSOCKOPT:
1463b40ce416SJulian Elischer 		return (linux_setsockopt(td, arg));
1464c21dee17SSøren Schmidt 	case LINUX_GETSOCKOPT:
1465b40ce416SJulian Elischer 		return (linux_getsockopt(td, arg));
1466e76bba09SSøren Schmidt 	case LINUX_SENDMSG:
1467ca26842eSHajimu UMEMOTO 		return (linux_sendmsg(td, arg));
1468e76bba09SSøren Schmidt 	case LINUX_RECVMSG:
1469b40ce416SJulian Elischer 		return (linux_recvmsg(td, arg));
1470c21dee17SSøren Schmidt 	}
14713f3a4815SMarcel Moolenaar 
14723f3a4815SMarcel Moolenaar 	uprintf("LINUX: 'socket' typ=%d not implemented\n", args->what);
14733f3a4815SMarcel Moolenaar 	return (ENOSYS);
1474c21dee17SSøren Schmidt }
1475