linux_socket.c revision 156850
1/*-
2 * Copyright (c) 1995 S�ren Schmidt
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer
10 *    in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 *    derived from this software without specific prior written permission
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <sys/cdefs.h>
30__FBSDID("$FreeBSD: head/sys/compat/linux/linux_socket.c 156850 2006-03-18 20:47:36Z netchild $");
31
32/* XXX we use functions that might not exist. */
33#include "opt_inet6.h"
34
35#include <sys/param.h>
36#include <sys/proc.h>
37#include <sys/systm.h>
38#include <sys/sysproto.h>
39#include <sys/fcntl.h>
40#include <sys/file.h>
41#include <sys/limits.h>
42#include <sys/lock.h>
43#include <sys/malloc.h>
44#include <sys/mutex.h>
45#include <sys/mbuf.h>
46#include <sys/socket.h>
47#include <sys/socketvar.h>
48#include <sys/syscallsubr.h>
49#include <sys/uio.h>
50#include <sys/syslog.h>
51
52#include <netinet/in.h>
53#include <netinet/in_systm.h>
54#include <netinet/ip.h>
55#ifdef INET6
56#include <netinet/ip6.h>
57#include <netinet6/ip6_var.h>
58#endif
59
60#ifdef COMPAT_LINUX32
61#include <machine/../linux32/linux.h>
62#include <machine/../linux32/linux32_proto.h>
63#else
64#include <machine/../linux/linux.h>
65#include <machine/../linux/linux_proto.h>
66#endif
67#include <compat/linux/linux_socket.h>
68#include <compat/linux/linux_util.h>
69
70static int do_sa_get(struct sockaddr **, const struct osockaddr *, int *,
71    struct malloc_type *);
72static int linux_to_bsd_domain(int);
73
74/*
75 * Reads a linux sockaddr and does any necessary translation.
76 * Linux sockaddrs don't have a length field, only a family.
77 */
78static int
79linux_getsockaddr(struct sockaddr **sap, const struct osockaddr *osa, int len)
80{
81	int osalen = len;
82
83	return (do_sa_get(sap, osa, &osalen, M_SONAME));
84}
85
86/*
87 * Copy the osockaddr structure pointed to by osa to kernel, adjust
88 * family and convert to sockaddr.
89 */
90static int
91do_sa_get(struct sockaddr **sap, const struct osockaddr *osa, int *osalen,
92    struct malloc_type *mtype)
93{
94	int error=0, bdom;
95	struct sockaddr *sa;
96	struct osockaddr *kosa;
97	int alloclen;
98#ifdef INET6
99	int oldv6size;
100	struct sockaddr_in6 *sin6;
101#endif
102
103	if (*osalen < 2 || *osalen > UCHAR_MAX || !osa)
104		return (EINVAL);
105
106	alloclen = *osalen;
107#ifdef INET6
108	oldv6size = 0;
109	/*
110	 * Check for old (pre-RFC2553) sockaddr_in6. We may accept it
111	 * if it's a v4-mapped address, so reserve the proper space
112	 * for it.
113	 */
114	if (alloclen == sizeof (struct sockaddr_in6) - sizeof (u_int32_t)) {
115		alloclen = sizeof (struct sockaddr_in6);
116		oldv6size = 1;
117	}
118#endif
119
120	MALLOC(kosa, struct osockaddr *, alloclen, mtype, M_WAITOK);
121
122	if ((error = copyin(osa, kosa, *osalen)))
123		goto out;
124
125	bdom = linux_to_bsd_domain(kosa->sa_family);
126	if (bdom == -1) {
127		error = EINVAL;
128		goto out;
129	}
130
131#ifdef INET6
132	/*
133	 * Older Linux IPv6 code uses obsolete RFC2133 struct sockaddr_in6,
134	 * which lacks the scope id compared with RFC2553 one. If we detect
135	 * the situation, reject the address and write a message to system log.
136	 *
137	 * Still accept addresses for which the scope id is not used.
138	 */
139	if (oldv6size && bdom == AF_INET6) {
140		sin6 = (struct sockaddr_in6 *)kosa;
141		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr) ||
142		    (!IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) &&
143		     !IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr) &&
144		     !IN6_IS_ADDR_V4COMPAT(&sin6->sin6_addr) &&
145		     !IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) &&
146		     !IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))) {
147			sin6->sin6_scope_id = 0;
148		} else {
149			log(LOG_DEBUG,
150			    "obsolete pre-RFC2553 sockaddr_in6 rejected\n");
151			error = EINVAL;
152			goto out;
153		}
154	} else
155#endif
156	if (bdom == AF_INET)
157		alloclen = sizeof(struct sockaddr_in);
158
159	sa = (struct sockaddr *) kosa;
160	sa->sa_family = bdom;
161	sa->sa_len = alloclen;
162
163	*sap = sa;
164	*osalen = alloclen;
165	return (0);
166
167out:
168	FREE(kosa, mtype);
169	return (error);
170}
171
172static int
173linux_to_bsd_domain(int domain)
174{
175
176	switch (domain) {
177	case LINUX_AF_UNSPEC:
178		return (AF_UNSPEC);
179	case LINUX_AF_UNIX:
180		return (AF_LOCAL);
181	case LINUX_AF_INET:
182		return (AF_INET);
183	case LINUX_AF_INET6:
184		return (AF_INET6);
185	case LINUX_AF_AX25:
186		return (AF_CCITT);
187	case LINUX_AF_IPX:
188		return (AF_IPX);
189	case LINUX_AF_APPLETALK:
190		return (AF_APPLETALK);
191	}
192	return (-1);
193}
194
195#ifndef __alpha__
196static int
197bsd_to_linux_domain(int domain)
198{
199
200	switch (domain) {
201	case AF_UNSPEC:
202		return (LINUX_AF_UNSPEC);
203	case AF_LOCAL:
204		return (LINUX_AF_UNIX);
205	case AF_INET:
206		return (LINUX_AF_INET);
207	case AF_INET6:
208		return (LINUX_AF_INET6);
209	case AF_CCITT:
210		return (LINUX_AF_AX25);
211	case AF_IPX:
212		return (LINUX_AF_IPX);
213	case AF_APPLETALK:
214		return (LINUX_AF_APPLETALK);
215	}
216	return (-1);
217}
218
219static int
220linux_to_bsd_sockopt_level(int level)
221{
222
223	switch (level) {
224	case LINUX_SOL_SOCKET:
225		return (SOL_SOCKET);
226	}
227	return (level);
228}
229
230static int
231bsd_to_linux_sockopt_level(int level)
232{
233
234	switch (level) {
235	case SOL_SOCKET:
236		return (LINUX_SOL_SOCKET);
237	}
238	return (level);
239}
240
241static int
242linux_to_bsd_ip_sockopt(int opt)
243{
244
245	switch (opt) {
246	case LINUX_IP_TOS:
247		return (IP_TOS);
248	case LINUX_IP_TTL:
249		return (IP_TTL);
250	case LINUX_IP_OPTIONS:
251		return (IP_OPTIONS);
252	case LINUX_IP_MULTICAST_IF:
253		return (IP_MULTICAST_IF);
254	case LINUX_IP_MULTICAST_TTL:
255		return (IP_MULTICAST_TTL);
256	case LINUX_IP_MULTICAST_LOOP:
257		return (IP_MULTICAST_LOOP);
258	case LINUX_IP_ADD_MEMBERSHIP:
259		return (IP_ADD_MEMBERSHIP);
260	case LINUX_IP_DROP_MEMBERSHIP:
261		return (IP_DROP_MEMBERSHIP);
262	case LINUX_IP_HDRINCL:
263		return (IP_HDRINCL);
264	}
265	return (-1);
266}
267
268static int
269linux_to_bsd_so_sockopt(int opt)
270{
271
272	switch (opt) {
273	case LINUX_SO_DEBUG:
274		return (SO_DEBUG);
275	case LINUX_SO_REUSEADDR:
276		return (SO_REUSEADDR);
277	case LINUX_SO_TYPE:
278		return (SO_TYPE);
279	case LINUX_SO_ERROR:
280		return (SO_ERROR);
281	case LINUX_SO_DONTROUTE:
282		return (SO_DONTROUTE);
283	case LINUX_SO_BROADCAST:
284		return (SO_BROADCAST);
285	case LINUX_SO_SNDBUF:
286		return (SO_SNDBUF);
287	case LINUX_SO_RCVBUF:
288		return (SO_RCVBUF);
289	case LINUX_SO_KEEPALIVE:
290		return (SO_KEEPALIVE);
291	case LINUX_SO_OOBINLINE:
292		return (SO_OOBINLINE);
293	case LINUX_SO_LINGER:
294		return (SO_LINGER);
295	}
296	return (-1);
297}
298
299static int
300linux_to_bsd_msg_flags(int flags)
301{
302	int ret_flags = 0;
303
304	if (flags & LINUX_MSG_OOB)
305		ret_flags |= MSG_OOB;
306	if (flags & LINUX_MSG_PEEK)
307		ret_flags |= MSG_PEEK;
308	if (flags & LINUX_MSG_DONTROUTE)
309		ret_flags |= MSG_DONTROUTE;
310	if (flags & LINUX_MSG_CTRUNC)
311		ret_flags |= MSG_CTRUNC;
312	if (flags & LINUX_MSG_TRUNC)
313		ret_flags |= MSG_TRUNC;
314	if (flags & LINUX_MSG_DONTWAIT)
315		ret_flags |= MSG_DONTWAIT;
316	if (flags & LINUX_MSG_EOR)
317		ret_flags |= MSG_EOR;
318	if (flags & LINUX_MSG_WAITALL)
319		ret_flags |= MSG_WAITALL;
320	if (flags & LINUX_MSG_NOSIGNAL)
321		ret_flags |= MSG_NOSIGNAL;
322#if 0 /* not handled */
323	if (flags & LINUX_MSG_PROXY)
324		;
325	if (flags & LINUX_MSG_FIN)
326		;
327	if (flags & LINUX_MSG_SYN)
328		;
329	if (flags & LINUX_MSG_CONFIRM)
330		;
331	if (flags & LINUX_MSG_RST)
332		;
333	if (flags & LINUX_MSG_ERRQUEUE)
334		;
335#endif
336	return ret_flags;
337}
338
339/*
340* If bsd_to_linux_sockaddr() or linux_to_bsd_sockaddr() faults, then the
341* native syscall will fault.  Thus, we don't really need to check the
342* return values for these functions.
343*/
344
345static int
346bsd_to_linux_sockaddr(struct sockaddr *arg)
347{
348	struct sockaddr sa;
349	size_t sa_len = sizeof(struct sockaddr);
350	int error;
351
352	if ((error = copyin(arg, &sa, sa_len)))
353		return (error);
354
355	*(u_short *)&sa = sa.sa_family;
356
357	error = copyout(&sa, arg, sa_len);
358
359	return (error);
360}
361
362static int
363linux_to_bsd_sockaddr(struct sockaddr *arg, int len)
364{
365	struct sockaddr sa;
366	size_t sa_len = sizeof(struct sockaddr);
367	int error;
368
369	if ((error = copyin(arg, &sa, sa_len)))
370		return (error);
371
372	sa.sa_family = *(sa_family_t *)&sa;
373	sa.sa_len = len;
374
375	error = copyout(&sa, arg, sa_len);
376
377	return (error);
378}
379
380
381static int
382linux_sa_put(struct osockaddr *osa)
383{
384	struct osockaddr sa;
385	int error, bdom;
386
387	/*
388	 * Only read/write the osockaddr family part, the rest is
389	 * not changed.
390	 */
391	error = copyin(osa, &sa, sizeof(sa.sa_family));
392	if (error)
393		return (error);
394
395	bdom = bsd_to_linux_domain(sa.sa_family);
396	if (bdom == -1)
397		return (EINVAL);
398
399	sa.sa_family = bdom;
400	error = copyout(&sa, osa, sizeof(sa.sa_family));
401	if (error)
402		return (error);
403
404	return (0);
405}
406
407static int
408linux_sendit(struct thread *td, int s, struct msghdr *mp, int flags,
409    enum uio_seg segflg)
410{
411	struct mbuf *control;
412	struct sockaddr *to;
413	int error;
414
415	if (mp->msg_name != NULL) {
416		error = linux_getsockaddr(&to, mp->msg_name, mp->msg_namelen);
417		if (error)
418			return (error);
419		mp->msg_name = to;
420	} else
421		to = NULL;
422
423	if (mp->msg_control != NULL) {
424		struct cmsghdr *cmsg;
425
426		if (mp->msg_controllen < sizeof(struct cmsghdr)) {
427			error = EINVAL;
428			goto bad;
429		}
430		error = sockargs(&control, mp->msg_control,
431		    mp->msg_controllen, MT_CONTROL);
432		if (error)
433			goto bad;
434
435		cmsg = mtod(control, struct cmsghdr *);
436		cmsg->cmsg_level = linux_to_bsd_sockopt_level(cmsg->cmsg_level);
437	} else
438		control = NULL;
439
440	error = kern_sendit(td, s, mp, linux_to_bsd_msg_flags(flags), control,
441	    segflg);
442
443bad:
444	if (to)
445		FREE(to, M_SONAME);
446	return (error);
447}
448
449/* Return 0 if IP_HDRINCL is set for the given socket. */
450static int
451linux_check_hdrincl(struct thread *td, int s)
452{
453	int error, optval, size_val;
454
455	size_val = sizeof(optval);
456	error = kern_getsockopt(td, s, IPPROTO_IP, IP_HDRINCL,
457	    &optval, UIO_SYSSPACE, &size_val);
458	if (error)
459		return (error);
460
461	return (optval == 0);
462}
463
464struct linux_sendto_args {
465	int s;
466	l_uintptr_t msg;
467	int len;
468	int flags;
469	l_uintptr_t to;
470	int tolen;
471};
472
473/*
474 * Updated sendto() when IP_HDRINCL is set:
475 * tweak endian-dependent fields in the IP packet.
476 */
477static int
478linux_sendto_hdrincl(struct thread *td, struct linux_sendto_args *linux_args)
479{
480/*
481 * linux_ip_copysize defines how many bytes we should copy
482 * from the beginning of the IP packet before we customize it for BSD.
483 * It should include all the fields we modify (ip_len and ip_off).
484 */
485#define linux_ip_copysize	8
486
487	struct ip *packet;
488	struct msghdr msg;
489	struct iovec aiov[1];
490	int error;
491
492	/* Check that the packet isn't too big or too small. */
493	if (linux_args->len < linux_ip_copysize ||
494	    linux_args->len > IP_MAXPACKET)
495		return (EINVAL);
496
497	packet = (struct ip *)malloc(linux_args->len, M_TEMP, M_WAITOK);
498
499	/* Make kernel copy of the packet to be sent */
500	if ((error = copyin(PTRIN(linux_args->msg), packet,
501	    linux_args->len)))
502		goto goout;
503
504	/* Convert fields from Linux to BSD raw IP socket format */
505	packet->ip_len = linux_args->len;
506	packet->ip_off = ntohs(packet->ip_off);
507
508	/* Prepare the msghdr and iovec structures describing the new packet */
509	msg.msg_name = PTRIN(linux_args->to);
510	msg.msg_namelen = linux_args->tolen;
511	msg.msg_iov = aiov;
512	msg.msg_iovlen = 1;
513	msg.msg_control = NULL;
514	msg.msg_flags = 0;
515	aiov[0].iov_base = (char *)packet;
516	aiov[0].iov_len = linux_args->len;
517	error = linux_sendit(td, linux_args->s, &msg, linux_args->flags,
518	    UIO_SYSSPACE);
519goout:
520	free(packet, M_TEMP);
521	return (error);
522}
523
524struct linux_socket_args {
525	int domain;
526	int type;
527	int protocol;
528};
529
530static int
531linux_socket(struct thread *td, struct linux_socket_args *args)
532{
533	struct linux_socket_args linux_args;
534	struct socket_args /* {
535		int domain;
536		int type;
537		int protocol;
538	} */ bsd_args;
539	int error;
540	int retval_socket;
541
542	if ((error = copyin(args, &linux_args, sizeof(linux_args))))
543		return (error);
544
545	bsd_args.protocol = linux_args.protocol;
546	bsd_args.type = linux_args.type;
547	bsd_args.domain = linux_to_bsd_domain(linux_args.domain);
548	if (bsd_args.domain == -1)
549		return (EINVAL);
550
551	retval_socket = socket(td, &bsd_args);
552	if (bsd_args.type == SOCK_RAW
553	    && (bsd_args.protocol == IPPROTO_RAW || bsd_args.protocol == 0)
554	    && bsd_args.domain == AF_INET
555	    && retval_socket >= 0) {
556		/* It's a raw IP socket: set the IP_HDRINCL option. */
557		int hdrincl;
558
559		hdrincl = 1;
560		/* We ignore any error returned by kern_setsockopt() */
561		kern_setsockopt(td, td->td_retval[0], IPPROTO_IP, IP_HDRINCL,
562		    &hdrincl, UIO_SYSSPACE, sizeof(hdrincl));
563	}
564#ifdef INET6
565	/*
566	 * Linux AF_INET6 socket has IPV6_V6ONLY setsockopt set to 0 by
567	 * default and some apps depend on this. So, set V6ONLY to 0
568	 * for Linux apps if the sysctl value is set to 1.
569	 */
570	if (bsd_args.domain == PF_INET6 && retval_socket >= 0
571#ifndef KLD_MODULE
572	    /*
573	     * XXX: Avoid undefined symbol error with an IPv4 only
574	     * kernel.
575	     */
576	    && ip6_v6only
577#endif
578	    ) {
579		int v6only;
580
581		v6only = 0;
582		/* We ignore any error returned by setsockopt() */
583		kern_setsockopt(td, td->td_retval[0], IPPROTO_IPV6, IPV6_V6ONLY,
584		    &v6only, UIO_SYSSPACE, sizeof(v6only));
585	}
586#endif
587
588	return (retval_socket);
589}
590
591struct linux_bind_args {
592	int s;
593	l_uintptr_t name;
594	int namelen;
595};
596
597static int
598linux_bind(struct thread *td, struct linux_bind_args *args)
599{
600	struct linux_bind_args linux_args;
601	struct sockaddr *sa;
602	int error;
603
604	if ((error = copyin(args, &linux_args, sizeof(linux_args))))
605		return (error);
606
607	error = linux_getsockaddr(&sa, PTRIN(linux_args.name),
608	    linux_args.namelen);
609	if (error)
610		return (error);
611
612	return (kern_bind(td, linux_args.s, sa));
613}
614
615struct linux_connect_args {
616	int s;
617	l_uintptr_t name;
618	int namelen;
619};
620int linux_connect(struct thread *, struct linux_connect_args *);
621#endif /* !__alpha__*/
622
623int
624linux_connect(struct thread *td, struct linux_connect_args *args)
625{
626	struct linux_connect_args linux_args;
627	struct socket *so;
628	struct sockaddr *sa;
629	u_int fflag;
630	int error;
631
632#ifdef __alpha__
633	bcopy(args, &linux_args, sizeof(linux_args));
634#else
635	if ((error = copyin(args, &linux_args, sizeof(linux_args))))
636		return (error);
637#endif /* __alpha__ */
638
639	error = linux_getsockaddr(&sa,
640	    (struct osockaddr *)PTRIN(linux_args.name),
641	    linux_args.namelen);
642	if (error)
643		return (error);
644
645	error = kern_connect(td, linux_args.s, sa);
646	if (error != EISCONN)
647		return (error);
648
649	/*
650	 * Linux doesn't return EISCONN the first time it occurs,
651	 * when on a non-blocking socket. Instead it returns the
652	 * error getsockopt(SOL_SOCKET, SO_ERROR) would return on BSD.
653	 */
654	NET_LOCK_GIANT();
655	error = fgetsock(td, linux_args.s, &so, &fflag);
656	if (error == 0) {
657		error = EISCONN;
658		if (fflag & FNONBLOCK) {
659			SOCK_LOCK(so);
660			if (so->so_emuldata == 0)
661				error = so->so_error;
662			so->so_emuldata = (void *)1;
663			SOCK_UNLOCK(so);
664		}
665		fputsock(so);
666	}
667	NET_UNLOCK_GIANT();
668	return (error);
669}
670
671#ifndef __alpha__
672
673struct linux_listen_args {
674	int s;
675	int backlog;
676};
677
678static int
679linux_listen(struct thread *td, struct linux_listen_args *args)
680{
681	struct linux_listen_args linux_args;
682	struct listen_args /* {
683		int s;
684		int backlog;
685	} */ bsd_args;
686	int error;
687
688	if ((error = copyin(args, &linux_args, sizeof(linux_args))))
689		return (error);
690
691	bsd_args.s = linux_args.s;
692	bsd_args.backlog = linux_args.backlog;
693	return (listen(td, &bsd_args));
694}
695
696struct linux_accept_args {
697	int s;
698	l_uintptr_t addr;
699	l_uintptr_t namelen;
700};
701
702static int
703linux_accept(struct thread *td, struct linux_accept_args *args)
704{
705	struct linux_accept_args linux_args;
706	struct accept_args /* {
707		int	s;
708		struct sockaddr * __restrict name;
709		socklen_t * __restrict anamelen;
710	} */ bsd_args;
711	struct close_args /* {
712		int     fd;
713	} */ c_args;
714	int error, fd;
715
716	if ((error = copyin(args, &linux_args, sizeof(linux_args))))
717		return (error);
718
719	bsd_args.s = linux_args.s;
720	/* XXX: */
721	bsd_args.name = (struct sockaddr * __restrict)PTRIN(linux_args.addr);
722	bsd_args.anamelen = PTRIN(linux_args.namelen);/* XXX */
723	error = accept(td, &bsd_args);
724	bsd_to_linux_sockaddr((struct sockaddr *)bsd_args.name);
725	if (error)
726		return (error);
727	if (linux_args.addr) {
728		error = linux_sa_put(PTRIN(linux_args.addr));
729		if (error) {
730			c_args.fd = td->td_retval[0];
731			(void)close(td, &c_args);
732			return (error);
733		}
734	}
735
736	/*
737	 * linux appears not to copy flags from the parent socket to the
738	 * accepted one, so we must clear the flags in the new descriptor.
739	 * Ignore any errors, because we already have an open fd.
740	 */
741	fd = td->td_retval[0];
742	(void)kern_fcntl(td, fd, F_SETFL, 0);
743	td->td_retval[0] = fd;
744	return (0);
745}
746
747struct linux_getsockname_args {
748	int s;
749	l_uintptr_t addr;
750	l_uintptr_t namelen;
751};
752
753static int
754linux_getsockname(struct thread *td, struct linux_getsockname_args *args)
755{
756	struct linux_getsockname_args linux_args;
757	struct getsockname_args /* {
758		int	fdes;
759		struct sockaddr * __restrict asa;
760		socklen_t * __restrict alen;
761	} */ bsd_args;
762	int error;
763
764	if ((error = copyin(args, &linux_args, sizeof(linux_args))))
765		return (error);
766
767	bsd_args.fdes = linux_args.s;
768	/* XXX: */
769	bsd_args.asa = (struct sockaddr * __restrict)PTRIN(linux_args.addr);
770	bsd_args.alen = PTRIN(linux_args.namelen);	/* XXX */
771	error = getsockname(td, &bsd_args);
772	bsd_to_linux_sockaddr((struct sockaddr *)bsd_args.asa);
773	if (error)
774		return (error);
775	error = linux_sa_put(PTRIN(linux_args.addr));
776	if (error)
777		return (error);
778	return (0);
779}
780
781struct linux_getpeername_args {
782	int s;
783	l_uintptr_t addr;
784	l_uintptr_t namelen;
785};
786
787static int
788linux_getpeername(struct thread *td, struct linux_getpeername_args *args)
789{
790	struct linux_getpeername_args linux_args;
791	struct getpeername_args /* {
792		int fdes;
793		caddr_t asa;
794		int *alen;
795	} */ bsd_args;
796	int error;
797
798	if ((error = copyin(args, &linux_args, sizeof(linux_args))))
799		return (error);
800
801	bsd_args.fdes = linux_args.s;
802	bsd_args.asa = (struct sockaddr *)PTRIN(linux_args.addr);
803	bsd_args.alen = (int *)PTRIN(linux_args.namelen);
804	error = getpeername(td, &bsd_args);
805	bsd_to_linux_sockaddr((struct sockaddr *)bsd_args.asa);
806	if (error)
807		return (error);
808	error = linux_sa_put(PTRIN(linux_args.addr));
809	if (error)
810		return (error);
811	return (0);
812}
813
814struct linux_socketpair_args {
815	int domain;
816	int type;
817	int protocol;
818	l_uintptr_t rsv;
819};
820
821static int
822linux_socketpair(struct thread *td, struct linux_socketpair_args *args)
823{
824	struct linux_socketpair_args linux_args;
825	struct socketpair_args /* {
826		int domain;
827		int type;
828		int protocol;
829		int *rsv;
830	} */ bsd_args;
831	int error;
832
833	if ((error = copyin(args, &linux_args, sizeof(linux_args))))
834		return (error);
835
836	bsd_args.domain = linux_to_bsd_domain(linux_args.domain);
837	if (bsd_args.domain == -1)
838		return (EINVAL);
839
840	bsd_args.type = linux_args.type;
841	bsd_args.protocol = linux_args.protocol;
842	bsd_args.rsv = (int *)PTRIN(linux_args.rsv);
843	return (socketpair(td, &bsd_args));
844}
845
846struct linux_send_args {
847	int s;
848	l_uintptr_t msg;
849	int len;
850	int flags;
851};
852
853static int
854linux_send(struct thread *td, struct linux_send_args *args)
855{
856	struct linux_send_args linux_args;
857	struct sendto_args /* {
858		int s;
859		caddr_t buf;
860		int len;
861		int flags;
862		caddr_t to;
863		int tolen;
864	} */ bsd_args;
865	int error;
866
867	if ((error = copyin(args, &linux_args, sizeof(linux_args))))
868		return (error);
869
870	bsd_args.s = linux_args.s;
871	bsd_args.buf = (caddr_t)PTRIN(linux_args.msg);
872	bsd_args.len = linux_args.len;
873	bsd_args.flags = linux_args.flags;
874	bsd_args.to = NULL;
875	bsd_args.tolen = 0;
876	return sendto(td, &bsd_args);
877}
878
879struct linux_recv_args {
880	int s;
881	l_uintptr_t msg;
882	int len;
883	int flags;
884};
885
886static int
887linux_recv(struct thread *td, struct linux_recv_args *args)
888{
889	struct linux_recv_args linux_args;
890	struct recvfrom_args /* {
891		int s;
892		caddr_t buf;
893		int len;
894		int flags;
895		struct sockaddr *from;
896		socklen_t fromlenaddr;
897	} */ bsd_args;
898	int error;
899
900	if ((error = copyin(args, &linux_args, sizeof(linux_args))))
901		return (error);
902
903	bsd_args.s = linux_args.s;
904	bsd_args.buf = (caddr_t)PTRIN(linux_args.msg);
905	bsd_args.len = linux_args.len;
906	bsd_args.flags = linux_args.flags;
907	bsd_args.from = NULL;
908	bsd_args.fromlenaddr = 0;
909	return (recvfrom(td, &bsd_args));
910}
911
912static int
913linux_sendto(struct thread *td, struct linux_sendto_args *args)
914{
915	struct linux_sendto_args linux_args;
916	struct msghdr msg;
917	struct iovec aiov;
918	int error;
919
920	if ((error = copyin(args, &linux_args, sizeof(linux_args))))
921		return (error);
922
923	if (linux_check_hdrincl(td, linux_args.s) == 0)
924		/* IP_HDRINCL set, tweak the packet before sending */
925		return (linux_sendto_hdrincl(td, &linux_args));
926
927	msg.msg_name = PTRIN(linux_args.to);
928	msg.msg_namelen = linux_args.tolen;
929	msg.msg_iov = &aiov;
930	msg.msg_iovlen = 1;
931	msg.msg_control = NULL;
932	msg.msg_flags = 0;
933	aiov.iov_base = PTRIN(linux_args.msg);
934	aiov.iov_len = linux_args.len;
935	error = linux_sendit(td, linux_args.s, &msg, linux_args.flags,
936	    UIO_USERSPACE);
937	return (error);
938}
939
940struct linux_recvfrom_args {
941	int s;
942	l_uintptr_t buf;
943	int len;
944	int flags;
945	l_uintptr_t from;
946	l_uintptr_t fromlen;
947};
948
949static int
950linux_recvfrom(struct thread *td, struct linux_recvfrom_args *args)
951{
952	struct linux_recvfrom_args linux_args;
953	struct recvfrom_args /* {
954		int	s;
955		caddr_t	buf;
956		size_t	len;
957		int	flags;
958		struct sockaddr * __restrict from;
959		socklen_t * __restrict fromlenaddr;
960	} */ bsd_args;
961	size_t len;
962	int error;
963
964	if ((error = copyin(args, &linux_args, sizeof(linux_args))))
965		return (error);
966
967	if ((error = copyin(PTRIN(linux_args.fromlen), &len, sizeof(size_t))))
968		return (error);
969
970	bsd_args.s = linux_args.s;
971	bsd_args.buf = PTRIN(linux_args.buf);
972	bsd_args.len = linux_args.len;
973	bsd_args.flags = linux_to_bsd_msg_flags(linux_args.flags);
974	/* XXX: */
975	bsd_args.from = (struct sockaddr * __restrict)PTRIN(linux_args.from);
976	bsd_args.fromlenaddr = PTRIN(linux_args.fromlen);/* XXX */
977
978	linux_to_bsd_sockaddr((struct sockaddr *)bsd_args.from, len);
979	error = recvfrom(td, &bsd_args);
980	bsd_to_linux_sockaddr((struct sockaddr *)bsd_args.from);
981
982	if (error)
983		return (error);
984	if (linux_args.from) {
985		error = linux_sa_put((struct osockaddr *)
986		    PTRIN(linux_args.from));
987		if (error)
988			return (error);
989	}
990	return (0);
991}
992
993struct linux_sendmsg_args {
994	int s;
995	l_uintptr_t msg;
996	int flags;
997};
998
999static int
1000linux_sendmsg(struct thread *td, struct linux_sendmsg_args *args)
1001{
1002	struct linux_sendmsg_args linux_args;
1003	struct msghdr msg;
1004	struct iovec *iov;
1005	int error;
1006
1007	/* XXXTJR sendmsg is broken on amd64 */
1008
1009	error = copyin(args, &linux_args, sizeof(linux_args));
1010	if (error)
1011		return (error);
1012	error = copyin(PTRIN(linux_args.msg), &msg, sizeof(msg));
1013	if (error)
1014		return (error);
1015	error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
1016	if (error)
1017		return (error);
1018	msg.msg_iov = iov;
1019	msg.msg_flags = 0;
1020	error = linux_sendit(td, linux_args.s, &msg, linux_args.flags,
1021	    UIO_USERSPACE);
1022	free(iov, M_IOV);
1023	return (error);
1024}
1025
1026struct linux_recvmsg_args {
1027	int s;
1028	l_uintptr_t msg;
1029	int flags;
1030};
1031
1032static int
1033linux_recvmsg(struct thread *td, struct linux_recvmsg_args *args)
1034{
1035	struct linux_recvmsg_args linux_args;
1036	struct recvmsg_args /* {
1037		int	s;
1038		struct	msghdr *msg;
1039		int	flags;
1040	} */ bsd_args;
1041	struct msghdr msg;
1042	struct cmsghdr *cmsg;
1043	int error;
1044
1045	/* XXXTJR recvmsg is broken on amd64 */
1046
1047	if ((error = copyin(args, &linux_args, sizeof(linux_args))))
1048		return (error);
1049
1050	bsd_args.s = linux_args.s;
1051	bsd_args.msg = PTRIN(linux_args.msg);
1052	bsd_args.flags = linux_to_bsd_msg_flags(linux_args.flags);
1053	if (msg.msg_name) {
1054	   	linux_to_bsd_sockaddr((struct sockaddr *)msg.msg_name,
1055		      msg.msg_namelen);
1056		error = recvmsg(td, &bsd_args);
1057		bsd_to_linux_sockaddr((struct sockaddr *)msg.msg_name);
1058	} else
1059	   	error = recvmsg(td, &bsd_args);
1060	if (error)
1061		return (error);
1062
1063	if (bsd_args.msg->msg_control != NULL &&
1064	    bsd_args.msg->msg_controllen > 0) {
1065		cmsg = (struct cmsghdr*)bsd_args.msg->msg_control;
1066		cmsg->cmsg_level = bsd_to_linux_sockopt_level(cmsg->cmsg_level);
1067	}
1068
1069	error = copyin(PTRIN(linux_args.msg), &msg, sizeof(msg));
1070	if (error)
1071		return (error);
1072	if (msg.msg_name && msg.msg_namelen > 2)
1073		error = linux_sa_put(msg.msg_name);
1074	return (error);
1075}
1076
1077struct linux_shutdown_args {
1078	int s;
1079	int how;
1080};
1081
1082static int
1083linux_shutdown(struct thread *td, struct linux_shutdown_args *args)
1084{
1085	struct linux_shutdown_args linux_args;
1086	struct shutdown_args /* {
1087		int s;
1088		int how;
1089	} */ bsd_args;
1090	int error;
1091
1092	if ((error = copyin(args, &linux_args, sizeof(linux_args))))
1093		return (error);
1094
1095	bsd_args.s = linux_args.s;
1096	bsd_args.how = linux_args.how;
1097	return (shutdown(td, &bsd_args));
1098}
1099
1100struct linux_setsockopt_args {
1101	int s;
1102	int level;
1103	int optname;
1104	l_uintptr_t optval;
1105	int optlen;
1106};
1107
1108static int
1109linux_setsockopt(struct thread *td, struct linux_setsockopt_args *args)
1110{
1111	struct linux_setsockopt_args linux_args;
1112	struct setsockopt_args /* {
1113		int s;
1114		int level;
1115		int name;
1116		caddr_t val;
1117		int valsize;
1118	} */ bsd_args;
1119	int error, name;
1120
1121	if ((error = copyin(args, &linux_args, sizeof(linux_args))))
1122		return (error);
1123
1124	bsd_args.s = linux_args.s;
1125	bsd_args.level = linux_to_bsd_sockopt_level(linux_args.level);
1126	switch (bsd_args.level) {
1127	case SOL_SOCKET:
1128		name = linux_to_bsd_so_sockopt(linux_args.optname);
1129		break;
1130	case IPPROTO_IP:
1131		name = linux_to_bsd_ip_sockopt(linux_args.optname);
1132		break;
1133	case IPPROTO_TCP:
1134		/* Linux TCP option values match BSD's */
1135		name = linux_args.optname;
1136		break;
1137	default:
1138		name = -1;
1139		break;
1140	}
1141	if (name == -1)
1142		return (EINVAL);
1143
1144	bsd_args.name = name;
1145	bsd_args.val = PTRIN(linux_args.optval);
1146	bsd_args.valsize = linux_args.optlen;
1147
1148	if (name == IPV6_NEXTHOP) {
1149		linux_to_bsd_sockaddr((struct sockaddr *)bsd_args.val,
1150			bsd_args.valsize);
1151		error = setsockopt(td, &bsd_args);
1152		bsd_to_linux_sockaddr((struct sockaddr *)bsd_args.val);
1153	} else
1154		error = setsockopt(td, &bsd_args);
1155
1156	return (error);
1157}
1158
1159struct linux_getsockopt_args {
1160	int s;
1161	int level;
1162	int optname;
1163	l_uintptr_t optval;
1164	l_uintptr_t optlen;
1165};
1166
1167static int
1168linux_getsockopt(struct thread *td, struct linux_getsockopt_args *args)
1169{
1170	struct linux_getsockopt_args linux_args;
1171	struct getsockopt_args /* {
1172		int s;
1173		int level;
1174		int name;
1175		caddr_t val;
1176		int *avalsize;
1177	} */ bsd_args;
1178	int error, name;
1179
1180	if ((error = copyin(args, &linux_args, sizeof(linux_args))))
1181		return (error);
1182
1183	bsd_args.s = linux_args.s;
1184	bsd_args.level = linux_to_bsd_sockopt_level(linux_args.level);
1185	switch (bsd_args.level) {
1186	case SOL_SOCKET:
1187		name = linux_to_bsd_so_sockopt(linux_args.optname);
1188		break;
1189	case IPPROTO_IP:
1190		name = linux_to_bsd_ip_sockopt(linux_args.optname);
1191		break;
1192	case IPPROTO_TCP:
1193		/* Linux TCP option values match BSD's */
1194		name = linux_args.optname;
1195		break;
1196	default:
1197		name = -1;
1198		break;
1199	}
1200	if (name == -1)
1201		return (EINVAL);
1202
1203	bsd_args.name = name;
1204	bsd_args.val = PTRIN(linux_args.optval);
1205	bsd_args.avalsize = PTRIN(linux_args.optlen);
1206
1207	if (name == IPV6_NEXTHOP) {
1208		error = getsockopt(td, &bsd_args);
1209		bsd_to_linux_sockaddr((struct sockaddr *)bsd_args.val);
1210	} else
1211		error = getsockopt(td, &bsd_args);
1212
1213	return (error);
1214}
1215
1216int
1217linux_socketcall(struct thread *td, struct linux_socketcall_args *args)
1218{
1219	void *arg = (void *)(intptr_t)args->args;
1220
1221	switch (args->what) {
1222	case LINUX_SOCKET:
1223		return (linux_socket(td, arg));
1224	case LINUX_BIND:
1225		return (linux_bind(td, arg));
1226	case LINUX_CONNECT:
1227		return (linux_connect(td, arg));
1228	case LINUX_LISTEN:
1229		return (linux_listen(td, arg));
1230	case LINUX_ACCEPT:
1231		return (linux_accept(td, arg));
1232	case LINUX_GETSOCKNAME:
1233		return (linux_getsockname(td, arg));
1234	case LINUX_GETPEERNAME:
1235		return (linux_getpeername(td, arg));
1236	case LINUX_SOCKETPAIR:
1237		return (linux_socketpair(td, arg));
1238	case LINUX_SEND:
1239		return (linux_send(td, arg));
1240	case LINUX_RECV:
1241		return (linux_recv(td, arg));
1242	case LINUX_SENDTO:
1243		return (linux_sendto(td, arg));
1244	case LINUX_RECVFROM:
1245		return (linux_recvfrom(td, arg));
1246	case LINUX_SHUTDOWN:
1247		return (linux_shutdown(td, arg));
1248	case LINUX_SETSOCKOPT:
1249		return (linux_setsockopt(td, arg));
1250	case LINUX_GETSOCKOPT:
1251		return (linux_getsockopt(td, arg));
1252	case LINUX_SENDMSG:
1253		return (linux_sendmsg(td, arg));
1254	case LINUX_RECVMSG:
1255		return (linux_recvmsg(td, arg));
1256	}
1257
1258	uprintf("LINUX: 'socket' typ=%d not implemented\n", args->what);
1259	return (ENOSYS);
1260}
1261#endif	/*!__alpha__*/
1262