uipc_syscalls_43.c revision 1.30 1 /* $NetBSD: uipc_syscalls_43.c,v 1.30 2007/06/01 22:53:52 dsl 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.30 2007/06/01 22:53:52 dsl 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 #include <sys/protosw.h>
54
55 #include <sys/mount.h>
56 #include <sys/syscallargs.h>
57
58 #include <net/if.h>
59 #include <compat/sys/socket.h>
60 #include <compat/sys/sockio.h>
61
62 #include <compat/common/compat_util.h>
63
64 #include <uvm/uvm_extern.h>
65
66 /*
67 * Following 4.3 syscalls were not versioned, even through they should
68 * have been:
69 * connect(2), bind(2), sendto(2)
70 */
71
72 static int compat_43_sa_put(void *);
73
74 int
75 compat_43_sys_accept(struct lwp *l, void *v, register_t *retval)
76 {
77 struct compat_43_sys_accept_args /* {
78 syscallarg(int) s;
79 syscallarg(void *) name;
80 syscallarg(int *) anamelen;
81 } */ *uap = v;
82 int error;
83
84 if ((error = sys_accept(l, v, retval)) != 0)
85 return error;
86
87 if (SCARG(uap, name)
88 && (error = compat_43_sa_put(SCARG(uap, name))))
89 return (error);
90
91 return 0;
92 }
93
94 int
95 compat_43_sys_getpeername(struct lwp *l, void *v, register_t *retval)
96 {
97 struct compat_43_sys_getpeername_args /* {
98 syscallarg(int) fdes;
99 syscallarg(void *) asa;
100 syscallarg(int *) alen;
101 } */ *uap = v;
102
103 int error;
104
105 if ((error = sys_getpeername(l, v, retval)) != 0)
106 return error;
107
108 if ((error = compat_43_sa_put(SCARG(uap, asa))))
109 return (error);
110
111 return 0;
112 }
113
114 int
115 compat_43_sys_getsockname(struct lwp *l, void *v, register_t *retval)
116 {
117 struct compat_43_sys_getsockname_args /* {
118 syscallarg(int) fdes;
119 syscallarg(void *) asa;
120 syscallarg(int *) alen;
121 } */ *uap = v;
122 int error;
123
124 if ((error = sys_getsockname(l, v, retval)) != 0)
125 return error;
126
127 if ((error = compat_43_sa_put(SCARG(uap, asa))))
128 return (error);
129
130 return 0;
131 }
132
133 int
134 compat_43_sys_recv(struct lwp *l, void *v, register_t *retval)
135 {
136 struct compat_43_sys_recv_args /* {
137 syscallarg(int) s;
138 syscallarg(void *) buf;
139 syscallarg(int) len;
140 syscallarg(int) flags;
141 } */ *uap = v;
142 struct sys_recvfrom_args bra;
143
144 SCARG(&bra, s) = SCARG(uap, s);
145 SCARG(&bra, buf) = SCARG(uap, buf);
146 SCARG(&bra, len) = (size_t) SCARG(uap, len);
147 SCARG(&bra, flags) = SCARG(uap, flags);
148 SCARG(&bra, from) = NULL;
149 SCARG(&bra, fromlenaddr) = NULL;
150
151 return (sys_recvfrom(l, &bra, retval));
152 }
153
154 int
155 compat_43_sys_recvfrom(struct lwp *l, void *v, register_t *retval)
156 {
157 struct compat_43_sys_recvfrom_args /* {
158 syscallarg(int) s;
159 syscallarg(void *) buf;
160 syscallarg(size_t) len;
161 syscallarg(int) flags;
162 syscallarg(void *) from;
163 syscallarg(int *) fromlenaddr;
164 } */ *uap = v;
165 int error;
166
167 if ((error = sys_recvfrom(l, v, retval)))
168 return (error);
169
170 if (SCARG(uap, from) && (error = compat_43_sa_put(SCARG(uap, from))))
171 return (error);
172
173 return (0);
174 }
175
176 /*
177 * Old recvmsg. Arrange necessary structures, calls generic code and
178 * adjusts results accordingly.
179 */
180 int
181 compat_43_sys_recvmsg(struct lwp *l, void *v, register_t *retval)
182 {
183 struct compat_43_sys_recvmsg_args /* {
184 syscallarg(int) s;
185 syscallarg(struct omsghdr *) msg;
186 syscallarg(int) flags;
187 } */ *uap = v;
188 struct proc *p = l->l_proc;
189 struct omsghdr omsg;
190 struct msghdr msg;
191 struct iovec aiov[UIO_SMALLIOV], *iov;
192 int error;
193
194 error = copyin((void *)SCARG(uap, msg), (void *)&omsg,
195 sizeof (struct omsghdr));
196 if (error)
197 return (error);
198 if ((u_int)omsg.msg_iovlen > UIO_SMALLIOV) {
199 if ((u_int)omsg.msg_iovlen > IOV_MAX)
200 return (EMSGSIZE);
201 iov = malloc(sizeof(struct iovec) * omsg.msg_iovlen,
202 M_IOV, M_WAITOK);
203 } else
204 iov = aiov;
205
206 error = copyin((void *)omsg.msg_iov, (void *)iov,
207 (unsigned)(omsg.msg_iovlen * sizeof (struct iovec)));
208 if (error)
209 goto done;
210
211 msg.msg_name = omsg.msg_name;
212 msg.msg_namelen = omsg.msg_namelen;
213 msg.msg_iovlen = omsg.msg_iovlen;
214 msg.msg_iov = iov;
215 msg.msg_flags = SCARG(uap, flags);
216
217 /*
218 * If caller passes accrights, arrange things for generic code to
219 * DTRT.
220 */
221 if (omsg.msg_accrights && omsg.msg_accrightslen) {
222 void *sg = stackgap_init(p, 0);
223 struct cmsg *ucmsg;
224
225 /* it was this way in 4.4BSD */
226 if ((u_int) omsg.msg_accrightslen > MLEN)
227 return (EINVAL);
228
229 ucmsg = stackgap_alloc(p, &sg, CMSG_SPACE(omsg.msg_accrightslen));
230 if (ucmsg == NULL)
231 return (EMSGSIZE);
232
233 msg.msg_control = ucmsg;
234 msg.msg_controllen = CMSG_SPACE(omsg.msg_accrightslen);
235 } else {
236 msg.msg_control = NULL;
237 msg.msg_controllen = 0;
238 }
239
240 error = recvit(l, SCARG(uap, s), &msg,
241 (void *)&SCARG(uap, msg)->msg_namelen, retval);
242
243 /*
244 * If there is any control information and it's SCM_RIGHTS,
245 * pass it back to the program.
246 */
247 if (!error && omsg.msg_accrights && msg.msg_controllen > 0) {
248 struct cmsghdr *cmsg;
249
250 /* safe - msg.msg_controllen set by kernel */
251 cmsg = malloc(msg.msg_controllen, M_TEMP, M_WAITOK);
252
253 error = copyin(msg.msg_control, cmsg, msg.msg_controllen);
254 if (error) {
255 free(cmsg, M_TEMP);
256 return (error);
257 }
258
259 if (cmsg->cmsg_level != SOL_SOCKET
260 || cmsg->cmsg_type != SCM_RIGHTS
261 || copyout(CMSG_DATA(cmsg), omsg.msg_accrights,
262 cmsg->cmsg_len)) {
263 omsg.msg_accrightslen = 0;
264 }
265
266 if (!error) {
267 error = copyout(&cmsg->cmsg_len,
268 &SCARG(uap, msg)->msg_accrightslen, sizeof(int));
269 }
270 free(cmsg, M_TEMP);
271 }
272
273 if (!error && omsg.msg_name) {
274 int namelen;
275
276 if ((error = copyin(&SCARG(uap, msg)->msg_namelen, &namelen, sizeof(int)) == 0)
277 && namelen > 0)
278 error = compat_43_sa_put(omsg.msg_name);
279 }
280
281 done:
282 if (iov != aiov)
283 free(iov, M_IOV);
284 return (error);
285 }
286
287 int
288 compat_43_sys_send(struct lwp *l, void *v, register_t *retval)
289 {
290 struct compat_43_sys_send_args /* {
291 syscallarg(int) s;
292 syscallarg(void *) buf;
293 syscallarg(int) len;
294 syscallarg(int) flags;
295 } */ *uap = v;
296 struct sys_sendto_args bsa;
297
298 SCARG(&bsa, s) = SCARG(uap, s);
299 SCARG(&bsa, buf) = SCARG(uap, buf);
300 SCARG(&bsa, len) = SCARG(uap, len);
301 SCARG(&bsa, flags) = SCARG(uap, flags);
302 SCARG(&bsa, to) = NULL;
303 SCARG(&bsa, tolen) = 0;
304
305 return (sys_sendto(l, &bsa, retval));
306 }
307
308 /*
309 * Old sendmsg. Arrange necessary structures, call generic code and
310 * adjust the results accordingly for old code.
311 */
312 int
313 compat_43_sys_sendmsg(struct lwp *l, void *v, register_t *retval)
314 {
315 struct compat_43_sys_sendmsg_args /* {
316 syscallarg(int) s;
317 syscallarg(void *) msg;
318 syscallarg(int) flags;
319 } */ *uap = v;
320 struct omsghdr omsg;
321 struct msghdr msg;
322 int error;
323 struct mbuf *nam;
324 struct mbuf *ctl;
325 struct osockaddr *osa;
326 struct sockaddr *sa;
327
328 error = copyin(SCARG(uap, msg), &omsg, sizeof (struct omsghdr));
329 if (error != 0)
330 return (error);
331
332 msg.msg_iovlen = omsg.msg_iovlen;
333 msg.msg_iov = omsg.msg_iov;
334
335 error = sockargs(&nam, omsg.msg_name, omsg.msg_namelen, MT_SONAME);
336 if (error != 0)
337 return (error);
338
339 sa = mtod(nam, void *);
340 osa = mtod(nam, void *);
341 sa->sa_family = osa->sa_family;
342 sa->sa_len = omsg.msg_namelen;
343
344 msg.msg_flags = MSG_IOVUSRSPACE | MSG_NAMEMBUF;
345
346 msg.msg_name = nam;
347 msg.msg_namelen = omsg.msg_namelen;
348
349 if (omsg.msg_accrights && omsg.msg_accrightslen != 0) {
350 struct cmsghdr *cmsg;
351 u_int clen;
352
353 clen = CMSG_SPACE(omsg.msg_accrightslen);
354 /* it was (almost) this way in 4.4BSD */
355 if (omsg.msg_accrightslen < 0 || clen > MLEN) {
356 error = EINVAL;
357 goto bad;
358 }
359
360 ctl = m_get(M_WAIT, MT_CONTROL);
361 ctl->m_len = clen;
362 cmsg = mtod(ctl, void *);
363 cmsg->cmsg_len = CMSG_SPACE(omsg.msg_accrightslen);
364 cmsg->cmsg_level = SOL_SOCKET;
365 cmsg->cmsg_type = SCM_RIGHTS;
366
367 error = copyin(omsg.msg_accrights, CMSG_DATA(cmsg),
368 omsg.msg_accrightslen);
369 if (error)
370 goto bad;
371
372 msg.msg_control = ctl;
373 msg.msg_controllen = clen;
374 msg.msg_flags |= MSG_CONTROLMBUF;
375 } else {
376 msg.msg_control = NULL;
377 msg.msg_controllen = 0;
378 }
379
380 return do_sys_sendmsg(l, SCARG(uap, s), &msg, SCARG(uap, flags), retval);
381
382 bad:
383 if (nam != NULL)
384 m_free(nam);
385
386 return (error);
387 }
388
389 static int
390 compat_43_sa_put(from)
391 void *from;
392 {
393 struct osockaddr *osa = (struct osockaddr *) from;
394 struct sockaddr sa;
395 struct osockaddr *kosa;
396 int error, len;
397
398 /*
399 * Only read/write the sockaddr family and length, the rest is
400 * not changed.
401 */
402 len = sizeof(sa.sa_len) + sizeof(sa.sa_family);
403
404 error = copyin((void *) osa, (void *) &sa, len);
405 if (error)
406 return (error);
407
408 /* Note: we convert from sockaddr sa_family to osockaddr one here */
409 kosa = (struct osockaddr *) &sa;
410 kosa->sa_family = sa.sa_family;
411 error = copyout(kosa, osa, len);
412 if (error)
413 return (error);
414
415 return (0);
416 }
417
418 int
419 compat_ifioctl(struct socket *so, u_long ocmd, u_long cmd, void *data,
420 struct lwp *l)
421 {
422 int error;
423 struct ifreq *ifr = data;
424 struct ifnet *ifp = ifunit(ifr->ifr_name);
425 struct sockaddr *sa;
426
427 if (ifp == NULL)
428 return ENXIO;
429
430 switch (ocmd) {
431 case OSIOCSIFADDR:
432 case OSIOCSIFDSTADDR:
433 case OSIOCSIFBRDADDR:
434 case OSIOCSIFNETMASK:
435 sa = &ifr->ifr_addr;
436 #if BYTE_ORDER != BIG_ENDIAN
437 if (sa->sa_family == 0 && sa->sa_len < 16) {
438 sa->sa_family = sa->sa_len;
439 sa->sa_len = 16;
440 }
441 #else
442 if (sa->sa_len == 0)
443 sa->sa_len = 16;
444 #endif
445 break;
446
447 case OOSIOCGIFADDR:
448 cmd = SIOCGIFADDR;
449 break;
450
451 case OOSIOCGIFDSTADDR:
452 cmd = SIOCGIFDSTADDR;
453 break;
454
455 case OOSIOCGIFBRDADDR:
456 cmd = SIOCGIFBRDADDR;
457 break;
458
459 case OOSIOCGIFNETMASK:
460 cmd = SIOCGIFNETMASK;
461 }
462
463 error = (*so->so_proto->pr_usrreq)(so, PRU_CONTROL,
464 (struct mbuf *)cmd, (struct mbuf *)ifr, (struct mbuf *)ifp, l);
465
466 switch (ocmd) {
467 case OOSIOCGIFADDR:
468 case OOSIOCGIFDSTADDR:
469 case OOSIOCGIFBRDADDR:
470 case OOSIOCGIFNETMASK:
471 *(u_int16_t *)&ifr->ifr_addr =
472 ((struct sockaddr *)&ifr->ifr_addr)->sa_family;
473 }
474 return error;
475 }
476