uipc_syscalls_43.c revision 1.22 1 /* $NetBSD: uipc_syscalls_43.c,v 1.22 2003/08/07 16:30:36 agc Exp $ */
2
3 /*
4 * Copyright (c) 1982, 1986, 1989, 1990, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 * @(#)uipc_syscalls.c 8.4 (Berkeley) 2/21/94
32 */
33
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: uipc_syscalls_43.c,v 1.22 2003/08/07 16:30:36 agc Exp $");
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/filedesc.h>
40 #include <sys/kernel.h>
41 #include <sys/proc.h>
42 #include <sys/file.h>
43 #include <sys/socket.h>
44 #include <sys/socketvar.h>
45 #include <sys/stat.h>
46 #include <sys/ioctl.h>
47 #include <sys/fcntl.h>
48 #include <sys/malloc.h>
49 #include <sys/syslog.h>
50 #include <sys/unistd.h>
51 #include <sys/resourcevar.h>
52 #include <sys/mbuf.h> /* for MLEN */
53
54 #include <sys/mount.h>
55 #include <sys/sa.h>
56 #include <sys/syscallargs.h>
57
58 #include <compat/common/compat_util.h>
59
60 #include <uvm/uvm_extern.h>
61
62 /*
63 * Following 4.3 syscalls were not versioned, even through they should
64 * have been:
65 * connect(2), bind(2), sendto(2)
66 */
67
68 static int compat_43_sa_put(caddr_t);
69
70 int
71 compat_43_sys_accept(struct lwp *l, void *v, register_t *retval)
72 {
73 struct compat_43_sys_accept_args /* {
74 syscallarg(int) s;
75 syscallarg(caddr_t) name;
76 syscallarg(int *) anamelen;
77 } */ *uap = v;
78 int error;
79
80 if ((error = sys_accept(l, v, retval)) != 0)
81 return error;
82
83 if (SCARG(uap, name)
84 && (error = compat_43_sa_put(SCARG(uap, name))))
85 return (error);
86
87 return 0;
88 }
89
90 int
91 compat_43_sys_getpeername(struct lwp *l, void *v, register_t *retval)
92 {
93 struct compat_43_sys_getpeername_args /* {
94 syscallarg(int) fdes;
95 syscallarg(caddr_t) asa;
96 syscallarg(int *) alen;
97 } */ *uap = v;
98
99 int error;
100
101 if ((error = sys_getpeername(l, v, retval)) != 0)
102 return error;
103
104 if ((error = compat_43_sa_put(SCARG(uap, asa))))
105 return (error);
106
107 return 0;
108 }
109
110 int
111 compat_43_sys_getsockname(struct lwp *l, void *v, register_t *retval)
112 {
113 struct compat_43_sys_getsockname_args /* {
114 syscallarg(int) fdes;
115 syscallarg(caddr_t) asa;
116 syscallarg(int *) alen;
117 } */ *uap = v;
118 int error;
119
120 if ((error = sys_getsockname(l, v, retval)) != 0)
121 return error;
122
123 if ((error = compat_43_sa_put(SCARG(uap, asa))))
124 return (error);
125
126 return 0;
127 }
128
129 int
130 compat_43_sys_recv(struct lwp *l, void *v, register_t *retval)
131 {
132 struct compat_43_sys_recv_args /* {
133 syscallarg(int) s;
134 syscallarg(caddr_t) buf;
135 syscallarg(int) len;
136 syscallarg(int) flags;
137 } */ *uap = v;
138 struct sys_recvfrom_args bra;
139
140 SCARG(&bra, s) = SCARG(uap, s);
141 SCARG(&bra, buf) = SCARG(uap, buf);
142 SCARG(&bra, len) = (size_t) SCARG(uap, len);
143 SCARG(&bra, flags) = SCARG(uap, flags);
144 SCARG(&bra, from) = NULL;
145 SCARG(&bra, fromlenaddr) = NULL;
146
147 return (sys_recvfrom(l, &bra, retval));
148 }
149
150 int
151 compat_43_sys_recvfrom(struct lwp *l, void *v, register_t *retval)
152 {
153 struct compat_43_sys_recvfrom_args /* {
154 syscallarg(int) s;
155 syscallarg(caddr_t) buf;
156 syscallarg(size_t) len;
157 syscallarg(int) flags;
158 syscallarg(caddr_t) from;
159 syscallarg(int *) fromlenaddr;
160 } */ *uap = v;
161 int error;
162
163 if ((error = sys_recvfrom(l, v, retval)))
164 return (error);
165
166 if (SCARG(uap, from) && (error = compat_43_sa_put(SCARG(uap, from))))
167 return (error);
168
169 return (0);
170 }
171
172 /*
173 * Old recvmsg. Arrange necessary structures, calls generic code and
174 * adjusts results accordingly.
175 */
176 int
177 compat_43_sys_recvmsg(struct lwp *l, void *v, register_t *retval)
178 {
179 struct compat_43_sys_recvmsg_args /* {
180 syscallarg(int) s;
181 syscallarg(struct omsghdr *) msg;
182 syscallarg(int) flags;
183 } */ *uap = v;
184 struct proc *p = l->l_proc;
185 struct omsghdr omsg;
186 struct msghdr msg;
187 struct iovec aiov[UIO_SMALLIOV], *iov;
188 int error;
189
190 error = copyin((caddr_t)SCARG(uap, msg), (caddr_t)&omsg,
191 sizeof (struct omsghdr));
192 if (error)
193 return (error);
194 if ((u_int)omsg.msg_iovlen > UIO_SMALLIOV) {
195 if ((u_int)omsg.msg_iovlen > IOV_MAX)
196 return (EMSGSIZE);
197 iov = malloc(sizeof(struct iovec) * omsg.msg_iovlen,
198 M_IOV, M_WAITOK);
199 } else
200 iov = aiov;
201
202 error = copyin((caddr_t)omsg.msg_iov, (caddr_t)iov,
203 (unsigned)(omsg.msg_iovlen * sizeof (struct iovec)));
204 if (error)
205 goto done;
206
207 msg.msg_name = omsg.msg_name;
208 msg.msg_namelen = omsg.msg_namelen;
209 msg.msg_iovlen = omsg.msg_iovlen;
210 msg.msg_iov = iov;
211 msg.msg_flags = SCARG(uap, flags);
212
213 /*
214 * If caller passes accrights, arrange things for generic code to
215 * DTRT.
216 */
217 if (omsg.msg_accrights && omsg.msg_accrightslen) {
218 caddr_t sg = stackgap_init(p, 0);
219 struct cmsg *ucmsg;
220
221 /* it was this way in 4.4BSD */
222 if ((u_int) omsg.msg_accrightslen > MLEN)
223 return (EINVAL);
224
225 ucmsg = stackgap_alloc(p, &sg, CMSG_SPACE(omsg.msg_accrightslen));
226 if (ucmsg == NULL)
227 return (EMSGSIZE);
228
229 msg.msg_control = ucmsg;
230 msg.msg_controllen = CMSG_SPACE(omsg.msg_accrightslen);
231 } else {
232 msg.msg_control = NULL;
233 msg.msg_controllen = 0;
234 }
235
236 error = recvit(p, SCARG(uap, s), &msg,
237 (caddr_t)&SCARG(uap, msg)->msg_namelen, retval);
238
239 /*
240 * If there is any control information and it's SCM_RIGHTS,
241 * pass it back to the program.
242 */
243 if (!error && omsg.msg_accrights && msg.msg_controllen > 0) {
244 struct cmsghdr *cmsg;
245
246 /* safe - msg.msg_controllen set by kernel */
247 cmsg = (struct cmsghdr *) malloc(msg.msg_controllen,
248 M_TEMP, M_WAITOK);
249
250 error = copyin(msg.msg_control, cmsg, msg.msg_controllen);
251 if (error) {
252 free(cmsg, M_TEMP);
253 return (error);
254 }
255
256 if (cmsg->cmsg_level != SOL_SOCKET
257 || cmsg->cmsg_type != SCM_RIGHTS
258 || copyout(CMSG_DATA(cmsg), omsg.msg_accrights,
259 cmsg->cmsg_len)) {
260 omsg.msg_accrightslen = 0;
261 }
262
263 free(cmsg, M_TEMP);
264
265 if (!error) {
266 error = copyout(&cmsg->cmsg_len,
267 &SCARG(uap, msg)->msg_accrightslen, sizeof(int));
268 }
269 }
270
271 if (!error && omsg.msg_name) {
272 int namelen;
273
274 if ((error = copyin(&SCARG(uap, msg)->msg_namelen, &namelen, sizeof(int)) == 0)
275 && namelen > 0)
276 error = compat_43_sa_put(omsg.msg_name);
277 }
278
279 done:
280 if (iov != aiov)
281 free(iov, M_IOV);
282 return (error);
283 }
284
285 int
286 compat_43_sys_send(struct lwp *l, void *v, register_t *retval)
287 {
288 struct compat_43_sys_send_args /* {
289 syscallarg(int) s;
290 syscallarg(caddr_t) buf;
291 syscallarg(int) len;
292 syscallarg(int) flags;
293 } */ *uap = v;
294 struct sys_sendto_args bsa;
295
296 SCARG(&bsa, s) = SCARG(uap, s);
297 SCARG(&bsa, buf) = SCARG(uap, buf);
298 SCARG(&bsa, len) = SCARG(uap, len);
299 SCARG(&bsa, flags) = SCARG(uap, flags);
300 SCARG(&bsa, to) = NULL;
301 SCARG(&bsa, tolen) = 0;
302
303 return (sys_sendto(l, &bsa, retval));
304 }
305
306 /*
307 * Old sendmsg. Arrange necessary structures, call generic code and
308 * adjust the results accordingly for old code.
309 */
310 int
311 compat_43_sys_sendmsg(struct lwp *l, void *v, register_t *retval)
312 {
313 struct compat_43_sys_sendmsg_args /* {
314 syscallarg(int) s;
315 syscallarg(caddr_t) msg;
316 syscallarg(int) flags;
317 } */ *uap = v;
318 struct proc *p = l->l_proc;
319 struct omsghdr omsg;
320 struct msghdr msg;
321 struct iovec aiov[UIO_SMALLIOV], *iov;
322 int error;
323 caddr_t sg = stackgap_init(p, 0);
324
325 error = copyin(SCARG(uap, msg), (caddr_t)&omsg,
326 sizeof (struct omsghdr));
327 if (error)
328 return (error);
329 if ((u_int)omsg.msg_iovlen > UIO_SMALLIOV) {
330 if ((u_int)omsg.msg_iovlen > IOV_MAX)
331 return (EMSGSIZE);
332 iov = malloc(sizeof(struct iovec) * omsg.msg_iovlen,
333 M_IOV, M_WAITOK);
334 } else
335 iov = aiov;
336 error = copyin((caddr_t)omsg.msg_iov, (caddr_t)iov,
337 (unsigned)(omsg.msg_iovlen * sizeof (struct iovec)));
338 if (error)
339 goto done;
340
341 if (omsg.msg_name) {
342 struct osockaddr *osa;
343 struct sockaddr *sa, *usa;
344
345 if ((u_int) omsg.msg_namelen > UCHAR_MAX)
346 return (EINVAL);
347
348 osa = malloc(omsg.msg_namelen, M_TEMP, M_WAITOK);
349
350 if ((error = copyin(omsg.msg_name, osa, omsg.msg_namelen))) {
351 free(osa, M_TEMP);
352 return (error);
353 }
354
355 sa = (struct sockaddr *) osa;
356 sa->sa_family = osa->sa_family;
357 sa->sa_len = omsg.msg_namelen;
358
359 usa = stackgap_alloc(p, &sg, omsg.msg_namelen);
360 if (!usa) {
361 free(osa, M_TEMP);
362 return (ENOMEM);
363 }
364
365 (void) copyout(sa, usa, omsg.msg_namelen);
366 free(osa, M_TEMP);
367
368 msg.msg_name = usa;
369 msg.msg_namelen = omsg.msg_namelen;
370 } else {
371 msg.msg_name = NULL;
372 msg.msg_namelen = 0;
373 }
374 msg.msg_iovlen = omsg.msg_iovlen;
375 msg.msg_iov = iov;
376 msg.msg_flags = 0;
377
378 if (omsg.msg_accrights && omsg.msg_accrightslen != 0) {
379 struct cmsghdr *cmsg, *ucmsg;
380
381 /* it was this way in 4.4BSD */
382 if ((u_int) omsg.msg_accrightslen > MLEN)
383 return (EINVAL);
384
385 cmsg = malloc(CMSG_SPACE(omsg.msg_accrightslen), M_TEMP,
386 M_WAITOK);
387 cmsg->cmsg_len = CMSG_SPACE(omsg.msg_accrightslen);
388 cmsg->cmsg_level = SOL_SOCKET;
389 cmsg->cmsg_type = SCM_RIGHTS;
390
391 error = copyin(omsg.msg_accrights, CMSG_DATA(cmsg),
392 omsg.msg_accrightslen);
393 if (error) {
394 free(cmsg, M_TEMP);
395 return (error);
396 }
397
398 ucmsg = stackgap_alloc(p, &sg, CMSG_SPACE(omsg.msg_accrightslen));
399 if (!ucmsg) {
400 free(cmsg, M_TEMP);
401 return (EMSGSIZE);
402 }
403
404 (void) copyout(cmsg, ucmsg, CMSG_SPACE(omsg.msg_accrightslen));
405 free(cmsg, M_TEMP);
406
407 msg.msg_control = ucmsg;
408 msg.msg_controllen = CMSG_SPACE(omsg.msg_accrightslen);
409 } else {
410 msg.msg_control = NULL;
411 msg.msg_controllen = 0;
412 }
413
414 error = sendit(p, SCARG(uap, s), &msg, SCARG(uap, flags), retval);
415 done:
416 if (iov != aiov)
417 FREE(iov, M_IOV);
418 return (error);
419 }
420
421 static int
422 compat_43_sa_put(from)
423 caddr_t from;
424 {
425 struct osockaddr *osa = (struct osockaddr *) from;
426 struct sockaddr sa;
427 struct osockaddr *kosa;
428 int error, len;
429
430 /*
431 * Only read/write the sockaddr family and length, the rest is
432 * not changed.
433 */
434 len = sizeof(sa.sa_len) + sizeof(sa.sa_family);
435
436 error = copyin((caddr_t) osa, (caddr_t) &sa, len);
437 if (error)
438 return (error);
439
440 /* Note: we convert from sockaddr sa_family to osockaddr one here */
441 kosa = (struct osockaddr *) &sa;
442 kosa->sa_family = sa.sa_family;
443 error = copyout(kosa, osa, len);
444 if (error)
445 return (error);
446
447 return (0);
448 }
449