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