xref: /freebsd/sys/compat/linux/linux_socket.c (revision 1ede983cc905643549d8cae56a9d0e28fc68375f)
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 
55c21dee17SSøren Schmidt #include <netinet/in.h>
56f2477ae1SMike Smith #include <netinet/in_systm.h>
57f2477ae1SMike Smith #include <netinet/ip.h>
58ca26842eSHajimu UMEMOTO #ifdef INET6
59ca26842eSHajimu UMEMOTO #include <netinet/ip6.h>
60ca26842eSHajimu UMEMOTO #include <netinet6/ip6_var.h>
61ca26842eSHajimu UMEMOTO #endif
62c21dee17SSøren Schmidt 
631997c537SDavid E. O'Brien #ifdef COMPAT_LINUX32
644af27623STim J. Robbins #include <machine/../linux32/linux.h>
654af27623STim J. Robbins #include <machine/../linux32/linux32_proto.h>
661997c537SDavid E. O'Brien #else
671997c537SDavid E. O'Brien #include <machine/../linux/linux.h>
681997c537SDavid E. O'Brien #include <machine/../linux/linux_proto.h>
694af27623STim J. Robbins #endif
7040dbba57SAssar Westerlund #include <compat/linux/linux_socket.h>
71ac951e62SMarcel Moolenaar #include <compat/linux/linux_util.h>
72c21dee17SSøren Schmidt 
73ca26842eSHajimu UMEMOTO static int do_sa_get(struct sockaddr **, const struct osockaddr *, int *,
74ca26842eSHajimu UMEMOTO     struct malloc_type *);
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.
804730796cSBill Fenner  */
814730796cSBill Fenner static int
82ca26842eSHajimu UMEMOTO linux_getsockaddr(struct sockaddr **sap, const struct osockaddr *osa, int len)
834730796cSBill Fenner {
84ca26842eSHajimu UMEMOTO 	int osalen = len;
854730796cSBill Fenner 
86ca26842eSHajimu UMEMOTO 	return (do_sa_get(sap, osa, &osalen, M_SONAME));
874730796cSBill Fenner }
884730796cSBill Fenner 
89ca26842eSHajimu UMEMOTO /*
90ca26842eSHajimu UMEMOTO  * Copy the osockaddr structure pointed to by osa to kernel, adjust
91ca26842eSHajimu UMEMOTO  * family and convert to sockaddr.
92ca26842eSHajimu UMEMOTO  */
93ca26842eSHajimu UMEMOTO static int
94ca26842eSHajimu UMEMOTO do_sa_get(struct sockaddr **sap, const struct osockaddr *osa, int *osalen,
95ca26842eSHajimu UMEMOTO     struct malloc_type *mtype)
96ca26842eSHajimu UMEMOTO {
97ca26842eSHajimu UMEMOTO 	int error=0, bdom;
98ca26842eSHajimu UMEMOTO 	struct sockaddr *sa;
99ca26842eSHajimu UMEMOTO 	struct osockaddr *kosa;
100ca26842eSHajimu UMEMOTO 	int alloclen;
101ca26842eSHajimu UMEMOTO #ifdef INET6
102ca26842eSHajimu UMEMOTO 	int oldv6size;
103ca26842eSHajimu UMEMOTO 	struct sockaddr_in6 *sin6;
104ca26842eSHajimu UMEMOTO #endif
105ca26842eSHajimu UMEMOTO 
106ca26842eSHajimu UMEMOTO 	if (*osalen < 2 || *osalen > UCHAR_MAX || !osa)
107ca26842eSHajimu UMEMOTO 		return (EINVAL);
108ca26842eSHajimu UMEMOTO 
109ca26842eSHajimu UMEMOTO 	alloclen = *osalen;
110ca26842eSHajimu UMEMOTO #ifdef INET6
111ca26842eSHajimu UMEMOTO 	oldv6size = 0;
112ca26842eSHajimu UMEMOTO 	/*
113ca26842eSHajimu UMEMOTO 	 * Check for old (pre-RFC2553) sockaddr_in6. We may accept it
114ca26842eSHajimu UMEMOTO 	 * if it's a v4-mapped address, so reserve the proper space
115ca26842eSHajimu UMEMOTO 	 * for it.
116ca26842eSHajimu UMEMOTO 	 */
117ca26842eSHajimu UMEMOTO 	if (alloclen == sizeof (struct sockaddr_in6) - sizeof (u_int32_t)) {
118ca26842eSHajimu UMEMOTO 		alloclen = sizeof (struct sockaddr_in6);
119ca26842eSHajimu UMEMOTO 		oldv6size = 1;
120ca26842eSHajimu UMEMOTO 	}
121ca26842eSHajimu UMEMOTO #endif
122ca26842eSHajimu UMEMOTO 
1231ede983cSDag-Erling Smørgrav 	kosa = malloc(alloclen, mtype, M_WAITOK);
124ca26842eSHajimu UMEMOTO 
1254b7ef73dSDag-Erling Smørgrav 	if ((error = copyin(osa, kosa, *osalen)))
126ca26842eSHajimu UMEMOTO 		goto out;
127ca26842eSHajimu UMEMOTO 
128ca26842eSHajimu UMEMOTO 	bdom = linux_to_bsd_domain(kosa->sa_family);
129ca26842eSHajimu UMEMOTO 	if (bdom == -1) {
130ca26842eSHajimu UMEMOTO 		error = EINVAL;
131ca26842eSHajimu UMEMOTO 		goto out;
132ca26842eSHajimu UMEMOTO 	}
133ca26842eSHajimu UMEMOTO 
134ca26842eSHajimu UMEMOTO #ifdef INET6
135ca26842eSHajimu UMEMOTO 	/*
136ca26842eSHajimu UMEMOTO 	 * Older Linux IPv6 code uses obsolete RFC2133 struct sockaddr_in6,
137ca26842eSHajimu UMEMOTO 	 * which lacks the scope id compared with RFC2553 one. If we detect
138ca26842eSHajimu UMEMOTO 	 * the situation, reject the address and write a message to system log.
139ca26842eSHajimu UMEMOTO 	 *
140ca26842eSHajimu UMEMOTO 	 * Still accept addresses for which the scope id is not used.
141ca26842eSHajimu UMEMOTO 	 */
142ca26842eSHajimu UMEMOTO 	if (oldv6size && bdom == AF_INET6) {
143ca26842eSHajimu UMEMOTO 		sin6 = (struct sockaddr_in6 *)kosa;
144ca26842eSHajimu UMEMOTO 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr) ||
145ca26842eSHajimu UMEMOTO 		    (!IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) &&
146ca26842eSHajimu UMEMOTO 		     !IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr) &&
147ca26842eSHajimu UMEMOTO 		     !IN6_IS_ADDR_V4COMPAT(&sin6->sin6_addr) &&
148ca26842eSHajimu UMEMOTO 		     !IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) &&
149ca26842eSHajimu UMEMOTO 		     !IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))) {
150ca26842eSHajimu UMEMOTO 			sin6->sin6_scope_id = 0;
151ca26842eSHajimu UMEMOTO 		} else {
152ca26842eSHajimu UMEMOTO 			log(LOG_DEBUG,
1533c616032SGleb Smirnoff 			    "obsolete pre-RFC2553 sockaddr_in6 rejected\n");
154ca26842eSHajimu UMEMOTO 			error = EINVAL;
155ca26842eSHajimu UMEMOTO 			goto out;
156ca26842eSHajimu UMEMOTO 		}
157ca26842eSHajimu UMEMOTO 	} else
158ca26842eSHajimu UMEMOTO #endif
159ca26842eSHajimu UMEMOTO 	if (bdom == AF_INET)
160ca26842eSHajimu UMEMOTO 		alloclen = sizeof(struct sockaddr_in);
161ca26842eSHajimu UMEMOTO 
162ca26842eSHajimu UMEMOTO 	sa = (struct sockaddr *) kosa;
163ca26842eSHajimu UMEMOTO 	sa->sa_family = bdom;
164ca26842eSHajimu UMEMOTO 	sa->sa_len = alloclen;
165ca26842eSHajimu UMEMOTO 
166ca26842eSHajimu UMEMOTO 	*sap = sa;
167ca26842eSHajimu UMEMOTO 	*osalen = alloclen;
168ca26842eSHajimu UMEMOTO 	return (0);
169ca26842eSHajimu UMEMOTO 
170ca26842eSHajimu UMEMOTO out:
1711ede983cSDag-Erling Smørgrav 	free(kosa, mtype);
172ca26842eSHajimu UMEMOTO 	return (error);
173ca26842eSHajimu UMEMOTO }
174ca26842eSHajimu UMEMOTO 
175c21dee17SSøren Schmidt static int
176c21dee17SSøren Schmidt linux_to_bsd_domain(int domain)
177c21dee17SSøren Schmidt {
1783f3a4815SMarcel Moolenaar 
179c21dee17SSøren Schmidt 	switch (domain) {
180c21dee17SSøren Schmidt 	case LINUX_AF_UNSPEC:
1813f3a4815SMarcel Moolenaar 		return (AF_UNSPEC);
182c21dee17SSøren Schmidt 	case LINUX_AF_UNIX:
1833f3a4815SMarcel Moolenaar 		return (AF_LOCAL);
184c21dee17SSøren Schmidt 	case LINUX_AF_INET:
1853f3a4815SMarcel Moolenaar 		return (AF_INET);
186ca26842eSHajimu UMEMOTO 	case LINUX_AF_INET6:
187ca26842eSHajimu UMEMOTO 		return (AF_INET6);
188c21dee17SSøren Schmidt 	case LINUX_AF_AX25:
1893f3a4815SMarcel Moolenaar 		return (AF_CCITT);
190c21dee17SSøren Schmidt 	case LINUX_AF_IPX:
1913f3a4815SMarcel Moolenaar 		return (AF_IPX);
192c21dee17SSøren Schmidt 	case LINUX_AF_APPLETALK:
1933f3a4815SMarcel Moolenaar 		return (AF_APPLETALK);
194c21dee17SSøren Schmidt 	}
1953f3a4815SMarcel Moolenaar 	return (-1);
196c21dee17SSøren Schmidt }
197c21dee17SSøren Schmidt 
198ca26842eSHajimu UMEMOTO static int
199ca26842eSHajimu UMEMOTO bsd_to_linux_domain(int domain)
200ca26842eSHajimu UMEMOTO {
201ca26842eSHajimu UMEMOTO 
202ca26842eSHajimu UMEMOTO 	switch (domain) {
203ca26842eSHajimu UMEMOTO 	case AF_UNSPEC:
204ca26842eSHajimu UMEMOTO 		return (LINUX_AF_UNSPEC);
205ca26842eSHajimu UMEMOTO 	case AF_LOCAL:
206ca26842eSHajimu UMEMOTO 		return (LINUX_AF_UNIX);
207ca26842eSHajimu UMEMOTO 	case AF_INET:
208ca26842eSHajimu UMEMOTO 		return (LINUX_AF_INET);
209ca26842eSHajimu UMEMOTO 	case AF_INET6:
210ca26842eSHajimu UMEMOTO 		return (LINUX_AF_INET6);
211ca26842eSHajimu UMEMOTO 	case AF_CCITT:
212ca26842eSHajimu UMEMOTO 		return (LINUX_AF_AX25);
213ca26842eSHajimu UMEMOTO 	case AF_IPX:
214ca26842eSHajimu UMEMOTO 		return (LINUX_AF_IPX);
215ca26842eSHajimu UMEMOTO 	case AF_APPLETALK:
216ca26842eSHajimu UMEMOTO 		return (LINUX_AF_APPLETALK);
217ca26842eSHajimu UMEMOTO 	}
218ca26842eSHajimu UMEMOTO 	return (-1);
219ca26842eSHajimu UMEMOTO }
220ca26842eSHajimu UMEMOTO 
221c21dee17SSøren Schmidt static int
222c21dee17SSøren Schmidt linux_to_bsd_sockopt_level(int level)
223c21dee17SSøren Schmidt {
2243f3a4815SMarcel Moolenaar 
225c21dee17SSøren Schmidt 	switch (level) {
226c21dee17SSøren Schmidt 	case LINUX_SOL_SOCKET:
2273f3a4815SMarcel Moolenaar 		return (SOL_SOCKET);
228c21dee17SSøren Schmidt 	}
2293f3a4815SMarcel Moolenaar 	return (level);
230c21dee17SSøren Schmidt }
231c21dee17SSøren Schmidt 
2323f3a4815SMarcel Moolenaar static int
23384b11cd8SMitsuru IWASAKI bsd_to_linux_sockopt_level(int level)
23484b11cd8SMitsuru IWASAKI {
23584b11cd8SMitsuru IWASAKI 
23684b11cd8SMitsuru IWASAKI 	switch (level) {
23784b11cd8SMitsuru IWASAKI 	case SOL_SOCKET:
23884b11cd8SMitsuru IWASAKI 		return (LINUX_SOL_SOCKET);
23984b11cd8SMitsuru IWASAKI 	}
24084b11cd8SMitsuru IWASAKI 	return (level);
24184b11cd8SMitsuru IWASAKI }
24284b11cd8SMitsuru IWASAKI 
24384b11cd8SMitsuru IWASAKI static int
2443f3a4815SMarcel Moolenaar linux_to_bsd_ip_sockopt(int opt)
245c21dee17SSøren Schmidt {
2463f3a4815SMarcel Moolenaar 
247c21dee17SSøren Schmidt 	switch (opt) {
248c21dee17SSøren Schmidt 	case LINUX_IP_TOS:
2493f3a4815SMarcel Moolenaar 		return (IP_TOS);
250c21dee17SSøren Schmidt 	case LINUX_IP_TTL:
2513f3a4815SMarcel Moolenaar 		return (IP_TTL);
25266ff6a3cSBill Fenner 	case LINUX_IP_OPTIONS:
2533f3a4815SMarcel Moolenaar 		return (IP_OPTIONS);
25466ff6a3cSBill Fenner 	case LINUX_IP_MULTICAST_IF:
2553f3a4815SMarcel Moolenaar 		return (IP_MULTICAST_IF);
25666ff6a3cSBill Fenner 	case LINUX_IP_MULTICAST_TTL:
2573f3a4815SMarcel Moolenaar 		return (IP_MULTICAST_TTL);
25866ff6a3cSBill Fenner 	case LINUX_IP_MULTICAST_LOOP:
2593f3a4815SMarcel Moolenaar 		return (IP_MULTICAST_LOOP);
26066ff6a3cSBill Fenner 	case LINUX_IP_ADD_MEMBERSHIP:
2613f3a4815SMarcel Moolenaar 		return (IP_ADD_MEMBERSHIP);
26266ff6a3cSBill Fenner 	case LINUX_IP_DROP_MEMBERSHIP:
2633f3a4815SMarcel Moolenaar 		return (IP_DROP_MEMBERSHIP);
26466ff6a3cSBill Fenner 	case LINUX_IP_HDRINCL:
2653f3a4815SMarcel Moolenaar 		return (IP_HDRINCL);
266c21dee17SSøren Schmidt 	}
2673f3a4815SMarcel Moolenaar 	return (-1);
268c21dee17SSøren Schmidt }
269c21dee17SSøren Schmidt 
270c21dee17SSøren Schmidt static int
271c21dee17SSøren Schmidt linux_to_bsd_so_sockopt(int opt)
272c21dee17SSøren Schmidt {
2733f3a4815SMarcel Moolenaar 
274c21dee17SSøren Schmidt 	switch (opt) {
275c21dee17SSøren Schmidt 	case LINUX_SO_DEBUG:
2763f3a4815SMarcel Moolenaar 		return (SO_DEBUG);
277c21dee17SSøren Schmidt 	case LINUX_SO_REUSEADDR:
2783f3a4815SMarcel Moolenaar 		return (SO_REUSEADDR);
279c21dee17SSøren Schmidt 	case LINUX_SO_TYPE:
2803f3a4815SMarcel Moolenaar 		return (SO_TYPE);
281c21dee17SSøren Schmidt 	case LINUX_SO_ERROR:
2823f3a4815SMarcel Moolenaar 		return (SO_ERROR);
283c21dee17SSøren Schmidt 	case LINUX_SO_DONTROUTE:
2843f3a4815SMarcel Moolenaar 		return (SO_DONTROUTE);
285c21dee17SSøren Schmidt 	case LINUX_SO_BROADCAST:
2863f3a4815SMarcel Moolenaar 		return (SO_BROADCAST);
287c21dee17SSøren Schmidt 	case LINUX_SO_SNDBUF:
2883f3a4815SMarcel Moolenaar 		return (SO_SNDBUF);
289c21dee17SSøren Schmidt 	case LINUX_SO_RCVBUF:
2903f3a4815SMarcel Moolenaar 		return (SO_RCVBUF);
291c21dee17SSøren Schmidt 	case LINUX_SO_KEEPALIVE:
2923f3a4815SMarcel Moolenaar 		return (SO_KEEPALIVE);
293c21dee17SSøren Schmidt 	case LINUX_SO_OOBINLINE:
2943f3a4815SMarcel Moolenaar 		return (SO_OOBINLINE);
295c21dee17SSøren Schmidt 	case LINUX_SO_LINGER:
2963f3a4815SMarcel Moolenaar 		return (SO_LINGER);
297d0b2365eSKonstantin Belousov 	case LINUX_SO_PEERCRED:
298d0b2365eSKonstantin Belousov 		return (LOCAL_PEERCRED);
299d0b2365eSKonstantin Belousov 	case LINUX_SO_RCVLOWAT:
300d0b2365eSKonstantin Belousov 		return (SO_RCVLOWAT);
301d0b2365eSKonstantin Belousov 	case LINUX_SO_SNDLOWAT:
302d0b2365eSKonstantin Belousov 		return (SO_SNDLOWAT);
303d0b2365eSKonstantin Belousov 	case LINUX_SO_RCVTIMEO:
304d0b2365eSKonstantin Belousov 		return (SO_RCVTIMEO);
305d0b2365eSKonstantin Belousov 	case LINUX_SO_SNDTIMEO:
306d0b2365eSKonstantin Belousov 		return (SO_SNDTIMEO);
307d0b2365eSKonstantin Belousov 	case LINUX_SO_TIMESTAMP:
308d0b2365eSKonstantin Belousov 		return (SO_TIMESTAMP);
309d0b2365eSKonstantin Belousov 	case LINUX_SO_ACCEPTCONN:
310d0b2365eSKonstantin Belousov 		return (SO_ACCEPTCONN);
311c21dee17SSøren Schmidt 	}
3123f3a4815SMarcel Moolenaar 	return (-1);
313c21dee17SSøren Schmidt }
314c21dee17SSøren Schmidt 
31540dbba57SAssar Westerlund static int
31640dbba57SAssar Westerlund linux_to_bsd_msg_flags(int flags)
31740dbba57SAssar Westerlund {
31840dbba57SAssar Westerlund 	int ret_flags = 0;
31940dbba57SAssar Westerlund 
32040dbba57SAssar Westerlund 	if (flags & LINUX_MSG_OOB)
32140dbba57SAssar Westerlund 		ret_flags |= MSG_OOB;
32240dbba57SAssar Westerlund 	if (flags & LINUX_MSG_PEEK)
32340dbba57SAssar Westerlund 		ret_flags |= MSG_PEEK;
32440dbba57SAssar Westerlund 	if (flags & LINUX_MSG_DONTROUTE)
32540dbba57SAssar Westerlund 		ret_flags |= MSG_DONTROUTE;
32640dbba57SAssar Westerlund 	if (flags & LINUX_MSG_CTRUNC)
32740dbba57SAssar Westerlund 		ret_flags |= MSG_CTRUNC;
32840dbba57SAssar Westerlund 	if (flags & LINUX_MSG_TRUNC)
32940dbba57SAssar Westerlund 		ret_flags |= MSG_TRUNC;
33040dbba57SAssar Westerlund 	if (flags & LINUX_MSG_DONTWAIT)
33140dbba57SAssar Westerlund 		ret_flags |= MSG_DONTWAIT;
33240dbba57SAssar Westerlund 	if (flags & LINUX_MSG_EOR)
33340dbba57SAssar Westerlund 		ret_flags |= MSG_EOR;
33440dbba57SAssar Westerlund 	if (flags & LINUX_MSG_WAITALL)
33540dbba57SAssar Westerlund 		ret_flags |= MSG_WAITALL;
3368d6e40c3SMaxim Sobolev 	if (flags & LINUX_MSG_NOSIGNAL)
3378d6e40c3SMaxim Sobolev 		ret_flags |= MSG_NOSIGNAL;
33840dbba57SAssar Westerlund #if 0 /* not handled */
33940dbba57SAssar Westerlund 	if (flags & LINUX_MSG_PROXY)
34040dbba57SAssar Westerlund 		;
34140dbba57SAssar Westerlund 	if (flags & LINUX_MSG_FIN)
34240dbba57SAssar Westerlund 		;
34340dbba57SAssar Westerlund 	if (flags & LINUX_MSG_SYN)
34440dbba57SAssar Westerlund 		;
34540dbba57SAssar Westerlund 	if (flags & LINUX_MSG_CONFIRM)
34640dbba57SAssar Westerlund 		;
34740dbba57SAssar Westerlund 	if (flags & LINUX_MSG_RST)
34840dbba57SAssar Westerlund 		;
34940dbba57SAssar Westerlund 	if (flags & LINUX_MSG_ERRQUEUE)
35040dbba57SAssar Westerlund 		;
35140dbba57SAssar Westerlund #endif
35240dbba57SAssar Westerlund 	return ret_flags;
35340dbba57SAssar Westerlund }
35440dbba57SAssar Westerlund 
3555c8919adSAlexander Leidinger /*
3565c8919adSAlexander Leidinger * If bsd_to_linux_sockaddr() or linux_to_bsd_sockaddr() faults, then the
3575c8919adSAlexander Leidinger * native syscall will fault.  Thus, we don't really need to check the
3585c8919adSAlexander Leidinger * return values for these functions.
3595c8919adSAlexander Leidinger */
3605c8919adSAlexander Leidinger 
3615c8919adSAlexander Leidinger static int
3625c8919adSAlexander Leidinger bsd_to_linux_sockaddr(struct sockaddr *arg)
3635c8919adSAlexander Leidinger {
3645c8919adSAlexander Leidinger 	struct sockaddr sa;
3655c8919adSAlexander Leidinger 	size_t sa_len = sizeof(struct sockaddr);
3665c8919adSAlexander Leidinger 	int error;
3675c8919adSAlexander Leidinger 
3685c8919adSAlexander Leidinger 	if ((error = copyin(arg, &sa, sa_len)))
3695c8919adSAlexander Leidinger 		return (error);
3705c8919adSAlexander Leidinger 
3715c8919adSAlexander Leidinger 	*(u_short *)&sa = sa.sa_family;
3725c8919adSAlexander Leidinger 
3735c8919adSAlexander Leidinger 	error = copyout(&sa, arg, sa_len);
3745c8919adSAlexander Leidinger 
3755c8919adSAlexander Leidinger 	return (error);
3765c8919adSAlexander Leidinger }
3775c8919adSAlexander Leidinger 
3785c8919adSAlexander Leidinger static int
3795c8919adSAlexander Leidinger linux_to_bsd_sockaddr(struct sockaddr *arg, int len)
3805c8919adSAlexander Leidinger {
3815c8919adSAlexander Leidinger 	struct sockaddr sa;
3825c8919adSAlexander Leidinger 	size_t sa_len = sizeof(struct sockaddr);
3835c8919adSAlexander Leidinger 	int error;
3845c8919adSAlexander Leidinger 
3855c8919adSAlexander Leidinger 	if ((error = copyin(arg, &sa, sa_len)))
3865c8919adSAlexander Leidinger 		return (error);
3875c8919adSAlexander Leidinger 
3885c8919adSAlexander Leidinger 	sa.sa_family = *(sa_family_t *)&sa;
3895c8919adSAlexander Leidinger 	sa.sa_len = len;
3905c8919adSAlexander Leidinger 
3915c8919adSAlexander Leidinger 	error = copyout(&sa, arg, sa_len);
3925c8919adSAlexander Leidinger 
3935c8919adSAlexander Leidinger 	return (error);
3945c8919adSAlexander Leidinger }
3955c8919adSAlexander Leidinger 
3965c8919adSAlexander Leidinger 
397ca26842eSHajimu UMEMOTO static int
398ca26842eSHajimu UMEMOTO linux_sa_put(struct osockaddr *osa)
399ca26842eSHajimu UMEMOTO {
400ca26842eSHajimu UMEMOTO 	struct osockaddr sa;
401ca26842eSHajimu UMEMOTO 	int error, bdom;
402ca26842eSHajimu UMEMOTO 
403ca26842eSHajimu UMEMOTO 	/*
404ca26842eSHajimu UMEMOTO 	 * Only read/write the osockaddr family part, the rest is
405ca26842eSHajimu UMEMOTO 	 * not changed.
406ca26842eSHajimu UMEMOTO 	 */
4074b7ef73dSDag-Erling Smørgrav 	error = copyin(osa, &sa, sizeof(sa.sa_family));
408ca26842eSHajimu UMEMOTO 	if (error)
409ca26842eSHajimu UMEMOTO 		return (error);
410ca26842eSHajimu UMEMOTO 
411ca26842eSHajimu UMEMOTO 	bdom = bsd_to_linux_domain(sa.sa_family);
412ca26842eSHajimu UMEMOTO 	if (bdom == -1)
413ca26842eSHajimu UMEMOTO 		return (EINVAL);
414ca26842eSHajimu UMEMOTO 
415ca26842eSHajimu UMEMOTO 	sa.sa_family = bdom;
416ca26842eSHajimu UMEMOTO 	error = copyout(&sa, osa, sizeof(sa.sa_family));
417ca26842eSHajimu UMEMOTO 	if (error)
418ca26842eSHajimu UMEMOTO 		return (error);
419ca26842eSHajimu UMEMOTO 
420ca26842eSHajimu UMEMOTO 	return (0);
421ca26842eSHajimu UMEMOTO }
422ca26842eSHajimu UMEMOTO 
4235a8a13e0SDavid Malone static int
424a6886ef1SMaxim Sobolev linux_sendit(struct thread *td, int s, struct msghdr *mp, int flags,
425a6886ef1SMaxim Sobolev     enum uio_seg segflg)
4265a8a13e0SDavid Malone {
4275a8a13e0SDavid Malone 	struct mbuf *control;
4285a8a13e0SDavid Malone 	struct sockaddr *to;
4295a8a13e0SDavid Malone 	int error;
4305a8a13e0SDavid Malone 
4315a8a13e0SDavid Malone 	if (mp->msg_name != NULL) {
4325a8a13e0SDavid Malone 		error = linux_getsockaddr(&to, mp->msg_name, mp->msg_namelen);
4335a8a13e0SDavid Malone 		if (error)
4345a8a13e0SDavid Malone 			return (error);
4355a8a13e0SDavid Malone 		mp->msg_name = to;
4365a8a13e0SDavid Malone 	} else
4375a8a13e0SDavid Malone 		to = NULL;
4385a8a13e0SDavid Malone 
4395a8a13e0SDavid Malone 	if (mp->msg_control != NULL) {
4405a8a13e0SDavid Malone 		struct cmsghdr *cmsg;
4415a8a13e0SDavid Malone 
4425a8a13e0SDavid Malone 		if (mp->msg_controllen < sizeof(struct cmsghdr)) {
4435a8a13e0SDavid Malone 			error = EINVAL;
4445a8a13e0SDavid Malone 			goto bad;
4455a8a13e0SDavid Malone 		}
4465a8a13e0SDavid Malone 		error = sockargs(&control, mp->msg_control,
4475a8a13e0SDavid Malone 		    mp->msg_controllen, MT_CONTROL);
4485a8a13e0SDavid Malone 		if (error)
4495a8a13e0SDavid Malone 			goto bad;
4505a8a13e0SDavid Malone 
4515a8a13e0SDavid Malone 		cmsg = mtod(control, struct cmsghdr *);
4525a8a13e0SDavid Malone 		cmsg->cmsg_level = linux_to_bsd_sockopt_level(cmsg->cmsg_level);
4535a8a13e0SDavid Malone 	} else
4545a8a13e0SDavid Malone 		control = NULL;
4555a8a13e0SDavid Malone 
456a6886ef1SMaxim Sobolev 	error = kern_sendit(td, s, mp, linux_to_bsd_msg_flags(flags), control,
457a6886ef1SMaxim Sobolev 	    segflg);
4585a8a13e0SDavid Malone 
4595a8a13e0SDavid Malone bad:
4605a8a13e0SDavid Malone 	if (to)
4611ede983cSDag-Erling Smørgrav 		free(to, M_SONAME);
4625a8a13e0SDavid Malone 	return (error);
4635a8a13e0SDavid Malone }
4645a8a13e0SDavid Malone 
4653f3a4815SMarcel Moolenaar /* Return 0 if IP_HDRINCL is set for the given socket. */
466f2477ae1SMike Smith static int
467e140eb43SDavid Malone linux_check_hdrincl(struct thread *td, int s)
468f2477ae1SMike Smith {
4693db2a843SBruce Evans 	int error, optval, size_val;
470f2477ae1SMike Smith 
471e140eb43SDavid Malone 	size_val = sizeof(optval);
472e140eb43SDavid Malone 	error = kern_getsockopt(td, s, IPPROTO_IP, IP_HDRINCL,
473e140eb43SDavid Malone 	    &optval, UIO_SYSSPACE, &size_val);
474e140eb43SDavid Malone 	if (error)
4753f3a4815SMarcel Moolenaar 		return (error);
4763f3a4815SMarcel Moolenaar 
4773f3a4815SMarcel Moolenaar 	return (optval == 0);
478f2477ae1SMike Smith }
479f2477ae1SMike Smith 
4805a8a13e0SDavid Malone struct linux_sendto_args {
4815a8a13e0SDavid Malone 	int s;
4824af27623STim J. Robbins 	l_uintptr_t msg;
4835a8a13e0SDavid Malone 	int len;
4845a8a13e0SDavid Malone 	int flags;
4854af27623STim J. Robbins 	l_uintptr_t to;
4865a8a13e0SDavid Malone 	int tolen;
4875a8a13e0SDavid Malone };
4885a8a13e0SDavid Malone 
489f2477ae1SMike Smith /*
490f2477ae1SMike Smith  * Updated sendto() when IP_HDRINCL is set:
491f2477ae1SMike Smith  * tweak endian-dependent fields in the IP packet.
492f2477ae1SMike Smith  */
493f2477ae1SMike Smith static int
49438da2381SRobert Watson linux_sendto_hdrincl(struct thread *td, struct linux_sendto_args *linux_args)
495f2477ae1SMike Smith {
496f2477ae1SMike Smith /*
497f2477ae1SMike Smith  * linux_ip_copysize defines how many bytes we should copy
498f2477ae1SMike Smith  * from the beginning of the IP packet before we customize it for BSD.
499a6886ef1SMaxim Sobolev  * It should include all the fields we modify (ip_len and ip_off).
500f2477ae1SMike Smith  */
501f2477ae1SMike Smith #define linux_ip_copysize	8
502f2477ae1SMike Smith 
503f2477ae1SMike Smith 	struct ip *packet;
5045a8a13e0SDavid Malone 	struct msghdr msg;
505a6886ef1SMaxim Sobolev 	struct iovec aiov[1];
506f2477ae1SMike Smith 	int error;
507f2477ae1SMike Smith 
508aa675b57SDavid Schultz 	/* Check that the packet isn't too big or too small. */
509aa675b57SDavid Schultz 	if (linux_args->len < linux_ip_copysize ||
510aa675b57SDavid Schultz 	    linux_args->len > IP_MAXPACKET)
5113f3a4815SMarcel Moolenaar 		return (EINVAL);
512f2477ae1SMike Smith 
513a6886ef1SMaxim Sobolev 	packet = (struct ip *)malloc(linux_args->len, M_TEMP, M_WAITOK);
514f2477ae1SMike Smith 
515a6886ef1SMaxim Sobolev 	/* Make kernel copy of the packet to be sent */
5164af27623STim J. Robbins 	if ((error = copyin(PTRIN(linux_args->msg), packet,
517a6886ef1SMaxim Sobolev 	    linux_args->len)))
518a6886ef1SMaxim Sobolev 		goto goout;
519f2477ae1SMike Smith 
520f2477ae1SMike Smith 	/* Convert fields from Linux to BSD raw IP socket format */
5215a8a13e0SDavid Malone 	packet->ip_len = linux_args->len;
522f2477ae1SMike Smith 	packet->ip_off = ntohs(packet->ip_off);
523f2477ae1SMike Smith 
524f2477ae1SMike Smith 	/* Prepare the msghdr and iovec structures describing the new packet */
5254af27623STim J. Robbins 	msg.msg_name = PTRIN(linux_args->to);
5265a8a13e0SDavid Malone 	msg.msg_namelen = linux_args->tolen;
5275a8a13e0SDavid Malone 	msg.msg_iov = aiov;
528a6886ef1SMaxim Sobolev 	msg.msg_iovlen = 1;
5295a8a13e0SDavid Malone 	msg.msg_control = NULL;
5305a8a13e0SDavid Malone 	msg.msg_flags = 0;
5315a8a13e0SDavid Malone 	aiov[0].iov_base = (char *)packet;
532a6886ef1SMaxim Sobolev 	aiov[0].iov_len = linux_args->len;
533a6886ef1SMaxim Sobolev 	error = linux_sendit(td, linux_args->s, &msg, linux_args->flags,
534a6886ef1SMaxim Sobolev 	    UIO_SYSSPACE);
535a6886ef1SMaxim Sobolev goout:
536a6886ef1SMaxim Sobolev 	free(packet, M_TEMP);
5375a8a13e0SDavid Malone 	return (error);
538f2477ae1SMike Smith }
539f2477ae1SMike Smith 
540c21dee17SSøren Schmidt struct linux_socket_args {
541c21dee17SSøren Schmidt 	int domain;
542c21dee17SSøren Schmidt 	int type;
543c21dee17SSøren Schmidt 	int protocol;
544c21dee17SSøren Schmidt };
545c21dee17SSøren Schmidt 
546c21dee17SSøren Schmidt static int
547b40ce416SJulian Elischer linux_socket(struct thread *td, struct linux_socket_args *args)
548c21dee17SSøren Schmidt {
5498b615593SMarko Zec #ifdef INET6
5508b615593SMarko Zec 	INIT_VNET_INET6(curvnet);
5518b615593SMarko Zec #endif
552ef04503dSPeter Wemm 	struct socket_args /* {
553c21dee17SSøren Schmidt 		int domain;
554c21dee17SSøren Schmidt 		int type;
555c21dee17SSøren Schmidt 		int protocol;
556ef04503dSPeter Wemm 	} */ bsd_args;
557f2477ae1SMike Smith 	int retval_socket;
558c21dee17SSøren Schmidt 
559745aaef5SKonstantin Belousov 	bsd_args.protocol = args->protocol;
560745aaef5SKonstantin Belousov 	bsd_args.type = args->type;
561745aaef5SKonstantin Belousov 	bsd_args.domain = linux_to_bsd_domain(args->domain);
562c21dee17SSøren Schmidt 	if (bsd_args.domain == -1)
5633f3a4815SMarcel Moolenaar 		return (EINVAL);
564f2477ae1SMike Smith 
565b40ce416SJulian Elischer 	retval_socket = socket(td, &bsd_args);
566f2477ae1SMike Smith 	if (bsd_args.type == SOCK_RAW
567f2477ae1SMike Smith 	    && (bsd_args.protocol == IPPROTO_RAW || bsd_args.protocol == 0)
568f2477ae1SMike Smith 	    && bsd_args.domain == AF_INET
569f2477ae1SMike Smith 	    && retval_socket >= 0) {
570f2477ae1SMike Smith 		/* It's a raw IP socket: set the IP_HDRINCL option. */
571e140eb43SDavid Malone 		int hdrincl;
572f2477ae1SMike Smith 
573e140eb43SDavid Malone 		hdrincl = 1;
574e140eb43SDavid Malone 		/* We ignore any error returned by kern_setsockopt() */
575e140eb43SDavid Malone 		kern_setsockopt(td, td->td_retval[0], IPPROTO_IP, IP_HDRINCL,
576e140eb43SDavid Malone 		    &hdrincl, UIO_SYSSPACE, sizeof(hdrincl));
577f2477ae1SMike Smith 	}
578ca26842eSHajimu UMEMOTO #ifdef INET6
579ca26842eSHajimu UMEMOTO 	/*
580ca26842eSHajimu UMEMOTO 	 * Linux AF_INET6 socket has IPV6_V6ONLY setsockopt set to 0 by
581ca26842eSHajimu UMEMOTO 	 * default and some apps depend on this. So, set V6ONLY to 0
582ca26842eSHajimu UMEMOTO 	 * for Linux apps if the sysctl value is set to 1.
583ca26842eSHajimu UMEMOTO 	 */
58437e7a5a2SHajimu UMEMOTO 	if (bsd_args.domain == PF_INET6 && retval_socket >= 0
58537e7a5a2SHajimu UMEMOTO #ifndef KLD_MODULE
58637e7a5a2SHajimu UMEMOTO 	    /*
58737e7a5a2SHajimu UMEMOTO 	     * XXX: Avoid undefined symbol error with an IPv4 only
58837e7a5a2SHajimu UMEMOTO 	     * kernel.
58937e7a5a2SHajimu UMEMOTO 	     */
590603724d3SBjoern A. Zeeb 	    && V_ip6_v6only
59137e7a5a2SHajimu UMEMOTO #endif
59237e7a5a2SHajimu UMEMOTO 	    ) {
593e140eb43SDavid Malone 		int v6only;
594ca26842eSHajimu UMEMOTO 
595e140eb43SDavid Malone 		v6only = 0;
596ca26842eSHajimu UMEMOTO 		/* We ignore any error returned by setsockopt() */
597e140eb43SDavid Malone 		kern_setsockopt(td, td->td_retval[0], IPPROTO_IPV6, IPV6_V6ONLY,
598e140eb43SDavid Malone 		    &v6only, UIO_SYSSPACE, sizeof(v6only));
599ca26842eSHajimu UMEMOTO 	}
600ca26842eSHajimu UMEMOTO #endif
6013f3a4815SMarcel Moolenaar 
6023f3a4815SMarcel Moolenaar 	return (retval_socket);
603c21dee17SSøren Schmidt }
604c21dee17SSøren Schmidt 
605c21dee17SSøren Schmidt struct linux_bind_args {
606c21dee17SSøren Schmidt 	int s;
6074af27623STim J. Robbins 	l_uintptr_t name;
608c21dee17SSøren Schmidt 	int namelen;
609c21dee17SSøren Schmidt };
610c21dee17SSøren Schmidt 
611c21dee17SSøren Schmidt static int
612b40ce416SJulian Elischer linux_bind(struct thread *td, struct linux_bind_args *args)
613c21dee17SSøren Schmidt {
614ca26842eSHajimu UMEMOTO 	struct sockaddr *sa;
615c21dee17SSøren Schmidt 	int error;
616c21dee17SSøren Schmidt 
617745aaef5SKonstantin Belousov 	error = linux_getsockaddr(&sa, PTRIN(args->name),
618745aaef5SKonstantin Belousov 	    args->namelen);
619ca26842eSHajimu UMEMOTO 	if (error)
620ca26842eSHajimu UMEMOTO 		return (error);
621ca26842eSHajimu UMEMOTO 
622745aaef5SKonstantin Belousov 	error = kern_bind(td, args->s, sa);
623b33887eaSJohn Baldwin 	free(sa, M_SONAME);
624745aaef5SKonstantin Belousov 	if (error == EADDRNOTAVAIL && args->namelen != sizeof(struct sockaddr_in))
625d4b7423fSAlexander Leidinger 	   	return (EINVAL);
626b33887eaSJohn Baldwin 	return (error);
627c21dee17SSøren Schmidt }
628c21dee17SSøren Schmidt 
62901e0ffbaSAlexander Leidinger struct linux_connect_args {
630c21dee17SSøren Schmidt 	int s;
6314af27623STim J. Robbins 	l_uintptr_t name;
632c21dee17SSøren Schmidt 	int namelen;
633c21dee17SSøren Schmidt };
634b40ce416SJulian Elischer int linux_connect(struct thread *, struct linux_connect_args *);
635c21dee17SSøren Schmidt 
636930a65feSAndrew Gallatin int
637b40ce416SJulian Elischer linux_connect(struct thread *td, struct linux_connect_args *args)
638c21dee17SSøren Schmidt {
6390bf301c0SJonathan Lemon 	struct socket *so;
640ca26842eSHajimu UMEMOTO 	struct sockaddr *sa;
64139c95b83SMatthew Dillon 	u_int fflag;
642c21dee17SSøren Schmidt 	int error;
643c21dee17SSøren Schmidt 
644745aaef5SKonstantin Belousov 	error = linux_getsockaddr(&sa, (struct osockaddr *)PTRIN(args->name),
645745aaef5SKonstantin Belousov 	    args->namelen);
646ca26842eSHajimu UMEMOTO 	if (error)
647ca26842eSHajimu UMEMOTO 		return (error);
648ca26842eSHajimu UMEMOTO 
649745aaef5SKonstantin Belousov 	error = kern_connect(td, args->s, sa);
650b33887eaSJohn Baldwin 	free(sa, M_SONAME);
6510bf301c0SJonathan Lemon 	if (error != EISCONN)
6520bf301c0SJonathan Lemon 		return (error);
6530bf301c0SJonathan Lemon 
654dad3b88aSMike Smith 	/*
655dad3b88aSMike Smith 	 * Linux doesn't return EISCONN the first time it occurs,
656dad3b88aSMike Smith 	 * when on a non-blocking socket. Instead it returns the
657dad3b88aSMike Smith 	 * error getsockopt(SOL_SOCKET, SO_ERROR) would return on BSD.
658f7f45ac8SRobert Watson 	 *
659f7f45ac8SRobert Watson 	 * XXXRW: Instead of using fgetsock(), check that it is a
660f7f45ac8SRobert Watson 	 * socket and use the file descriptor reference instead of
661f7f45ac8SRobert Watson 	 * creating a new one.
662dad3b88aSMike Smith 	 */
663745aaef5SKonstantin Belousov 	error = fgetsock(td, args->s, &so, &fflag);
6644641373fSJohn Baldwin 	if (error == 0) {
6650bf301c0SJonathan Lemon 		error = EISCONN;
66639c95b83SMatthew Dillon 		if (fflag & FNONBLOCK) {
6674641373fSJohn Baldwin 			SOCK_LOCK(so);
6685002a60fSMarcel Moolenaar 			if (so->so_emuldata == 0)
6690bf301c0SJonathan Lemon 				error = so->so_error;
6700bf301c0SJonathan Lemon 			so->so_emuldata = (void *)1;
6714641373fSJohn Baldwin 			SOCK_UNLOCK(so);
672dad3b88aSMike Smith 		}
67339c95b83SMatthew Dillon 		fputsock(so);
6744641373fSJohn Baldwin 	}
6753f3a4815SMarcel Moolenaar 	return (error);
676c21dee17SSøren Schmidt }
677c21dee17SSøren Schmidt 
678c21dee17SSøren Schmidt struct linux_listen_args {
679c21dee17SSøren Schmidt 	int s;
680c21dee17SSøren Schmidt 	int backlog;
681c21dee17SSøren Schmidt };
682c21dee17SSøren Schmidt 
683c21dee17SSøren Schmidt static int
684b40ce416SJulian Elischer linux_listen(struct thread *td, struct linux_listen_args *args)
685c21dee17SSøren Schmidt {
686ef04503dSPeter Wemm 	struct listen_args /* {
687c21dee17SSøren Schmidt 		int s;
688c21dee17SSøren Schmidt 		int backlog;
689ef04503dSPeter Wemm 	} */ bsd_args;
690c21dee17SSøren Schmidt 
691745aaef5SKonstantin Belousov 	bsd_args.s = args->s;
692745aaef5SKonstantin Belousov 	bsd_args.backlog = args->backlog;
693b40ce416SJulian Elischer 	return (listen(td, &bsd_args));
694c21dee17SSøren Schmidt }
695c21dee17SSøren Schmidt 
69601e0ffbaSAlexander Leidinger struct linux_accept_args {
697c21dee17SSøren Schmidt 	int s;
6984af27623STim J. Robbins 	l_uintptr_t addr;
6994af27623STim J. Robbins 	l_uintptr_t namelen;
700c21dee17SSøren Schmidt };
701c21dee17SSøren Schmidt 
70201e0ffbaSAlexander Leidinger static int
703b40ce416SJulian Elischer linux_accept(struct thread *td, struct linux_accept_args *args)
704c21dee17SSøren Schmidt {
705ef04503dSPeter Wemm 	struct accept_args /* {
706c21dee17SSøren Schmidt 		int	s;
7073db2a843SBruce Evans 		struct sockaddr * __restrict name;
7083db2a843SBruce Evans 		socklen_t * __restrict anamelen;
709ef04503dSPeter Wemm 	} */ bsd_args;
7102ca25ab5SJohn Baldwin 	int error, fd;
711c21dee17SSøren Schmidt 
712745aaef5SKonstantin Belousov 	bsd_args.s = args->s;
7133db2a843SBruce Evans 	/* XXX: */
714745aaef5SKonstantin Belousov 	bsd_args.name = (struct sockaddr * __restrict)PTRIN(args->addr);
715745aaef5SKonstantin Belousov 	bsd_args.anamelen = PTRIN(args->namelen);/* XXX */
7165c8919adSAlexander Leidinger 	error = accept(td, &bsd_args);
7175c8919adSAlexander Leidinger 	bsd_to_linux_sockaddr((struct sockaddr *)bsd_args.name);
718d4b7423fSAlexander Leidinger 	if (error) {
719745aaef5SKonstantin Belousov 		if (error == EFAULT && args->namelen != sizeof(struct sockaddr_in))
720d4b7423fSAlexander Leidinger 			return (EINVAL);
721dba5ab66SMarcel Moolenaar 		return (error);
722d4b7423fSAlexander Leidinger 	}
723745aaef5SKonstantin Belousov 	if (args->addr) {
724745aaef5SKonstantin Belousov 		error = linux_sa_put(PTRIN(args->addr));
725ca26842eSHajimu UMEMOTO 		if (error) {
726c1cccebeSJohn Baldwin 			(void)kern_close(td, td->td_retval[0]);
727ca26842eSHajimu UMEMOTO 			return (error);
728ca26842eSHajimu UMEMOTO 		}
729ca26842eSHajimu UMEMOTO 	}
730dba5ab66SMarcel Moolenaar 
731dba5ab66SMarcel Moolenaar 	/*
732dba5ab66SMarcel Moolenaar 	 * linux appears not to copy flags from the parent socket to the
733dba5ab66SMarcel Moolenaar 	 * accepted one, so we must clear the flags in the new descriptor.
734dba5ab66SMarcel Moolenaar 	 * Ignore any errors, because we already have an open fd.
735dba5ab66SMarcel Moolenaar 	 */
7362ca25ab5SJohn Baldwin 	fd = td->td_retval[0];
7372ca25ab5SJohn Baldwin 	(void)kern_fcntl(td, fd, F_SETFL, 0);
7382ca25ab5SJohn Baldwin 	td->td_retval[0] = fd;
739dba5ab66SMarcel Moolenaar 	return (0);
740c21dee17SSøren Schmidt }
741c21dee17SSøren Schmidt 
74201e0ffbaSAlexander Leidinger struct linux_getsockname_args {
743c21dee17SSøren Schmidt 	int s;
7444af27623STim J. Robbins 	l_uintptr_t addr;
7454af27623STim J. Robbins 	l_uintptr_t namelen;
746c21dee17SSøren Schmidt };
747c21dee17SSøren Schmidt 
74801e0ffbaSAlexander Leidinger static int
749b40ce416SJulian Elischer linux_getsockname(struct thread *td, struct linux_getsockname_args *args)
750c21dee17SSøren Schmidt {
751ef04503dSPeter Wemm 	struct getsockname_args /* {
752c21dee17SSøren Schmidt 		int	fdes;
7533db2a843SBruce Evans 		struct sockaddr * __restrict asa;
7543db2a843SBruce Evans 		socklen_t * __restrict alen;
755ef04503dSPeter Wemm 	} */ bsd_args;
756c21dee17SSøren Schmidt 	int error;
757c21dee17SSøren Schmidt 
758745aaef5SKonstantin Belousov 	bsd_args.fdes = args->s;
7593db2a843SBruce Evans 	/* XXX: */
760745aaef5SKonstantin Belousov 	bsd_args.asa = (struct sockaddr * __restrict)PTRIN(args->addr);
761745aaef5SKonstantin Belousov 	bsd_args.alen = PTRIN(args->namelen);	/* XXX */
7625c8919adSAlexander Leidinger 	error = getsockname(td, &bsd_args);
7635c8919adSAlexander Leidinger 	bsd_to_linux_sockaddr((struct sockaddr *)bsd_args.asa);
764ca26842eSHajimu UMEMOTO 	if (error)
765ca26842eSHajimu UMEMOTO 		return (error);
766745aaef5SKonstantin Belousov 	error = linux_sa_put(PTRIN(args->addr));
767ca26842eSHajimu UMEMOTO 	if (error)
768ca26842eSHajimu UMEMOTO 		return (error);
769ca26842eSHajimu UMEMOTO 	return (0);
770c21dee17SSøren Schmidt }
771c21dee17SSøren Schmidt 
77201e0ffbaSAlexander Leidinger struct linux_getpeername_args {
773c21dee17SSøren Schmidt 	int s;
7744af27623STim J. Robbins 	l_uintptr_t addr;
7754af27623STim J. Robbins 	l_uintptr_t namelen;
776c21dee17SSøren Schmidt };
777c21dee17SSøren Schmidt 
77801e0ffbaSAlexander Leidinger static int
779b40ce416SJulian Elischer linux_getpeername(struct thread *td, struct linux_getpeername_args *args)
780c21dee17SSøren Schmidt {
7815c8919adSAlexander Leidinger 	struct getpeername_args /* {
782c21dee17SSøren Schmidt 		int fdes;
783c21dee17SSøren Schmidt 		caddr_t asa;
784c21dee17SSøren Schmidt 		int *alen;
785ef04503dSPeter Wemm 	} */ bsd_args;
786c21dee17SSøren Schmidt 	int error;
787c21dee17SSøren Schmidt 
788745aaef5SKonstantin Belousov 	bsd_args.fdes = args->s;
789745aaef5SKonstantin Belousov 	bsd_args.asa = (struct sockaddr *)PTRIN(args->addr);
790745aaef5SKonstantin Belousov 	bsd_args.alen = (int *)PTRIN(args->namelen);
7915c8919adSAlexander Leidinger 	error = getpeername(td, &bsd_args);
7925c8919adSAlexander Leidinger 	bsd_to_linux_sockaddr((struct sockaddr *)bsd_args.asa);
793ca26842eSHajimu UMEMOTO 	if (error)
794ca26842eSHajimu UMEMOTO 		return (error);
795745aaef5SKonstantin Belousov 	error = linux_sa_put(PTRIN(args->addr));
796ca26842eSHajimu UMEMOTO 	if (error)
797ca26842eSHajimu UMEMOTO 		return (error);
798ca26842eSHajimu UMEMOTO 	return (0);
799c21dee17SSøren Schmidt }
800c21dee17SSøren Schmidt 
80101e0ffbaSAlexander Leidinger struct linux_socketpair_args {
802c21dee17SSøren Schmidt 	int domain;
803c21dee17SSøren Schmidt 	int type;
804c21dee17SSøren Schmidt 	int protocol;
8054af27623STim J. Robbins 	l_uintptr_t rsv;
806c21dee17SSøren Schmidt };
807c21dee17SSøren Schmidt 
80801e0ffbaSAlexander Leidinger static int
809b40ce416SJulian Elischer linux_socketpair(struct thread *td, struct linux_socketpair_args *args)
810c21dee17SSøren Schmidt {
811ef04503dSPeter Wemm 	struct socketpair_args /* {
812c21dee17SSøren Schmidt 		int domain;
813c21dee17SSøren Schmidt 		int type;
814c21dee17SSøren Schmidt 		int protocol;
815c21dee17SSøren Schmidt 		int *rsv;
816ef04503dSPeter Wemm 	} */ bsd_args;
817c21dee17SSøren Schmidt 
818745aaef5SKonstantin Belousov 	bsd_args.domain = linux_to_bsd_domain(args->domain);
819c21dee17SSøren Schmidt 	if (bsd_args.domain == -1)
8203f3a4815SMarcel Moolenaar 		return (EINVAL);
8213f3a4815SMarcel Moolenaar 
822745aaef5SKonstantin Belousov 	bsd_args.type = args->type;
823745aaef5SKonstantin Belousov 	bsd_args.protocol = args->protocol;
824745aaef5SKonstantin Belousov 	bsd_args.rsv = (int *)PTRIN(args->rsv);
825b40ce416SJulian Elischer 	return (socketpair(td, &bsd_args));
826c21dee17SSøren Schmidt }
827c21dee17SSøren Schmidt 
82801e0ffbaSAlexander Leidinger struct linux_send_args {
829c21dee17SSøren Schmidt 	int s;
8304af27623STim J. Robbins 	l_uintptr_t msg;
831c21dee17SSøren Schmidt 	int len;
832c21dee17SSøren Schmidt 	int flags;
833c21dee17SSøren Schmidt };
834c21dee17SSøren Schmidt 
83501e0ffbaSAlexander Leidinger static int
836b40ce416SJulian Elischer linux_send(struct thread *td, struct linux_send_args *args)
837c21dee17SSøren Schmidt {
83887d72a8fSPoul-Henning Kamp 	struct sendto_args /* {
839c21dee17SSøren Schmidt 		int s;
840c21dee17SSøren Schmidt 		caddr_t buf;
841044af7c3SJonathan Mini 		int len;
842c21dee17SSøren Schmidt 		int flags;
84387d72a8fSPoul-Henning Kamp 		caddr_t to;
84487d72a8fSPoul-Henning Kamp 		int tolen;
845ef04503dSPeter Wemm 	} */ bsd_args;
846c21dee17SSøren Schmidt 
847745aaef5SKonstantin Belousov 	bsd_args.s = args->s;
848745aaef5SKonstantin Belousov 	bsd_args.buf = (caddr_t)PTRIN(args->msg);
849745aaef5SKonstantin Belousov 	bsd_args.len = args->len;
850745aaef5SKonstantin Belousov 	bsd_args.flags = args->flags;
85187d72a8fSPoul-Henning Kamp 	bsd_args.to = NULL;
85287d72a8fSPoul-Henning Kamp 	bsd_args.tolen = 0;
8538d6e40c3SMaxim Sobolev 	return sendto(td, &bsd_args);
854c21dee17SSøren Schmidt }
855c21dee17SSøren Schmidt 
85601e0ffbaSAlexander Leidinger struct linux_recv_args {
857c21dee17SSøren Schmidt 	int s;
8584af27623STim J. Robbins 	l_uintptr_t msg;
859c21dee17SSøren Schmidt 	int len;
860c21dee17SSøren Schmidt 	int flags;
861c21dee17SSøren Schmidt };
862c21dee17SSøren Schmidt 
86301e0ffbaSAlexander Leidinger static int
864b40ce416SJulian Elischer linux_recv(struct thread *td, struct linux_recv_args *args)
865c21dee17SSøren Schmidt {
86687d72a8fSPoul-Henning Kamp 	struct recvfrom_args /* {
867c21dee17SSøren Schmidt 		int s;
868c21dee17SSøren Schmidt 		caddr_t buf;
869c21dee17SSøren Schmidt 		int len;
870c21dee17SSøren Schmidt 		int flags;
87187d72a8fSPoul-Henning Kamp 		struct sockaddr *from;
87287d72a8fSPoul-Henning Kamp 		socklen_t fromlenaddr;
873ef04503dSPeter Wemm 	} */ bsd_args;
874c21dee17SSøren Schmidt 
875745aaef5SKonstantin Belousov 	bsd_args.s = args->s;
876745aaef5SKonstantin Belousov 	bsd_args.buf = (caddr_t)PTRIN(args->msg);
877745aaef5SKonstantin Belousov 	bsd_args.len = args->len;
878745aaef5SKonstantin Belousov 	bsd_args.flags = args->flags;
87987d72a8fSPoul-Henning Kamp 	bsd_args.from = NULL;
88087d72a8fSPoul-Henning Kamp 	bsd_args.fromlenaddr = 0;
88187d72a8fSPoul-Henning Kamp 	return (recvfrom(td, &bsd_args));
882c21dee17SSøren Schmidt }
883c21dee17SSøren Schmidt 
884c21dee17SSøren Schmidt static int
885b40ce416SJulian Elischer linux_sendto(struct thread *td, struct linux_sendto_args *args)
886c21dee17SSøren Schmidt {
8875a8a13e0SDavid Malone 	struct msghdr msg;
8885a8a13e0SDavid Malone 	struct iovec aiov;
8895a8a13e0SDavid Malone 	int error;
890c21dee17SSøren Schmidt 
891745aaef5SKonstantin Belousov 	if (linux_check_hdrincl(td, args->s) == 0)
892f2477ae1SMike Smith 		/* IP_HDRINCL set, tweak the packet before sending */
893745aaef5SKonstantin Belousov 		return (linux_sendto_hdrincl(td, args));
894f2477ae1SMike Smith 
895745aaef5SKonstantin Belousov 	msg.msg_name = PTRIN(args->to);
896745aaef5SKonstantin Belousov 	msg.msg_namelen = args->tolen;
8975a8a13e0SDavid Malone 	msg.msg_iov = &aiov;
8985a8a13e0SDavid Malone 	msg.msg_iovlen = 1;
8995a8a13e0SDavid Malone 	msg.msg_control = NULL;
9005a8a13e0SDavid Malone 	msg.msg_flags = 0;
901745aaef5SKonstantin Belousov 	aiov.iov_base = PTRIN(args->msg);
902745aaef5SKonstantin Belousov 	aiov.iov_len = args->len;
903745aaef5SKonstantin Belousov 	error = linux_sendit(td, args->s, &msg, args->flags, UIO_USERSPACE);
9045a8a13e0SDavid Malone 	return (error);
905c21dee17SSøren Schmidt }
906c21dee17SSøren Schmidt 
90701e0ffbaSAlexander Leidinger struct linux_recvfrom_args {
908c21dee17SSøren Schmidt 	int s;
9094af27623STim J. Robbins 	l_uintptr_t buf;
910c21dee17SSøren Schmidt 	int len;
911c21dee17SSøren Schmidt 	int flags;
9124af27623STim J. Robbins 	l_uintptr_t from;
9134af27623STim J. Robbins 	l_uintptr_t fromlen;
914c21dee17SSøren Schmidt };
915c21dee17SSøren Schmidt 
91601e0ffbaSAlexander Leidinger static int
917b40ce416SJulian Elischer linux_recvfrom(struct thread *td, struct linux_recvfrom_args *args)
918c21dee17SSøren Schmidt {
919ef04503dSPeter Wemm 	struct recvfrom_args /* {
920c21dee17SSøren Schmidt 		int	s;
921c21dee17SSøren Schmidt 		caddr_t	buf;
922c21dee17SSøren Schmidt 		size_t	len;
923c21dee17SSøren Schmidt 		int	flags;
9243db2a843SBruce Evans 		struct sockaddr * __restrict from;
9253db2a843SBruce Evans 		socklen_t * __restrict fromlenaddr;
926ef04503dSPeter Wemm 	} */ bsd_args;
9275c8919adSAlexander Leidinger 	size_t len;
928c21dee17SSøren Schmidt 	int error;
929c21dee17SSøren Schmidt 
930745aaef5SKonstantin Belousov 	if ((error = copyin(PTRIN(args->fromlen), &len, sizeof(size_t))))
9313f3a4815SMarcel Moolenaar 		return (error);
9323f3a4815SMarcel Moolenaar 
933745aaef5SKonstantin Belousov 	bsd_args.s = args->s;
934745aaef5SKonstantin Belousov 	bsd_args.buf = PTRIN(args->buf);
935745aaef5SKonstantin Belousov 	bsd_args.len = args->len;
936745aaef5SKonstantin Belousov 	bsd_args.flags = linux_to_bsd_msg_flags(args->flags);
9373db2a843SBruce Evans 	/* XXX: */
938745aaef5SKonstantin Belousov 	bsd_args.from = (struct sockaddr * __restrict)PTRIN(args->from);
939745aaef5SKonstantin Belousov 	bsd_args.fromlenaddr = PTRIN(args->fromlen);/* XXX */
9405c8919adSAlexander Leidinger 
9415c8919adSAlexander Leidinger 	linux_to_bsd_sockaddr((struct sockaddr *)bsd_args.from, len);
9425c8919adSAlexander Leidinger 	error = recvfrom(td, &bsd_args);
9435c8919adSAlexander Leidinger 	bsd_to_linux_sockaddr((struct sockaddr *)bsd_args.from);
9445c8919adSAlexander Leidinger 
945ca26842eSHajimu UMEMOTO 	if (error)
946ca26842eSHajimu UMEMOTO 		return (error);
947745aaef5SKonstantin Belousov 	if (args->from) {
9484af27623STim J. Robbins 		error = linux_sa_put((struct osockaddr *)
949745aaef5SKonstantin Belousov 		    PTRIN(args->from));
950ca26842eSHajimu UMEMOTO 		if (error)
951ca26842eSHajimu UMEMOTO 			return (error);
952ca26842eSHajimu UMEMOTO 	}
953ca26842eSHajimu UMEMOTO 	return (0);
954ca26842eSHajimu UMEMOTO }
955ca26842eSHajimu UMEMOTO 
95601e0ffbaSAlexander Leidinger struct linux_sendmsg_args {
957ca26842eSHajimu UMEMOTO 	int s;
9584af27623STim J. Robbins 	l_uintptr_t msg;
959ca26842eSHajimu UMEMOTO 	int flags;
960ca26842eSHajimu UMEMOTO };
961ca26842eSHajimu UMEMOTO 
96201e0ffbaSAlexander Leidinger static int
963ca26842eSHajimu UMEMOTO linux_sendmsg(struct thread *td, struct linux_sendmsg_args *args)
964ca26842eSHajimu UMEMOTO {
965ca26842eSHajimu UMEMOTO 	struct msghdr msg;
966552afd9cSPoul-Henning Kamp 	struct iovec *iov;
967ca26842eSHajimu UMEMOTO 	int error;
968ca26842eSHajimu UMEMOTO 
9694af27623STim J. Robbins 	/* XXXTJR sendmsg is broken on amd64 */
9704af27623STim J. Robbins 
971745aaef5SKonstantin Belousov 	error = copyin(PTRIN(args->msg), &msg, sizeof(msg));
972ca26842eSHajimu UMEMOTO 	if (error)
973ca26842eSHajimu UMEMOTO 		return (error);
974d72a6158SRobert Watson 
975d72a6158SRobert Watson 	/*
976d72a6158SRobert Watson 	 * Some Linux applications (ping) define a non-NULL control data
977d72a6158SRobert Watson 	 * pointer, but a msg_controllen of 0, which is not allowed in the
978d72a6158SRobert Watson 	 * FreeBSD system call interface.  NULL the msg_control pointer in
979d72a6158SRobert Watson 	 * order to handle this case.  This should be checked, but allows the
980d72a6158SRobert Watson 	 * Linux ping to work.
981d72a6158SRobert Watson 	 */
982d72a6158SRobert Watson 	if (msg.msg_control != NULL && msg.msg_controllen == 0)
983d72a6158SRobert Watson 		msg.msg_control = NULL;
984552afd9cSPoul-Henning Kamp 	error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
985552afd9cSPoul-Henning Kamp 	if (error)
986552afd9cSPoul-Henning Kamp 		return (error);
9875a8a13e0SDavid Malone 	msg.msg_iov = iov;
9885a8a13e0SDavid Malone 	msg.msg_flags = 0;
989745aaef5SKonstantin Belousov 	error = linux_sendit(td, args->s, &msg, args->flags, UIO_USERSPACE);
990552afd9cSPoul-Henning Kamp 	free(iov, M_IOV);
991ca26842eSHajimu UMEMOTO 	return (error);
992c21dee17SSøren Schmidt }
993c21dee17SSøren Schmidt 
99401e0ffbaSAlexander Leidinger struct linux_recvmsg_args {
99540dbba57SAssar Westerlund 	int s;
9964af27623STim J. Robbins 	l_uintptr_t msg;
99740dbba57SAssar Westerlund 	int flags;
99840dbba57SAssar Westerlund };
99940dbba57SAssar Westerlund 
100001e0ffbaSAlexander Leidinger static int
1001b40ce416SJulian Elischer linux_recvmsg(struct thread *td, struct linux_recvmsg_args *args)
100240dbba57SAssar Westerlund {
100340dbba57SAssar Westerlund 	struct recvmsg_args /* {
100440dbba57SAssar Westerlund 		int	s;
100540dbba57SAssar Westerlund 		struct	msghdr *msg;
100640dbba57SAssar Westerlund 		int	flags;
100740dbba57SAssar Westerlund 	} */ bsd_args;
1008ca26842eSHajimu UMEMOTO 	struct msghdr msg;
100984b11cd8SMitsuru IWASAKI 	struct cmsghdr *cmsg;
101040dbba57SAssar Westerlund 	int error;
101140dbba57SAssar Westerlund 
10124af27623STim J. Robbins 	/* XXXTJR recvmsg is broken on amd64 */
10134af27623STim J. Robbins 
1014d0b2365eSKonstantin Belousov 	if ((error = copyin(PTRIN(args->msg), &msg, sizeof (msg))))
1015d0b2365eSKonstantin Belousov 		return (error);
1016d0b2365eSKonstantin Belousov 
1017745aaef5SKonstantin Belousov 	bsd_args.s = args->s;
1018745aaef5SKonstantin Belousov 	bsd_args.msg = PTRIN(args->msg);
1019745aaef5SKonstantin Belousov 	bsd_args.flags = linux_to_bsd_msg_flags(args->flags);
10205c8919adSAlexander Leidinger 	if (msg.msg_name) {
10215c8919adSAlexander Leidinger 	   	linux_to_bsd_sockaddr((struct sockaddr *)msg.msg_name,
10225c8919adSAlexander Leidinger 		      msg.msg_namelen);
10235c8919adSAlexander Leidinger 		error = recvmsg(td, &bsd_args);
10245c8919adSAlexander Leidinger 		bsd_to_linux_sockaddr((struct sockaddr *)msg.msg_name);
10255c8919adSAlexander Leidinger 	} else
1026ca26842eSHajimu UMEMOTO 	   	error = recvmsg(td, &bsd_args);
1027ca26842eSHajimu UMEMOTO 	if (error)
1028ca26842eSHajimu UMEMOTO 		return (error);
1029ca26842eSHajimu UMEMOTO 
103072261b9fSDag-Erling Smørgrav 	if (bsd_args.msg->msg_control != NULL &&
103172261b9fSDag-Erling Smørgrav 	    bsd_args.msg->msg_controllen > 0) {
103284b11cd8SMitsuru IWASAKI 		cmsg = (struct cmsghdr*)bsd_args.msg->msg_control;
103384b11cd8SMitsuru IWASAKI 		cmsg->cmsg_level = bsd_to_linux_sockopt_level(cmsg->cmsg_level);
103484b11cd8SMitsuru IWASAKI 	}
103584b11cd8SMitsuru IWASAKI 
1036745aaef5SKonstantin Belousov 	error = copyin(PTRIN(args->msg), &msg, sizeof(msg));
1037ca26842eSHajimu UMEMOTO 	if (error)
1038ca26842eSHajimu UMEMOTO 		return (error);
1039ca26842eSHajimu UMEMOTO 	if (msg.msg_name && msg.msg_namelen > 2)
1040ca26842eSHajimu UMEMOTO 		error = linux_sa_put(msg.msg_name);
1041ca26842eSHajimu UMEMOTO 	return (error);
104240dbba57SAssar Westerlund }
104340dbba57SAssar Westerlund 
1044c21dee17SSøren Schmidt struct linux_shutdown_args {
1045c21dee17SSøren Schmidt 	int s;
1046c21dee17SSøren Schmidt 	int how;
1047c21dee17SSøren Schmidt };
1048c21dee17SSøren Schmidt 
1049c21dee17SSøren Schmidt static int
1050b40ce416SJulian Elischer linux_shutdown(struct thread *td, struct linux_shutdown_args *args)
1051c21dee17SSøren Schmidt {
1052ef04503dSPeter Wemm 	struct shutdown_args /* {
1053c21dee17SSøren Schmidt 		int s;
1054c21dee17SSøren Schmidt 		int how;
1055ef04503dSPeter Wemm 	} */ bsd_args;
1056c21dee17SSøren Schmidt 
1057745aaef5SKonstantin Belousov 	bsd_args.s = args->s;
1058745aaef5SKonstantin Belousov 	bsd_args.how = args->how;
1059b40ce416SJulian Elischer 	return (shutdown(td, &bsd_args));
1060c21dee17SSøren Schmidt }
1061c21dee17SSøren Schmidt 
1062c21dee17SSøren Schmidt struct linux_setsockopt_args {
1063c21dee17SSøren Schmidt 	int s;
1064c21dee17SSøren Schmidt 	int level;
1065c21dee17SSøren Schmidt 	int optname;
10664af27623STim J. Robbins 	l_uintptr_t optval;
1067c21dee17SSøren Schmidt 	int optlen;
1068c21dee17SSøren Schmidt };
1069c21dee17SSøren Schmidt 
1070c21dee17SSøren Schmidt static int
1071b40ce416SJulian Elischer linux_setsockopt(struct thread *td, struct linux_setsockopt_args *args)
1072c21dee17SSøren Schmidt {
1073ef04503dSPeter Wemm 	struct setsockopt_args /* {
1074c21dee17SSøren Schmidt 		int s;
1075c21dee17SSøren Schmidt 		int level;
1076c21dee17SSøren Schmidt 		int name;
1077c21dee17SSøren Schmidt 		caddr_t val;
1078c21dee17SSøren Schmidt 		int valsize;
1079ef04503dSPeter Wemm 	} */ bsd_args;
1080c21dee17SSøren Schmidt 	int error, name;
1081c21dee17SSøren Schmidt 
1082745aaef5SKonstantin Belousov 	bsd_args.s = args->s;
1083745aaef5SKonstantin Belousov 	bsd_args.level = linux_to_bsd_sockopt_level(args->level);
1084c21dee17SSøren Schmidt 	switch (bsd_args.level) {
1085c21dee17SSøren Schmidt 	case SOL_SOCKET:
1086745aaef5SKonstantin Belousov 		name = linux_to_bsd_so_sockopt(args->optname);
1087c21dee17SSøren Schmidt 		break;
1088c21dee17SSøren Schmidt 	case IPPROTO_IP:
1089745aaef5SKonstantin Belousov 		name = linux_to_bsd_ip_sockopt(args->optname);
1090c21dee17SSøren Schmidt 		break;
1091dad3b88aSMike Smith 	case IPPROTO_TCP:
1092dad3b88aSMike Smith 		/* Linux TCP option values match BSD's */
1093745aaef5SKonstantin Belousov 		name = args->optname;
1094dad3b88aSMike Smith 		break;
1095c21dee17SSøren Schmidt 	default:
10963f3a4815SMarcel Moolenaar 		name = -1;
10973f3a4815SMarcel Moolenaar 		break;
1098c21dee17SSøren Schmidt 	}
1099c21dee17SSøren Schmidt 	if (name == -1)
1100d4b7423fSAlexander Leidinger 		return (ENOPROTOOPT);
11013f3a4815SMarcel Moolenaar 
1102c21dee17SSøren Schmidt 	bsd_args.name = name;
1103745aaef5SKonstantin Belousov 	bsd_args.val = PTRIN(args->optval);
1104745aaef5SKonstantin Belousov 	bsd_args.valsize = args->optlen;
11055c8919adSAlexander Leidinger 
11065c8919adSAlexander Leidinger 	if (name == IPV6_NEXTHOP) {
11075c8919adSAlexander Leidinger 		linux_to_bsd_sockaddr((struct sockaddr *)bsd_args.val,
11085c8919adSAlexander Leidinger 			bsd_args.valsize);
11095c8919adSAlexander Leidinger 		error = setsockopt(td, &bsd_args);
11105c8919adSAlexander Leidinger 		bsd_to_linux_sockaddr((struct sockaddr *)bsd_args.val);
11115c8919adSAlexander Leidinger 	} else
11125c8919adSAlexander Leidinger 		error = setsockopt(td, &bsd_args);
11135c8919adSAlexander Leidinger 
11145c8919adSAlexander Leidinger 	return (error);
1115c21dee17SSøren Schmidt }
1116c21dee17SSøren Schmidt 
1117c21dee17SSøren Schmidt struct linux_getsockopt_args {
1118c21dee17SSøren Schmidt 	int s;
1119c21dee17SSøren Schmidt 	int level;
1120c21dee17SSøren Schmidt 	int optname;
11214af27623STim J. Robbins 	l_uintptr_t optval;
11224af27623STim J. Robbins 	l_uintptr_t optlen;
1123c21dee17SSøren Schmidt };
1124c21dee17SSøren Schmidt 
1125c21dee17SSøren Schmidt static int
1126b40ce416SJulian Elischer linux_getsockopt(struct thread *td, struct linux_getsockopt_args *args)
1127c21dee17SSøren Schmidt {
1128ef04503dSPeter Wemm 	struct getsockopt_args /* {
1129c21dee17SSøren Schmidt 		int s;
1130c21dee17SSøren Schmidt 		int level;
1131c21dee17SSøren Schmidt 		int name;
1132c21dee17SSøren Schmidt 		caddr_t val;
1133c21dee17SSøren Schmidt 		int *avalsize;
1134ef04503dSPeter Wemm 	} */ bsd_args;
1135c21dee17SSøren Schmidt 	int error, name;
1136c21dee17SSøren Schmidt 
1137745aaef5SKonstantin Belousov 	bsd_args.s = args->s;
1138745aaef5SKonstantin Belousov 	bsd_args.level = linux_to_bsd_sockopt_level(args->level);
1139c21dee17SSøren Schmidt 	switch (bsd_args.level) {
1140c21dee17SSøren Schmidt 	case SOL_SOCKET:
1141745aaef5SKonstantin Belousov 		name = linux_to_bsd_so_sockopt(args->optname);
1142c21dee17SSøren Schmidt 		break;
1143c21dee17SSøren Schmidt 	case IPPROTO_IP:
1144745aaef5SKonstantin Belousov 		name = linux_to_bsd_ip_sockopt(args->optname);
1145c21dee17SSøren Schmidt 		break;
1146dad3b88aSMike Smith 	case IPPROTO_TCP:
1147dad3b88aSMike Smith 		/* Linux TCP option values match BSD's */
1148745aaef5SKonstantin Belousov 		name = args->optname;
1149dad3b88aSMike Smith 		break;
1150c21dee17SSøren Schmidt 	default:
11513f3a4815SMarcel Moolenaar 		name = -1;
11523f3a4815SMarcel Moolenaar 		break;
1153c21dee17SSøren Schmidt 	}
1154c21dee17SSøren Schmidt 	if (name == -1)
11553f3a4815SMarcel Moolenaar 		return (EINVAL);
11563f3a4815SMarcel Moolenaar 
1157f2477ae1SMike Smith 	bsd_args.name = name;
1158745aaef5SKonstantin Belousov 	bsd_args.val = PTRIN(args->optval);
1159745aaef5SKonstantin Belousov 	bsd_args.avalsize = PTRIN(args->optlen);
11605c8919adSAlexander Leidinger 
11615c8919adSAlexander Leidinger 	if (name == IPV6_NEXTHOP) {
11625c8919adSAlexander Leidinger 		error = getsockopt(td, &bsd_args);
11635c8919adSAlexander Leidinger 		bsd_to_linux_sockaddr((struct sockaddr *)bsd_args.val);
11645c8919adSAlexander Leidinger 	} else
11655c8919adSAlexander Leidinger 		error = getsockopt(td, &bsd_args);
11665c8919adSAlexander Leidinger 
11675c8919adSAlexander Leidinger 	return (error);
1168c21dee17SSøren Schmidt }
1169c21dee17SSøren Schmidt 
1170c21dee17SSøren Schmidt int
1171b40ce416SJulian Elischer linux_socketcall(struct thread *td, struct linux_socketcall_args *args)
1172c21dee17SSøren Schmidt {
11734af27623STim J. Robbins 	void *arg = (void *)(intptr_t)args->args;
11743f3a4815SMarcel Moolenaar 
1175c21dee17SSøren Schmidt 	switch (args->what) {
1176c21dee17SSøren Schmidt 	case LINUX_SOCKET:
1177b40ce416SJulian Elischer 		return (linux_socket(td, arg));
1178c21dee17SSøren Schmidt 	case LINUX_BIND:
1179b40ce416SJulian Elischer 		return (linux_bind(td, arg));
1180c21dee17SSøren Schmidt 	case LINUX_CONNECT:
1181b40ce416SJulian Elischer 		return (linux_connect(td, arg));
1182c21dee17SSøren Schmidt 	case LINUX_LISTEN:
1183b40ce416SJulian Elischer 		return (linux_listen(td, arg));
1184c21dee17SSøren Schmidt 	case LINUX_ACCEPT:
1185b40ce416SJulian Elischer 		return (linux_accept(td, arg));
1186c21dee17SSøren Schmidt 	case LINUX_GETSOCKNAME:
1187b40ce416SJulian Elischer 		return (linux_getsockname(td, arg));
1188c21dee17SSøren Schmidt 	case LINUX_GETPEERNAME:
1189b40ce416SJulian Elischer 		return (linux_getpeername(td, arg));
1190c21dee17SSøren Schmidt 	case LINUX_SOCKETPAIR:
1191b40ce416SJulian Elischer 		return (linux_socketpair(td, arg));
1192c21dee17SSøren Schmidt 	case LINUX_SEND:
1193b40ce416SJulian Elischer 		return (linux_send(td, arg));
1194c21dee17SSøren Schmidt 	case LINUX_RECV:
1195b40ce416SJulian Elischer 		return (linux_recv(td, arg));
1196c21dee17SSøren Schmidt 	case LINUX_SENDTO:
1197b40ce416SJulian Elischer 		return (linux_sendto(td, arg));
1198c21dee17SSøren Schmidt 	case LINUX_RECVFROM:
1199b40ce416SJulian Elischer 		return (linux_recvfrom(td, arg));
1200c21dee17SSøren Schmidt 	case LINUX_SHUTDOWN:
1201b40ce416SJulian Elischer 		return (linux_shutdown(td, arg));
1202c21dee17SSøren Schmidt 	case LINUX_SETSOCKOPT:
1203b40ce416SJulian Elischer 		return (linux_setsockopt(td, arg));
1204c21dee17SSøren Schmidt 	case LINUX_GETSOCKOPT:
1205b40ce416SJulian Elischer 		return (linux_getsockopt(td, arg));
1206e76bba09SSøren Schmidt 	case LINUX_SENDMSG:
1207ca26842eSHajimu UMEMOTO 		return (linux_sendmsg(td, arg));
1208e76bba09SSøren Schmidt 	case LINUX_RECVMSG:
1209b40ce416SJulian Elischer 		return (linux_recvmsg(td, arg));
1210c21dee17SSøren Schmidt 	}
12113f3a4815SMarcel Moolenaar 
12123f3a4815SMarcel Moolenaar 	uprintf("LINUX: 'socket' typ=%d not implemented\n", args->what);
12133f3a4815SMarcel Moolenaar 	return (ENOSYS);
1214c21dee17SSøren Schmidt }
1215