1 /* $NetBSD: osf1_socket.c,v 1.21 2015/08/08 12:02:35 maxv Exp $ */
2 
3 /*
4  * Copyright (c) 1999 Christopher G. Demetriou.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
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. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *      This product includes software developed by Christopher G. Demetriou
17  *	for the NetBSD Project.
18  * 4. The name of the author may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 /*
34  * Copyright (c) 1994, 1995 Carnegie-Mellon University.
35  * All rights reserved.
36  *
37  * Author: Chris G. Demetriou
38  *
39  * Permission to use, copy, modify and distribute this software and
40  * its documentation is hereby granted, provided that both the copyright
41  * notice and this permission notice appear in all copies of the
42  * software, derivative works or modified versions, and any portions
43  * thereof, and that both notices appear in supporting documentation.
44  *
45  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
46  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
47  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
48  *
49  * Carnegie Mellon requests users of this software to return to
50  *
51  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
52  *  School of Computer Science
53  *  Carnegie Mellon University
54  *  Pittsburgh PA 15213-3890
55  *
56  * any improvements or extensions that they make and grant Carnegie the
57  * rights to redistribute these changes.
58  */
59 
60 #include <sys/cdefs.h>
61 __KERNEL_RCSID(0, "$NetBSD: osf1_socket.c,v 1.21 2015/08/08 12:02:35 maxv Exp $");
62 
63 #include <sys/param.h>
64 #include <sys/systm.h>
65 #include <sys/namei.h>
66 #include <sys/proc.h>
67 #include <sys/file.h>
68 #include <sys/mount.h>
69 #include <sys/syscallargs.h>
70 #include <sys/socket.h>
71 #include <sys/socketvar.h>
72 #include <sys/exec.h>
73 
74 #include <compat/osf1/osf1.h>
75 #include <compat/osf1/osf1_syscallargs.h>
76 #include <compat/common/compat_util.h>
77 #include <compat/osf1/osf1_cvt.h>
78 
79 int
osf1_sys_recvmsg_xopen(struct lwp * l,const struct osf1_sys_recvmsg_xopen_args * uap,register_t * retval)80 osf1_sys_recvmsg_xopen(struct lwp *l, const struct osf1_sys_recvmsg_xopen_args *uap, register_t *retval)
81 {
82 
83 	/* XXX */
84 	return (EINVAL);
85 }
86 
87 int
osf1_sys_sendmsg_xopen(struct lwp * l,const struct osf1_sys_sendmsg_xopen_args * uap,register_t * retval)88 osf1_sys_sendmsg_xopen(struct lwp *l, const struct osf1_sys_sendmsg_xopen_args *uap, register_t *retval)
89 {
90 	struct osf1_msghdr_xopen osf_msghdr;
91 	struct osf1_iovec_xopen osf_iovec, *osf_iovec_ptr;
92 	struct msghdr bsd_msghdr;
93 	struct iovec *bsd_iovec;
94 	unsigned long leftovers;
95 	int flags;
96 	unsigned int i, iov_len;
97 	int error;
98 
99 	/*
100 	 * translate msghdr structure
101 	 */
102 	if ((error = copyin(SCARG(uap, msg), &osf_msghdr,
103 	    sizeof osf_msghdr)) != 0)
104 		return (error);
105 
106 	error = osf1_cvt_msghdr_xopen_to_native(&osf_msghdr, &bsd_msghdr);
107 	if (error != 0)
108 		return (error);
109 
110 	/*
111 	 * translate flags
112 	 */
113 	flags = emul_flags_translate(osf1_sendrecv_msg_flags_xtab,
114 	    SCARG(uap, flags), &leftovers);
115 	if (leftovers != 0)
116 		return (EINVAL);
117 
118 	iov_len = bsd_msghdr.msg_iovlen;
119 	if ((iov_len > IOV_MAX) || (iov_len == 0))
120 		return EMSGSIZE;
121 	bsd_iovec = kmem_alloc(iov_len * sizeof(struct iovec), KM_SLEEP);
122 	bsd_msghdr.msg_iov = bsd_iovec;
123 
124 	osf_iovec_ptr = osf_msghdr.msg_iov;
125 	for (i = 0; i < iov_len; i++) {
126 		error = copyin(&osf_iovec_ptr[i], &osf_iovec, sizeof osf_iovec);
127 		if (error != 0)
128 			goto err;
129                 bsd_iovec[i].iov_base = osf_iovec.iov_base;
130                 bsd_iovec[i].iov_len = osf_iovec.iov_len;
131 	}
132 
133 	error = do_sys_sendmsg(l, SCARG(uap, s), &bsd_msghdr, flags, retval);
134 err:
135 	kmem_free(bsd_iovec, iov_len * sizeof(struct iovec));
136 	return error;
137 }
138 
139 int
osf1_sys_sendto(struct lwp * l,const struct osf1_sys_sendto_args * uap,register_t * retval)140 osf1_sys_sendto(struct lwp *l, const struct osf1_sys_sendto_args *uap, register_t *retval)
141 {
142 	struct sys_sendto_args a;
143 	unsigned long leftovers;
144 
145 	SCARG(&a, s) = SCARG(uap, s);
146 	SCARG(&a, buf) = SCARG(uap, buf);
147 	SCARG(&a, len) = SCARG(uap, len);
148 	SCARG(&a, to) = SCARG(uap, to);
149 	SCARG(&a, tolen) = SCARG(uap, tolen);
150 
151 	/* translate flags */
152 	SCARG(&a, flags) = emul_flags_translate(osf1_sendrecv_msg_flags_xtab,
153 	    SCARG(uap, flags), &leftovers);
154 	if (leftovers != 0)
155 		return (EINVAL);
156 
157 	return sys_sendto(l, &a, retval);
158 }
159 
160 int
osf1_sys_socket(struct lwp * l,const struct osf1_sys_socket_args * uap,register_t * retval)161 osf1_sys_socket(struct lwp *l, const struct osf1_sys_socket_args *uap, register_t *retval)
162 {
163 	struct compat_30_sys_socket_args a;
164 
165 	/* XXX TRANSLATE */
166 
167 	if (SCARG(uap, domain) > AF_LINK)
168 		return (EINVAL);	/* XXX After AF_LINK, divergence. */
169 
170 	SCARG(&a, domain) = SCARG(uap, domain);
171 	SCARG(&a, type) = SCARG(uap, type);
172 	SCARG(&a, protocol) = SCARG(uap, protocol);
173 
174 	return compat_30_sys_socket(l, &a, retval);
175 }
176 
177 int
osf1_sys_socketpair(struct lwp * l,const struct osf1_sys_socketpair_args * uap,register_t * retval)178 osf1_sys_socketpair(struct lwp *l, const struct osf1_sys_socketpair_args *uap, register_t *retval)
179 {
180 	struct sys_socketpair_args a;
181 
182 	/* XXX TRANSLATE */
183 
184 	if (SCARG(uap, domain) > AF_LINK)
185 		return (EINVAL);	/* XXX After AF_LINK, divergence. */
186 
187 	SCARG(&a, domain) = SCARG(uap, domain);
188 	SCARG(&a, type) = SCARG(uap, type);
189 	SCARG(&a, protocol) = SCARG(uap, protocol);
190 	SCARG(&a, rsv) = SCARG(uap, rsv);
191 
192 	return sys_socketpair(l, &a, retval);
193 }
194