xref: /freebsd/sys/compat/linux/linux_socket.c (revision d056fa046c6a91b90cd98165face0e42a33a5173)
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$");
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 
71 static int do_sa_get(struct sockaddr **, const struct osockaddr *, int *,
72     struct malloc_type *);
73 static 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  */
79 static int
80 linux_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  */
91 static int
92 do_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 
168 out:
169 	FREE(kosa, mtype);
170 	return (error);
171 }
172 
173 static int
174 linux_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 static int
197 bsd_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 
219 static int
220 linux_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 
230 static int
231 bsd_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 
241 static int
242 linux_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 
268 static int
269 linux_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 
299 static int
300 linux_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 
345 static int
346 bsd_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 
362 static int
363 linux_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 
381 static int
382 linux_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 
407 static int
408 linux_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 
443 bad:
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. */
450 static int
451 linux_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 
464 struct 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  */
477 static int
478 linux_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);
519 goout:
520 	free(packet, M_TEMP);
521 	return (error);
522 }
523 
524 struct linux_socket_args {
525 	int domain;
526 	int type;
527 	int protocol;
528 };
529 
530 static int
531 linux_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 
591 struct linux_bind_args {
592 	int s;
593 	l_uintptr_t name;
594 	int namelen;
595 };
596 
597 static int
598 linux_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 
615 struct linux_connect_args {
616 	int s;
617 	l_uintptr_t name;
618 	int namelen;
619 };
620 int linux_connect(struct thread *, struct linux_connect_args *);
621 
622 int
623 linux_connect(struct thread *td, struct linux_connect_args *args)
624 {
625 	struct linux_connect_args linux_args;
626 	struct socket *so;
627 	struct sockaddr *sa;
628 	u_int fflag;
629 	int error;
630 
631 	if ((error = copyin(args, &linux_args, sizeof(linux_args))))
632 		return (error);
633 
634 	error = linux_getsockaddr(&sa,
635 	    (struct osockaddr *)PTRIN(linux_args.name),
636 	    linux_args.namelen);
637 	if (error)
638 		return (error);
639 
640 	error = kern_connect(td, linux_args.s, sa);
641 	if (error != EISCONN)
642 		return (error);
643 
644 	/*
645 	 * Linux doesn't return EISCONN the first time it occurs,
646 	 * when on a non-blocking socket. Instead it returns the
647 	 * error getsockopt(SOL_SOCKET, SO_ERROR) would return on BSD.
648 	 *
649 	 * XXXRW: Instead of using fgetsock(), check that it is a
650 	 * socket and use the file descriptor reference instead of
651 	 * creating a new one.
652 	 */
653 	NET_LOCK_GIANT();
654 	error = fgetsock(td, linux_args.s, &so, &fflag);
655 	if (error == 0) {
656 		error = EISCONN;
657 		if (fflag & FNONBLOCK) {
658 			SOCK_LOCK(so);
659 			if (so->so_emuldata == 0)
660 				error = so->so_error;
661 			so->so_emuldata = (void *)1;
662 			SOCK_UNLOCK(so);
663 		}
664 		fputsock(so);
665 	}
666 	NET_UNLOCK_GIANT();
667 	return (error);
668 }
669 
670 struct linux_listen_args {
671 	int s;
672 	int backlog;
673 };
674 
675 static int
676 linux_listen(struct thread *td, struct linux_listen_args *args)
677 {
678 	struct linux_listen_args linux_args;
679 	struct listen_args /* {
680 		int s;
681 		int backlog;
682 	} */ bsd_args;
683 	int error;
684 
685 	if ((error = copyin(args, &linux_args, sizeof(linux_args))))
686 		return (error);
687 
688 	bsd_args.s = linux_args.s;
689 	bsd_args.backlog = linux_args.backlog;
690 	return (listen(td, &bsd_args));
691 }
692 
693 struct linux_accept_args {
694 	int s;
695 	l_uintptr_t addr;
696 	l_uintptr_t namelen;
697 };
698 
699 static int
700 linux_accept(struct thread *td, struct linux_accept_args *args)
701 {
702 	struct linux_accept_args linux_args;
703 	struct accept_args /* {
704 		int	s;
705 		struct sockaddr * __restrict name;
706 		socklen_t * __restrict anamelen;
707 	} */ bsd_args;
708 	int error, fd;
709 
710 	if ((error = copyin(args, &linux_args, sizeof(linux_args))))
711 		return (error);
712 
713 	bsd_args.s = linux_args.s;
714 	/* XXX: */
715 	bsd_args.name = (struct sockaddr * __restrict)PTRIN(linux_args.addr);
716 	bsd_args.anamelen = PTRIN(linux_args.namelen);/* XXX */
717 	error = accept(td, &bsd_args);
718 	bsd_to_linux_sockaddr((struct sockaddr *)bsd_args.name);
719 	if (error)
720 		return (error);
721 	if (linux_args.addr) {
722 		error = linux_sa_put(PTRIN(linux_args.addr));
723 		if (error) {
724 			(void)kern_close(td, td->td_retval[0]);
725 			return (error);
726 		}
727 	}
728 
729 	/*
730 	 * linux appears not to copy flags from the parent socket to the
731 	 * accepted one, so we must clear the flags in the new descriptor.
732 	 * Ignore any errors, because we already have an open fd.
733 	 */
734 	fd = td->td_retval[0];
735 	(void)kern_fcntl(td, fd, F_SETFL, 0);
736 	td->td_retval[0] = fd;
737 	return (0);
738 }
739 
740 struct linux_getsockname_args {
741 	int s;
742 	l_uintptr_t addr;
743 	l_uintptr_t namelen;
744 };
745 
746 static int
747 linux_getsockname(struct thread *td, struct linux_getsockname_args *args)
748 {
749 	struct linux_getsockname_args linux_args;
750 	struct getsockname_args /* {
751 		int	fdes;
752 		struct sockaddr * __restrict asa;
753 		socklen_t * __restrict alen;
754 	} */ bsd_args;
755 	int error;
756 
757 	if ((error = copyin(args, &linux_args, sizeof(linux_args))))
758 		return (error);
759 
760 	bsd_args.fdes = linux_args.s;
761 	/* XXX: */
762 	bsd_args.asa = (struct sockaddr * __restrict)PTRIN(linux_args.addr);
763 	bsd_args.alen = PTRIN(linux_args.namelen);	/* XXX */
764 	error = getsockname(td, &bsd_args);
765 	bsd_to_linux_sockaddr((struct sockaddr *)bsd_args.asa);
766 	if (error)
767 		return (error);
768 	error = linux_sa_put(PTRIN(linux_args.addr));
769 	if (error)
770 		return (error);
771 	return (0);
772 }
773 
774 struct linux_getpeername_args {
775 	int s;
776 	l_uintptr_t addr;
777 	l_uintptr_t namelen;
778 };
779 
780 static int
781 linux_getpeername(struct thread *td, struct linux_getpeername_args *args)
782 {
783 	struct linux_getpeername_args linux_args;
784 	struct getpeername_args /* {
785 		int fdes;
786 		caddr_t asa;
787 		int *alen;
788 	} */ bsd_args;
789 	int error;
790 
791 	if ((error = copyin(args, &linux_args, sizeof(linux_args))))
792 		return (error);
793 
794 	bsd_args.fdes = linux_args.s;
795 	bsd_args.asa = (struct sockaddr *)PTRIN(linux_args.addr);
796 	bsd_args.alen = (int *)PTRIN(linux_args.namelen);
797 	error = getpeername(td, &bsd_args);
798 	bsd_to_linux_sockaddr((struct sockaddr *)bsd_args.asa);
799 	if (error)
800 		return (error);
801 	error = linux_sa_put(PTRIN(linux_args.addr));
802 	if (error)
803 		return (error);
804 	return (0);
805 }
806 
807 struct linux_socketpair_args {
808 	int domain;
809 	int type;
810 	int protocol;
811 	l_uintptr_t rsv;
812 };
813 
814 static int
815 linux_socketpair(struct thread *td, struct linux_socketpair_args *args)
816 {
817 	struct linux_socketpair_args linux_args;
818 	struct socketpair_args /* {
819 		int domain;
820 		int type;
821 		int protocol;
822 		int *rsv;
823 	} */ bsd_args;
824 	int error;
825 
826 	if ((error = copyin(args, &linux_args, sizeof(linux_args))))
827 		return (error);
828 
829 	bsd_args.domain = linux_to_bsd_domain(linux_args.domain);
830 	if (bsd_args.domain == -1)
831 		return (EINVAL);
832 
833 	bsd_args.type = linux_args.type;
834 	bsd_args.protocol = linux_args.protocol;
835 	bsd_args.rsv = (int *)PTRIN(linux_args.rsv);
836 	return (socketpair(td, &bsd_args));
837 }
838 
839 struct linux_send_args {
840 	int s;
841 	l_uintptr_t msg;
842 	int len;
843 	int flags;
844 };
845 
846 static int
847 linux_send(struct thread *td, struct linux_send_args *args)
848 {
849 	struct linux_send_args linux_args;
850 	struct sendto_args /* {
851 		int s;
852 		caddr_t buf;
853 		int len;
854 		int flags;
855 		caddr_t to;
856 		int tolen;
857 	} */ bsd_args;
858 	int error;
859 
860 	if ((error = copyin(args, &linux_args, sizeof(linux_args))))
861 		return (error);
862 
863 	bsd_args.s = linux_args.s;
864 	bsd_args.buf = (caddr_t)PTRIN(linux_args.msg);
865 	bsd_args.len = linux_args.len;
866 	bsd_args.flags = linux_args.flags;
867 	bsd_args.to = NULL;
868 	bsd_args.tolen = 0;
869 	return sendto(td, &bsd_args);
870 }
871 
872 struct linux_recv_args {
873 	int s;
874 	l_uintptr_t msg;
875 	int len;
876 	int flags;
877 };
878 
879 static int
880 linux_recv(struct thread *td, struct linux_recv_args *args)
881 {
882 	struct linux_recv_args linux_args;
883 	struct recvfrom_args /* {
884 		int s;
885 		caddr_t buf;
886 		int len;
887 		int flags;
888 		struct sockaddr *from;
889 		socklen_t fromlenaddr;
890 	} */ bsd_args;
891 	int error;
892 
893 	if ((error = copyin(args, &linux_args, sizeof(linux_args))))
894 		return (error);
895 
896 	bsd_args.s = linux_args.s;
897 	bsd_args.buf = (caddr_t)PTRIN(linux_args.msg);
898 	bsd_args.len = linux_args.len;
899 	bsd_args.flags = linux_args.flags;
900 	bsd_args.from = NULL;
901 	bsd_args.fromlenaddr = 0;
902 	return (recvfrom(td, &bsd_args));
903 }
904 
905 static int
906 linux_sendto(struct thread *td, struct linux_sendto_args *args)
907 {
908 	struct linux_sendto_args linux_args;
909 	struct msghdr msg;
910 	struct iovec aiov;
911 	int error;
912 
913 	if ((error = copyin(args, &linux_args, sizeof(linux_args))))
914 		return (error);
915 
916 	if (linux_check_hdrincl(td, linux_args.s) == 0)
917 		/* IP_HDRINCL set, tweak the packet before sending */
918 		return (linux_sendto_hdrincl(td, &linux_args));
919 
920 	msg.msg_name = PTRIN(linux_args.to);
921 	msg.msg_namelen = linux_args.tolen;
922 	msg.msg_iov = &aiov;
923 	msg.msg_iovlen = 1;
924 	msg.msg_control = NULL;
925 	msg.msg_flags = 0;
926 	aiov.iov_base = PTRIN(linux_args.msg);
927 	aiov.iov_len = linux_args.len;
928 	error = linux_sendit(td, linux_args.s, &msg, linux_args.flags,
929 	    UIO_USERSPACE);
930 	return (error);
931 }
932 
933 struct linux_recvfrom_args {
934 	int s;
935 	l_uintptr_t buf;
936 	int len;
937 	int flags;
938 	l_uintptr_t from;
939 	l_uintptr_t fromlen;
940 };
941 
942 static int
943 linux_recvfrom(struct thread *td, struct linux_recvfrom_args *args)
944 {
945 	struct linux_recvfrom_args linux_args;
946 	struct recvfrom_args /* {
947 		int	s;
948 		caddr_t	buf;
949 		size_t	len;
950 		int	flags;
951 		struct sockaddr * __restrict from;
952 		socklen_t * __restrict fromlenaddr;
953 	} */ bsd_args;
954 	size_t len;
955 	int error;
956 
957 	if ((error = copyin(args, &linux_args, sizeof(linux_args))))
958 		return (error);
959 
960 	if ((error = copyin(PTRIN(linux_args.fromlen), &len, sizeof(size_t))))
961 		return (error);
962 
963 	bsd_args.s = linux_args.s;
964 	bsd_args.buf = PTRIN(linux_args.buf);
965 	bsd_args.len = linux_args.len;
966 	bsd_args.flags = linux_to_bsd_msg_flags(linux_args.flags);
967 	/* XXX: */
968 	bsd_args.from = (struct sockaddr * __restrict)PTRIN(linux_args.from);
969 	bsd_args.fromlenaddr = PTRIN(linux_args.fromlen);/* XXX */
970 
971 	linux_to_bsd_sockaddr((struct sockaddr *)bsd_args.from, len);
972 	error = recvfrom(td, &bsd_args);
973 	bsd_to_linux_sockaddr((struct sockaddr *)bsd_args.from);
974 
975 	if (error)
976 		return (error);
977 	if (linux_args.from) {
978 		error = linux_sa_put((struct osockaddr *)
979 		    PTRIN(linux_args.from));
980 		if (error)
981 			return (error);
982 	}
983 	return (0);
984 }
985 
986 struct linux_sendmsg_args {
987 	int s;
988 	l_uintptr_t msg;
989 	int flags;
990 };
991 
992 static int
993 linux_sendmsg(struct thread *td, struct linux_sendmsg_args *args)
994 {
995 	struct linux_sendmsg_args linux_args;
996 	struct msghdr msg;
997 	struct iovec *iov;
998 	int error;
999 
1000 	/* XXXTJR sendmsg is broken on amd64 */
1001 
1002 	error = copyin(args, &linux_args, sizeof(linux_args));
1003 	if (error)
1004 		return (error);
1005 	error = copyin(PTRIN(linux_args.msg), &msg, sizeof(msg));
1006 	if (error)
1007 		return (error);
1008 	error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
1009 	if (error)
1010 		return (error);
1011 	msg.msg_iov = iov;
1012 	msg.msg_flags = 0;
1013 	error = linux_sendit(td, linux_args.s, &msg, linux_args.flags,
1014 	    UIO_USERSPACE);
1015 	free(iov, M_IOV);
1016 	return (error);
1017 }
1018 
1019 struct linux_recvmsg_args {
1020 	int s;
1021 	l_uintptr_t msg;
1022 	int flags;
1023 };
1024 
1025 static int
1026 linux_recvmsg(struct thread *td, struct linux_recvmsg_args *args)
1027 {
1028 	struct linux_recvmsg_args linux_args;
1029 	struct recvmsg_args /* {
1030 		int	s;
1031 		struct	msghdr *msg;
1032 		int	flags;
1033 	} */ bsd_args;
1034 	struct msghdr msg;
1035 	struct cmsghdr *cmsg;
1036 	int error;
1037 
1038 	/* XXXTJR recvmsg is broken on amd64 */
1039 
1040 	if ((error = copyin(args, &linux_args, sizeof(linux_args))))
1041 		return (error);
1042 
1043 	bsd_args.s = linux_args.s;
1044 	bsd_args.msg = PTRIN(linux_args.msg);
1045 	bsd_args.flags = linux_to_bsd_msg_flags(linux_args.flags);
1046 	if (msg.msg_name) {
1047 	   	linux_to_bsd_sockaddr((struct sockaddr *)msg.msg_name,
1048 		      msg.msg_namelen);
1049 		error = recvmsg(td, &bsd_args);
1050 		bsd_to_linux_sockaddr((struct sockaddr *)msg.msg_name);
1051 	} else
1052 	   	error = recvmsg(td, &bsd_args);
1053 	if (error)
1054 		return (error);
1055 
1056 	if (bsd_args.msg->msg_control != NULL &&
1057 	    bsd_args.msg->msg_controllen > 0) {
1058 		cmsg = (struct cmsghdr*)bsd_args.msg->msg_control;
1059 		cmsg->cmsg_level = bsd_to_linux_sockopt_level(cmsg->cmsg_level);
1060 	}
1061 
1062 	error = copyin(PTRIN(linux_args.msg), &msg, sizeof(msg));
1063 	if (error)
1064 		return (error);
1065 	if (msg.msg_name && msg.msg_namelen > 2)
1066 		error = linux_sa_put(msg.msg_name);
1067 	return (error);
1068 }
1069 
1070 struct linux_shutdown_args {
1071 	int s;
1072 	int how;
1073 };
1074 
1075 static int
1076 linux_shutdown(struct thread *td, struct linux_shutdown_args *args)
1077 {
1078 	struct linux_shutdown_args linux_args;
1079 	struct shutdown_args /* {
1080 		int s;
1081 		int how;
1082 	} */ bsd_args;
1083 	int error;
1084 
1085 	if ((error = copyin(args, &linux_args, sizeof(linux_args))))
1086 		return (error);
1087 
1088 	bsd_args.s = linux_args.s;
1089 	bsd_args.how = linux_args.how;
1090 	return (shutdown(td, &bsd_args));
1091 }
1092 
1093 struct linux_setsockopt_args {
1094 	int s;
1095 	int level;
1096 	int optname;
1097 	l_uintptr_t optval;
1098 	int optlen;
1099 };
1100 
1101 static int
1102 linux_setsockopt(struct thread *td, struct linux_setsockopt_args *args)
1103 {
1104 	struct linux_setsockopt_args linux_args;
1105 	struct setsockopt_args /* {
1106 		int s;
1107 		int level;
1108 		int name;
1109 		caddr_t val;
1110 		int valsize;
1111 	} */ bsd_args;
1112 	int error, name;
1113 
1114 	if ((error = copyin(args, &linux_args, sizeof(linux_args))))
1115 		return (error);
1116 
1117 	bsd_args.s = linux_args.s;
1118 	bsd_args.level = linux_to_bsd_sockopt_level(linux_args.level);
1119 	switch (bsd_args.level) {
1120 	case SOL_SOCKET:
1121 		name = linux_to_bsd_so_sockopt(linux_args.optname);
1122 		break;
1123 	case IPPROTO_IP:
1124 		name = linux_to_bsd_ip_sockopt(linux_args.optname);
1125 		break;
1126 	case IPPROTO_TCP:
1127 		/* Linux TCP option values match BSD's */
1128 		name = linux_args.optname;
1129 		break;
1130 	default:
1131 		name = -1;
1132 		break;
1133 	}
1134 	if (name == -1)
1135 		return (EINVAL);
1136 
1137 	bsd_args.name = name;
1138 	bsd_args.val = PTRIN(linux_args.optval);
1139 	bsd_args.valsize = linux_args.optlen;
1140 
1141 	if (name == IPV6_NEXTHOP) {
1142 		linux_to_bsd_sockaddr((struct sockaddr *)bsd_args.val,
1143 			bsd_args.valsize);
1144 		error = setsockopt(td, &bsd_args);
1145 		bsd_to_linux_sockaddr((struct sockaddr *)bsd_args.val);
1146 	} else
1147 		error = setsockopt(td, &bsd_args);
1148 
1149 	return (error);
1150 }
1151 
1152 struct linux_getsockopt_args {
1153 	int s;
1154 	int level;
1155 	int optname;
1156 	l_uintptr_t optval;
1157 	l_uintptr_t optlen;
1158 };
1159 
1160 static int
1161 linux_getsockopt(struct thread *td, struct linux_getsockopt_args *args)
1162 {
1163 	struct linux_getsockopt_args linux_args;
1164 	struct getsockopt_args /* {
1165 		int s;
1166 		int level;
1167 		int name;
1168 		caddr_t val;
1169 		int *avalsize;
1170 	} */ bsd_args;
1171 	int error, name;
1172 
1173 	if ((error = copyin(args, &linux_args, sizeof(linux_args))))
1174 		return (error);
1175 
1176 	bsd_args.s = linux_args.s;
1177 	bsd_args.level = linux_to_bsd_sockopt_level(linux_args.level);
1178 	switch (bsd_args.level) {
1179 	case SOL_SOCKET:
1180 		name = linux_to_bsd_so_sockopt(linux_args.optname);
1181 		break;
1182 	case IPPROTO_IP:
1183 		name = linux_to_bsd_ip_sockopt(linux_args.optname);
1184 		break;
1185 	case IPPROTO_TCP:
1186 		/* Linux TCP option values match BSD's */
1187 		name = linux_args.optname;
1188 		break;
1189 	default:
1190 		name = -1;
1191 		break;
1192 	}
1193 	if (name == -1)
1194 		return (EINVAL);
1195 
1196 	bsd_args.name = name;
1197 	bsd_args.val = PTRIN(linux_args.optval);
1198 	bsd_args.avalsize = PTRIN(linux_args.optlen);
1199 
1200 	if (name == IPV6_NEXTHOP) {
1201 		error = getsockopt(td, &bsd_args);
1202 		bsd_to_linux_sockaddr((struct sockaddr *)bsd_args.val);
1203 	} else
1204 		error = getsockopt(td, &bsd_args);
1205 
1206 	return (error);
1207 }
1208 
1209 int
1210 linux_socketcall(struct thread *td, struct linux_socketcall_args *args)
1211 {
1212 	void *arg = (void *)(intptr_t)args->args;
1213 
1214 	switch (args->what) {
1215 	case LINUX_SOCKET:
1216 		return (linux_socket(td, arg));
1217 	case LINUX_BIND:
1218 		return (linux_bind(td, arg));
1219 	case LINUX_CONNECT:
1220 		return (linux_connect(td, arg));
1221 	case LINUX_LISTEN:
1222 		return (linux_listen(td, arg));
1223 	case LINUX_ACCEPT:
1224 		return (linux_accept(td, arg));
1225 	case LINUX_GETSOCKNAME:
1226 		return (linux_getsockname(td, arg));
1227 	case LINUX_GETPEERNAME:
1228 		return (linux_getpeername(td, arg));
1229 	case LINUX_SOCKETPAIR:
1230 		return (linux_socketpair(td, arg));
1231 	case LINUX_SEND:
1232 		return (linux_send(td, arg));
1233 	case LINUX_RECV:
1234 		return (linux_recv(td, arg));
1235 	case LINUX_SENDTO:
1236 		return (linux_sendto(td, arg));
1237 	case LINUX_RECVFROM:
1238 		return (linux_recvfrom(td, arg));
1239 	case LINUX_SHUTDOWN:
1240 		return (linux_shutdown(td, arg));
1241 	case LINUX_SETSOCKOPT:
1242 		return (linux_setsockopt(td, arg));
1243 	case LINUX_GETSOCKOPT:
1244 		return (linux_getsockopt(td, arg));
1245 	case LINUX_SENDMSG:
1246 		return (linux_sendmsg(td, arg));
1247 	case LINUX_RECVMSG:
1248 		return (linux_recvmsg(td, arg));
1249 	}
1250 
1251 	uprintf("LINUX: 'socket' typ=%d not implemented\n", args->what);
1252 	return (ENOSYS);
1253 }
1254