1 /* $NetBSD: darwin_socket.c,v 1.19.2.2 2008/11/01 21:22:25 christos Exp $ */
4 * Copyright (c) 2004, 2008 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: darwin_socket.c,v 1.19.2.2 2008/11/01 21:22:25 christos Exp $");
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/socket.h>
38 #include <sys/signal.h>
40 #include <sys/socketvar.h>
43 #include <sys/filedesc.h>
44 #include <sys/protosw.h>
45 #include <sys/syscallargs.h>
47 #include <compat/sys/signal.h>
49 #include <compat/common/compat_util.h>
51 #include <compat/mach/mach_vm.h>
53 #include <compat/darwin/darwin_types.h>
54 #include <compat/darwin/darwin_audit.h>
55 #include <compat/darwin/darwin_socket.h>
56 #include <compat/darwin/darwin_syscallargs.h>
58 unsigned char native_to_darwin_af
[] = {
64 DARWIN_AF_CHAOS
, /* 5 */
69 DARWIN_AF_CCITT
, /* 10 */
74 DARWIN_AF_HYLINK
, /* 15 */
79 DARWIN_AF_COIP
, /* 20 */
84 DARWIN_AF_PIP
, /* 25 */
89 DARWIN_AF_HDRCMPLT
, /* 30 */
98 unsigned char darwin_to_native_af
[] = {
134 pseudo_AF_HDRCMPLT
, /* 35 */
139 native_to_darwin_sockaddr(struct mbuf
*nam
)
141 struct sockaddr
*sa
= mtod(nam
, void *);
143 /* We only need to translate the address family */
144 if ((unsigned)sa
->sa_family
>= __arraycount(native_to_darwin_af
))
145 return EPROTONOSUPPORT
;
147 sa
->sa_family
= native_to_darwin_af
[sa
->sa_family
];
152 darwin_to_native_sockaddr(struct mbuf
*nam
)
154 struct sockaddr
*sa
= mtod(nam
, void *);
156 if ((unsigned)sa
->sa_family
>= __arraycount(darwin_to_native_af
)) {
158 return EPROTONOSUPPORT
;
160 sa
->sa_family
= darwin_to_native_af
[sa
->sa_family
];
163 * sa_len is zero for AF_LOCAL sockets, believe size we copied in!
164 * The code used to strlen the filename, but that way lies madness!
167 if (sa
->sa_len
> nam
->m_len
|| sa
->sa_len
== 0)
168 sa
->sa_len
= nam
->m_len
;
170 nam
->m_len
= sa
->sa_len
;
176 darwin_sys_socket(struct lwp
*l
, const struct darwin_sys_socket_args
*uap
, register_t
*retval
)
179 syscallarg(int) domain;
180 syscallarg(int) type;
181 syscallarg(int) protocol;
183 struct compat_30_sys_socket_args cup
;
185 if ((unsigned)SCARG(uap
, domain
) >= __arraycount(darwin_to_native_af
))
186 return (EPROTONOSUPPORT
);
188 SCARG(&cup
, domain
) = darwin_to_native_af
[SCARG(uap
, domain
)];
189 SCARG(&cup
, type
) = SCARG(uap
, type
);
190 SCARG(&cup
, protocol
) = SCARG(uap
, protocol
);
192 return compat_30_sys_socket(l
, &cup
, retval
);
196 darwin_sys_recvfrom(struct lwp
*l
, const struct darwin_sys_recvfrom_args
*uap
, register_t
*retval
)
200 syscallarg(void *) buf;
201 syscallarg(size_t) len;
202 syscallarg(int) flags;
203 syscallarg(struct sockaddr *) from;
204 syscallarg(unsigned int *) fromlenaddr;
211 msg
.msg_name
= NULL
;;
214 aiov
.iov_base
= SCARG(uap
, buf
);
215 aiov
.iov_len
= SCARG(uap
, len
);
216 msg
.msg_control
= NULL
;
217 msg
.msg_flags
= SCARG(uap
, flags
) & MSG_USERFLAGS
;
219 error
= do_sys_recvmsg(l
, SCARG(uap
, s
), &msg
, &from
, NULL
, retval
);
223 error
= native_to_darwin_sockaddr(from
);
225 error
= copyout_sockname(SCARG(uap
, from
),
226 SCARG(uap
, fromlenaddr
), MSG_LENUSRSPACE
, from
);
233 darwin_sys_accept(struct lwp
*l
, const struct darwin_sys_accept_args
*uap
, register_t
*retval
)
237 syscallarg(struct sockaddr *) name;
238 syscallarg(unsigned int *) anamelen;
243 error
= do_sys_accept(l
, SCARG(uap
, s
), &name
, retval
);
247 error
= native_to_darwin_sockaddr(name
);
249 error
= copyout_sockname(SCARG(uap
, name
), SCARG(uap
, anamelen
),
250 MSG_LENUSRSPACE
, name
);
255 if (fd_getfile(fd
) != NULL
)
262 darwin_sys_getpeername(struct lwp
*l
, const struct darwin_sys_getpeername_args
*uap
, register_t
*retval
)
265 syscallarg(int) fdes;
266 syscallarg(struct sockaddr *) asa;
267 syscallarg(unsigned int *) alen;
272 error
= do_sys_getsockname(l
, SCARG(uap
, fdes
), PRU_PEERADDR
, &m
);
276 error
= native_to_darwin_sockaddr(m
);
278 error
= copyout_sockname(SCARG(uap
, asa
), SCARG(uap
, alen
),
286 darwin_sys_getsockname(struct lwp
*l
, const struct darwin_sys_getsockname_args
*uap
, register_t
*retval
)
289 syscallarg(int) fdes;
290 syscallarg(struct sockaddr *) asa;
291 syscallarg(unsigned int *) alen;
296 error
= do_sys_getsockname(l
, SCARG(uap
, fdes
), PRU_SOCKADDR
, &m
);
300 error
= native_to_darwin_sockaddr(m
);
302 error
= copyout_sockname(SCARG(uap
, asa
), SCARG(uap
, alen
),
310 darwin_sys_connect(struct lwp
*l
, const struct darwin_sys_connect_args
*uap
, register_t
*retval
)
314 syscallarg(struct sockaddr *) name;
315 syscallarg(unsigned int *) namelen;
320 error
= sockargs(&nam
, SCARG(uap
, name
), SCARG(uap
, namelen
),
323 error
= darwin_to_native_sockaddr(nam
);
325 error
= do_sys_connect(l
, SCARG(uap
, s
), nam
);
331 darwin_sys_bind(struct lwp
*l
, const struct darwin_sys_bind_args
*uap
, register_t
*retval
)
335 syscallarg(struct sockaddr *) name;
336 syscallarg(unsigned int *) namelen;
341 error
= sockargs(&nam
, SCARG(uap
, name
), SCARG(uap
, namelen
),
344 error
= darwin_to_native_sockaddr(nam
);
346 error
= do_sys_bind(l
, SCARG(uap
, s
), nam
);
352 darwin_sys_sendto(struct lwp
*l
, const struct darwin_sys_sendto_args
*uap
, register_t
*retval
)
356 syscallarg(const void *) buf;
357 syscallarg(size_t) len;
358 syscallarg(int) flags;
359 syscallarg(struct sockaddr *) to;
360 syscallarg(unsigned int) tolen;
368 error
= sockargs(&nam
, SCARG(uap
, to
), SCARG(uap
, tolen
), MT_SONAME
);
371 error
= darwin_to_native_sockaddr(nam
);
380 msg
.msg_flags
= MSG_NAMEMBUF
;
381 aiov
.iov_base
= __UNCONST(SCARG(uap
, buf
)); /* XXXUNCONST kills const */
382 aiov
.iov_len
= SCARG(uap
, len
);
383 return do_sys_sendmsg(l
, SCARG(uap
, s
), &msg
, SCARG(uap
, flags
), retval
);