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