uipc_syscalls.c revision 1.52 1 1.52 sommerfe /* $NetBSD: uipc_syscalls.c,v 1.52 2000/05/27 00:40:47 sommerfeld Exp $ */
2 1.8 cgd
3 1.1 cgd /*
4 1.7 mycroft * Copyright (c) 1982, 1986, 1989, 1990, 1993
5 1.7 mycroft * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.1 cgd * 3. All advertising materials mentioning features or use of this software
16 1.1 cgd * must display the following acknowledgement:
17 1.1 cgd * This product includes software developed by the University of
18 1.1 cgd * California, Berkeley and its contributors.
19 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
20 1.1 cgd * may be used to endorse or promote products derived from this software
21 1.1 cgd * without specific prior written permission.
22 1.1 cgd *
23 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 cgd * SUCH DAMAGE.
34 1.1 cgd *
35 1.29 fvdl * @(#)uipc_syscalls.c 8.6 (Berkeley) 2/14/95
36 1.1 cgd */
37 1.31 thorpej
38 1.31 thorpej #include "opt_ktrace.h"
39 1.40 drochner #include "opt_compat_freebsd.h"
40 1.40 drochner #include "opt_compat_linux.h"
41 1.40 drochner #include "opt_compat_sunos.h"
42 1.40 drochner #include "opt_compat_hpux.h"
43 1.40 drochner #include "opt_compat_ultrix.h"
44 1.40 drochner #include "opt_compat_43.h"
45 1.42 cgd #include "opt_compat_osf1.h"
46 1.40 drochner #if defined(COMPAT_43) || defined(COMPAT_SUNOS) || defined(COMPAT_LINUX) || \
47 1.42 cgd defined(COMPAT_HPUX) || defined(COMPAT_FREEBSD) || \
48 1.42 cgd defined(COMPAT_ULTRIX) || defined(COMPAT_OSF1)
49 1.40 drochner #define COMPAT_OLDSOCK /* used by <sys/socket.h> */
50 1.40 drochner #endif
51 1.1 cgd
52 1.6 mycroft #include <sys/param.h>
53 1.9 cgd #include <sys/systm.h>
54 1.6 mycroft #include <sys/filedesc.h>
55 1.6 mycroft #include <sys/proc.h>
56 1.6 mycroft #include <sys/file.h>
57 1.6 mycroft #include <sys/buf.h>
58 1.6 mycroft #include <sys/malloc.h>
59 1.6 mycroft #include <sys/mbuf.h>
60 1.6 mycroft #include <sys/protosw.h>
61 1.6 mycroft #include <sys/socket.h>
62 1.6 mycroft #include <sys/socketvar.h>
63 1.18 christos #include <sys/signalvar.h>
64 1.18 christos #include <sys/un.h>
65 1.1 cgd #ifdef KTRACE
66 1.6 mycroft #include <sys/ktrace.h>
67 1.1 cgd #endif
68 1.1 cgd
69 1.9 cgd #include <sys/mount.h>
70 1.9 cgd #include <sys/syscallargs.h>
71 1.9 cgd
72 1.44 darrenr #include <vm/vm.h>
73 1.44 darrenr #include <uvm/uvm_extern.h>
74 1.44 darrenr
75 1.1 cgd /*
76 1.1 cgd * System call interface to the socket abstraction.
77 1.1 cgd */
78 1.1 cgd extern struct fileops socketops;
79 1.1 cgd
80 1.7 mycroft int
81 1.16 mycroft sys_socket(p, v, retval)
82 1.1 cgd struct proc *p;
83 1.15 thorpej void *v;
84 1.15 thorpej register_t *retval;
85 1.15 thorpej {
86 1.51 augustss struct sys_socket_args /* {
87 1.9 cgd syscallarg(int) domain;
88 1.9 cgd syscallarg(int) type;
89 1.9 cgd syscallarg(int) protocol;
90 1.15 thorpej } */ *uap = v;
91 1.1 cgd struct filedesc *fdp = p->p_fd;
92 1.1 cgd struct socket *so;
93 1.1 cgd struct file *fp;
94 1.1 cgd int fd, error;
95 1.1 cgd
96 1.43 thorpej /* falloc() will use the desciptor for us */
97 1.18 christos if ((error = falloc(p, &fp, &fd)) != 0)
98 1.1 cgd return (error);
99 1.1 cgd fp->f_flag = FREAD|FWRITE;
100 1.1 cgd fp->f_type = DTYPE_SOCKET;
101 1.1 cgd fp->f_ops = &socketops;
102 1.18 christos error = socreate(SCARG(uap, domain), &so, SCARG(uap, type),
103 1.18 christos SCARG(uap, protocol));
104 1.18 christos if (error) {
105 1.43 thorpej FILE_UNUSE(fp, p);
106 1.50 thorpej fdremove(fdp, fd);
107 1.1 cgd ffree(fp);
108 1.1 cgd } else {
109 1.1 cgd fp->f_data = (caddr_t)so;
110 1.43 thorpej FILE_UNUSE(fp, p);
111 1.1 cgd *retval = fd;
112 1.1 cgd }
113 1.1 cgd return (error);
114 1.1 cgd }
115 1.1 cgd
116 1.1 cgd /* ARGSUSED */
117 1.7 mycroft int
118 1.16 mycroft sys_bind(p, v, retval)
119 1.1 cgd struct proc *p;
120 1.15 thorpej void *v;
121 1.15 thorpej register_t *retval;
122 1.15 thorpej {
123 1.51 augustss struct sys_bind_args /* {
124 1.9 cgd syscallarg(int) s;
125 1.23 cgd syscallarg(const struct sockaddr *) name;
126 1.41 kleink syscallarg(unsigned int) namelen;
127 1.15 thorpej } */ *uap = v;
128 1.1 cgd struct file *fp;
129 1.1 cgd struct mbuf *nam;
130 1.1 cgd int error;
131 1.1 cgd
132 1.43 thorpej /* getsock() will use the descriptor for us */
133 1.18 christos if ((error = getsock(p->p_fd, SCARG(uap, s), &fp)) != 0)
134 1.1 cgd return (error);
135 1.18 christos error = sockargs(&nam, SCARG(uap, name), SCARG(uap, namelen),
136 1.23 cgd MT_SONAME);
137 1.43 thorpej if (error) {
138 1.43 thorpej FILE_UNUSE(fp, p);
139 1.1 cgd return (error);
140 1.43 thorpej }
141 1.1 cgd error = sobind((struct socket *)fp->f_data, nam);
142 1.1 cgd m_freem(nam);
143 1.43 thorpej FILE_UNUSE(fp, p);
144 1.1 cgd return (error);
145 1.1 cgd }
146 1.1 cgd
147 1.1 cgd /* ARGSUSED */
148 1.7 mycroft int
149 1.16 mycroft sys_listen(p, v, retval)
150 1.1 cgd struct proc *p;
151 1.15 thorpej void *v;
152 1.15 thorpej register_t *retval;
153 1.15 thorpej {
154 1.51 augustss struct sys_listen_args /* {
155 1.9 cgd syscallarg(int) s;
156 1.9 cgd syscallarg(int) backlog;
157 1.15 thorpej } */ *uap = v;
158 1.1 cgd struct file *fp;
159 1.1 cgd int error;
160 1.1 cgd
161 1.43 thorpej /* getsock() will use the descriptor for us */
162 1.18 christos if ((error = getsock(p->p_fd, SCARG(uap, s), &fp)) != 0)
163 1.1 cgd return (error);
164 1.43 thorpej error = solisten((struct socket *)fp->f_data, SCARG(uap, backlog));
165 1.43 thorpej FILE_UNUSE(fp, p);
166 1.43 thorpej return (error);
167 1.1 cgd }
168 1.1 cgd
169 1.7 mycroft int
170 1.16 mycroft sys_accept(p, v, retval)
171 1.1 cgd struct proc *p;
172 1.15 thorpej void *v;
173 1.15 thorpej register_t *retval;
174 1.15 thorpej {
175 1.51 augustss struct sys_accept_args /* {
176 1.9 cgd syscallarg(int) s;
177 1.23 cgd syscallarg(struct sockaddr *) name;
178 1.41 kleink syscallarg(unsigned int *) anamelen;
179 1.15 thorpej } */ *uap = v;
180 1.49 mycroft struct filedesc *fdp = p->p_fd;
181 1.1 cgd struct file *fp;
182 1.1 cgd struct mbuf *nam;
183 1.41 kleink unsigned int namelen;
184 1.49 mycroft int error, s, fd;
185 1.51 augustss struct socket *so;
186 1.1 cgd
187 1.9 cgd if (SCARG(uap, name) && (error = copyin((caddr_t)SCARG(uap, anamelen),
188 1.34 perry (caddr_t)&namelen, sizeof(namelen))))
189 1.1 cgd return (error);
190 1.44 darrenr if (SCARG(uap, name) != NULL &&
191 1.44 darrenr uvm_useracc((caddr_t)SCARG(uap, name), sizeof(struct sockaddr),
192 1.44 darrenr B_WRITE) == FALSE)
193 1.44 darrenr return (EFAULT);
194 1.44 darrenr
195 1.43 thorpej /* getsock() will use the descriptor for us */
196 1.49 mycroft if ((error = getsock(fdp, SCARG(uap, s), &fp)) != 0)
197 1.1 cgd return (error);
198 1.14 mycroft s = splsoftnet();
199 1.1 cgd so = (struct socket *)fp->f_data;
200 1.43 thorpej FILE_UNUSE(fp, p);
201 1.44 darrenr if (!(so->so_proto->pr_flags & PR_LISTEN)) {
202 1.44 darrenr splx(s);
203 1.44 darrenr return (EOPNOTSUPP);
204 1.44 darrenr }
205 1.1 cgd if ((so->so_options & SO_ACCEPTCONN) == 0) {
206 1.1 cgd splx(s);
207 1.1 cgd return (EINVAL);
208 1.1 cgd }
209 1.1 cgd if ((so->so_state & SS_NBIO) && so->so_qlen == 0) {
210 1.1 cgd splx(s);
211 1.1 cgd return (EWOULDBLOCK);
212 1.1 cgd }
213 1.1 cgd while (so->so_qlen == 0 && so->so_error == 0) {
214 1.1 cgd if (so->so_state & SS_CANTRCVMORE) {
215 1.1 cgd so->so_error = ECONNABORTED;
216 1.1 cgd break;
217 1.1 cgd }
218 1.18 christos error = tsleep((caddr_t)&so->so_timeo, PSOCK | PCATCH,
219 1.18 christos netcon, 0);
220 1.18 christos if (error) {
221 1.1 cgd splx(s);
222 1.1 cgd return (error);
223 1.1 cgd }
224 1.1 cgd }
225 1.1 cgd if (so->so_error) {
226 1.1 cgd error = so->so_error;
227 1.1 cgd so->so_error = 0;
228 1.1 cgd splx(s);
229 1.1 cgd return (error);
230 1.1 cgd }
231 1.43 thorpej /* falloc() will use the descriptor for us */
232 1.49 mycroft if ((error = falloc(p, &fp, &fd)) != 0) {
233 1.1 cgd splx(s);
234 1.1 cgd return (error);
235 1.1 cgd }
236 1.49 mycroft *retval = fd;
237 1.27 thorpej { struct socket *aso = so->so_q.tqh_first;
238 1.1 cgd if (soqremque(aso, 1) == 0)
239 1.1 cgd panic("accept");
240 1.1 cgd so = aso;
241 1.1 cgd }
242 1.1 cgd fp->f_type = DTYPE_SOCKET;
243 1.1 cgd fp->f_flag = FREAD|FWRITE;
244 1.1 cgd fp->f_ops = &socketops;
245 1.1 cgd fp->f_data = (caddr_t)so;
246 1.43 thorpej FILE_UNUSE(fp, p);
247 1.1 cgd nam = m_get(M_WAIT, MT_SONAME);
248 1.47 jdolecek if ((error = soaccept(so, nam)) == 0 && SCARG(uap, name)) {
249 1.1 cgd if (namelen > nam->m_len)
250 1.1 cgd namelen = nam->m_len;
251 1.1 cgd /* SHOULD COPY OUT A CHAIN HERE */
252 1.9 cgd if ((error = copyout(mtod(nam, caddr_t),
253 1.48 enami (caddr_t)SCARG(uap, name), namelen)) == 0)
254 1.47 jdolecek error = copyout((caddr_t)&namelen,
255 1.48 enami (caddr_t)SCARG(uap, anamelen),
256 1.48 enami sizeof(*SCARG(uap, anamelen)));
257 1.1 cgd }
258 1.47 jdolecek /* if an error occured, free the file descriptor */
259 1.49 mycroft if (error) {
260 1.50 thorpej fdremove(fdp, fd);
261 1.47 jdolecek ffree(fp);
262 1.49 mycroft }
263 1.46 darrenr m_freem(nam);
264 1.46 darrenr splx(s);
265 1.46 darrenr return (error);
266 1.1 cgd }
267 1.1 cgd
268 1.1 cgd /* ARGSUSED */
269 1.7 mycroft int
270 1.16 mycroft sys_connect(p, v, retval)
271 1.1 cgd struct proc *p;
272 1.15 thorpej void *v;
273 1.15 thorpej register_t *retval;
274 1.15 thorpej {
275 1.51 augustss struct sys_connect_args /* {
276 1.9 cgd syscallarg(int) s;
277 1.23 cgd syscallarg(const struct sockaddr *) name;
278 1.41 kleink syscallarg(unsigned int) namelen;
279 1.15 thorpej } */ *uap = v;
280 1.1 cgd struct file *fp;
281 1.51 augustss struct socket *so;
282 1.1 cgd struct mbuf *nam;
283 1.1 cgd int error, s;
284 1.1 cgd
285 1.43 thorpej /* getsock() will use the descriptor for us */
286 1.18 christos if ((error = getsock(p->p_fd, SCARG(uap, s), &fp)) != 0)
287 1.1 cgd return (error);
288 1.1 cgd so = (struct socket *)fp->f_data;
289 1.43 thorpej FILE_UNUSE(fp, p);
290 1.1 cgd if ((so->so_state & SS_NBIO) && (so->so_state & SS_ISCONNECTING))
291 1.1 cgd return (EALREADY);
292 1.18 christos error = sockargs(&nam, SCARG(uap, name), SCARG(uap, namelen),
293 1.23 cgd MT_SONAME);
294 1.18 christos if (error)
295 1.1 cgd return (error);
296 1.1 cgd error = soconnect(so, nam);
297 1.1 cgd if (error)
298 1.1 cgd goto bad;
299 1.1 cgd if ((so->so_state & SS_NBIO) && (so->so_state & SS_ISCONNECTING)) {
300 1.1 cgd m_freem(nam);
301 1.1 cgd return (EINPROGRESS);
302 1.1 cgd }
303 1.14 mycroft s = splsoftnet();
304 1.18 christos while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
305 1.18 christos error = tsleep((caddr_t)&so->so_timeo, PSOCK | PCATCH,
306 1.18 christos netcon, 0);
307 1.18 christos if (error)
308 1.1 cgd break;
309 1.18 christos }
310 1.1 cgd if (error == 0) {
311 1.1 cgd error = so->so_error;
312 1.1 cgd so->so_error = 0;
313 1.1 cgd }
314 1.1 cgd splx(s);
315 1.1 cgd bad:
316 1.1 cgd so->so_state &= ~SS_ISCONNECTING;
317 1.1 cgd m_freem(nam);
318 1.1 cgd if (error == ERESTART)
319 1.1 cgd error = EINTR;
320 1.1 cgd return (error);
321 1.1 cgd }
322 1.1 cgd
323 1.7 mycroft int
324 1.16 mycroft sys_socketpair(p, v, retval)
325 1.1 cgd struct proc *p;
326 1.15 thorpej void *v;
327 1.15 thorpej register_t *retval;
328 1.15 thorpej {
329 1.51 augustss struct sys_socketpair_args /* {
330 1.9 cgd syscallarg(int) domain;
331 1.9 cgd syscallarg(int) type;
332 1.9 cgd syscallarg(int) protocol;
333 1.9 cgd syscallarg(int *) rsv;
334 1.15 thorpej } */ *uap = v;
335 1.51 augustss struct filedesc *fdp = p->p_fd;
336 1.1 cgd struct file *fp1, *fp2;
337 1.1 cgd struct socket *so1, *so2;
338 1.1 cgd int fd, error, sv[2];
339 1.1 cgd
340 1.18 christos error = socreate(SCARG(uap, domain), &so1, SCARG(uap, type),
341 1.18 christos SCARG(uap, protocol));
342 1.18 christos if (error)
343 1.1 cgd return (error);
344 1.18 christos error = socreate(SCARG(uap, domain), &so2, SCARG(uap, type),
345 1.18 christos SCARG(uap, protocol));
346 1.18 christos if (error)
347 1.1 cgd goto free1;
348 1.43 thorpej /* falloc() will use the descriptor for us */
349 1.18 christos if ((error = falloc(p, &fp1, &fd)) != 0)
350 1.1 cgd goto free2;
351 1.1 cgd sv[0] = fd;
352 1.1 cgd fp1->f_flag = FREAD|FWRITE;
353 1.1 cgd fp1->f_type = DTYPE_SOCKET;
354 1.1 cgd fp1->f_ops = &socketops;
355 1.1 cgd fp1->f_data = (caddr_t)so1;
356 1.18 christos if ((error = falloc(p, &fp2, &fd)) != 0)
357 1.1 cgd goto free3;
358 1.1 cgd fp2->f_flag = FREAD|FWRITE;
359 1.1 cgd fp2->f_type = DTYPE_SOCKET;
360 1.1 cgd fp2->f_ops = &socketops;
361 1.1 cgd fp2->f_data = (caddr_t)so2;
362 1.1 cgd sv[1] = fd;
363 1.18 christos if ((error = soconnect2(so1, so2)) != 0)
364 1.1 cgd goto free4;
365 1.9 cgd if (SCARG(uap, type) == SOCK_DGRAM) {
366 1.1 cgd /*
367 1.1 cgd * Datagram socket connection is asymmetric.
368 1.1 cgd */
369 1.18 christos if ((error = soconnect2(so2, so1)) != 0)
370 1.1 cgd goto free4;
371 1.1 cgd }
372 1.9 cgd error = copyout((caddr_t)sv, (caddr_t)SCARG(uap, rsv),
373 1.34 perry 2 * sizeof(int));
374 1.43 thorpej FILE_UNUSE(fp1, p);
375 1.43 thorpej FILE_UNUSE(fp2, p);
376 1.1 cgd return (error);
377 1.1 cgd free4:
378 1.43 thorpej FILE_UNUSE(fp2, p);
379 1.1 cgd ffree(fp2);
380 1.50 thorpej fdremove(fdp, sv[1]);
381 1.1 cgd free3:
382 1.43 thorpej FILE_UNUSE(fp1, p);
383 1.1 cgd ffree(fp1);
384 1.50 thorpej fdremove(fdp, sv[0]);
385 1.1 cgd free2:
386 1.1 cgd (void)soclose(so2);
387 1.1 cgd free1:
388 1.1 cgd (void)soclose(so1);
389 1.1 cgd return (error);
390 1.1 cgd }
391 1.1 cgd
392 1.7 mycroft int
393 1.16 mycroft sys_sendto(p, v, retval)
394 1.1 cgd struct proc *p;
395 1.15 thorpej void *v;
396 1.15 thorpej register_t *retval;
397 1.15 thorpej {
398 1.51 augustss struct sys_sendto_args /* {
399 1.9 cgd syscallarg(int) s;
400 1.23 cgd syscallarg(const void *) buf;
401 1.9 cgd syscallarg(size_t) len;
402 1.9 cgd syscallarg(int) flags;
403 1.23 cgd syscallarg(const struct sockaddr *) to;
404 1.41 kleink syscallarg(unsigned int) tolen;
405 1.15 thorpej } */ *uap = v;
406 1.1 cgd struct msghdr msg;
407 1.1 cgd struct iovec aiov;
408 1.1 cgd
409 1.23 cgd msg.msg_name = (caddr_t)SCARG(uap, to); /* XXX kills const */
410 1.9 cgd msg.msg_namelen = SCARG(uap, tolen);
411 1.1 cgd msg.msg_iov = &aiov;
412 1.1 cgd msg.msg_iovlen = 1;
413 1.1 cgd msg.msg_control = 0;
414 1.7 mycroft #ifdef COMPAT_OLDSOCK
415 1.1 cgd msg.msg_flags = 0;
416 1.1 cgd #endif
417 1.23 cgd aiov.iov_base = (char *)SCARG(uap, buf); /* XXX kills const */
418 1.9 cgd aiov.iov_len = SCARG(uap, len);
419 1.9 cgd return (sendit(p, SCARG(uap, s), &msg, SCARG(uap, flags), retval));
420 1.1 cgd }
421 1.1 cgd
422 1.7 mycroft int
423 1.16 mycroft sys_sendmsg(p, v, retval)
424 1.1 cgd struct proc *p;
425 1.15 thorpej void *v;
426 1.15 thorpej register_t *retval;
427 1.15 thorpej {
428 1.51 augustss struct sys_sendmsg_args /* {
429 1.9 cgd syscallarg(int) s;
430 1.23 cgd syscallarg(const struct msghdr *) msg;
431 1.9 cgd syscallarg(int) flags;
432 1.15 thorpej } */ *uap = v;
433 1.1 cgd struct msghdr msg;
434 1.1 cgd struct iovec aiov[UIO_SMALLIOV], *iov;
435 1.1 cgd int error;
436 1.1 cgd
437 1.34 perry error = copyin(SCARG(uap, msg), (caddr_t)&msg, sizeof(msg));
438 1.18 christos if (error)
439 1.1 cgd return (error);
440 1.41 kleink if ((unsigned int)msg.msg_iovlen > UIO_SMALLIOV) {
441 1.41 kleink if ((unsigned int)msg.msg_iovlen > IOV_MAX)
442 1.1 cgd return (EMSGSIZE);
443 1.1 cgd MALLOC(iov, struct iovec *,
444 1.39 mycroft sizeof(struct iovec) * msg.msg_iovlen, M_IOV, M_WAITOK);
445 1.39 mycroft } else
446 1.1 cgd iov = aiov;
447 1.41 kleink if ((unsigned int)msg.msg_iovlen > 0) {
448 1.39 mycroft error = copyin((caddr_t)msg.msg_iov, (caddr_t)iov,
449 1.39 mycroft (size_t)(msg.msg_iovlen * sizeof(struct iovec)));
450 1.39 mycroft if (error)
451 1.39 mycroft goto done;
452 1.39 mycroft }
453 1.1 cgd msg.msg_iov = iov;
454 1.7 mycroft #ifdef COMPAT_OLDSOCK
455 1.1 cgd msg.msg_flags = 0;
456 1.1 cgd #endif
457 1.9 cgd error = sendit(p, SCARG(uap, s), &msg, SCARG(uap, flags), retval);
458 1.1 cgd done:
459 1.1 cgd if (iov != aiov)
460 1.1 cgd FREE(iov, M_IOV);
461 1.1 cgd return (error);
462 1.1 cgd }
463 1.1 cgd
464 1.7 mycroft int
465 1.1 cgd sendit(p, s, mp, flags, retsize)
466 1.51 augustss struct proc *p;
467 1.1 cgd int s;
468 1.51 augustss struct msghdr *mp;
469 1.9 cgd int flags;
470 1.9 cgd register_t *retsize;
471 1.1 cgd {
472 1.1 cgd struct file *fp;
473 1.1 cgd struct uio auio;
474 1.51 augustss struct iovec *iov;
475 1.51 augustss int i;
476 1.1 cgd struct mbuf *to, *control;
477 1.1 cgd int len, error;
478 1.30 matt struct socket *so;
479 1.1 cgd #ifdef KTRACE
480 1.1 cgd struct iovec *ktriov = NULL;
481 1.1 cgd #endif
482 1.1 cgd
483 1.43 thorpej /* getsock() will use the descriptor for us */
484 1.18 christos if ((error = getsock(p->p_fd, s, &fp)) != 0)
485 1.1 cgd return (error);
486 1.1 cgd auio.uio_iov = mp->msg_iov;
487 1.1 cgd auio.uio_iovcnt = mp->msg_iovlen;
488 1.1 cgd auio.uio_segflg = UIO_USERSPACE;
489 1.1 cgd auio.uio_rw = UIO_WRITE;
490 1.1 cgd auio.uio_procp = p;
491 1.1 cgd auio.uio_offset = 0; /* XXX */
492 1.1 cgd auio.uio_resid = 0;
493 1.1 cgd iov = mp->msg_iov;
494 1.1 cgd for (i = 0; i < mp->msg_iovlen; i++, iov++) {
495 1.18 christos #if 0
496 1.18 christos /* cannot happen; iov_len is unsigned */
497 1.43 thorpej if (iov->iov_len < 0) {
498 1.43 thorpej error = EINVAL;
499 1.43 thorpej goto out;
500 1.43 thorpej }
501 1.18 christos #endif
502 1.33 thorpej /*
503 1.33 thorpej * Writes return ssize_t because -1 is returned on error.
504 1.33 thorpej * Therefore, we must restrict the length to SSIZE_MAX to
505 1.33 thorpej * avoid garbage return values.
506 1.33 thorpej */
507 1.33 thorpej auio.uio_resid += iov->iov_len;
508 1.43 thorpej if (iov->iov_len > SSIZE_MAX || auio.uio_resid > SSIZE_MAX) {
509 1.43 thorpej error = EINVAL;
510 1.43 thorpej goto out;
511 1.43 thorpej }
512 1.1 cgd }
513 1.1 cgd if (mp->msg_name) {
514 1.18 christos error = sockargs(&to, mp->msg_name, mp->msg_namelen,
515 1.18 christos MT_SONAME);
516 1.18 christos if (error)
517 1.43 thorpej goto out;
518 1.1 cgd } else
519 1.1 cgd to = 0;
520 1.1 cgd if (mp->msg_control) {
521 1.1 cgd if (mp->msg_controllen < sizeof(struct cmsghdr)
522 1.7 mycroft #ifdef COMPAT_OLDSOCK
523 1.1 cgd && mp->msg_flags != MSG_COMPAT
524 1.1 cgd #endif
525 1.1 cgd ) {
526 1.1 cgd error = EINVAL;
527 1.1 cgd goto bad;
528 1.1 cgd }
529 1.18 christos error = sockargs(&control, mp->msg_control,
530 1.18 christos mp->msg_controllen, MT_CONTROL);
531 1.18 christos if (error)
532 1.1 cgd goto bad;
533 1.7 mycroft #ifdef COMPAT_OLDSOCK
534 1.1 cgd if (mp->msg_flags == MSG_COMPAT) {
535 1.51 augustss struct cmsghdr *cm;
536 1.1 cgd
537 1.1 cgd M_PREPEND(control, sizeof(*cm), M_WAIT);
538 1.1 cgd if (control == 0) {
539 1.1 cgd error = ENOBUFS;
540 1.1 cgd goto bad;
541 1.1 cgd } else {
542 1.1 cgd cm = mtod(control, struct cmsghdr *);
543 1.1 cgd cm->cmsg_len = control->m_len;
544 1.1 cgd cm->cmsg_level = SOL_SOCKET;
545 1.1 cgd cm->cmsg_type = SCM_RIGHTS;
546 1.1 cgd }
547 1.1 cgd }
548 1.1 cgd #endif
549 1.1 cgd } else
550 1.1 cgd control = 0;
551 1.1 cgd #ifdef KTRACE
552 1.1 cgd if (KTRPOINT(p, KTR_GENIO)) {
553 1.34 perry int iovlen = auio.uio_iovcnt * sizeof(struct iovec);
554 1.1 cgd
555 1.1 cgd MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
556 1.36 perry memcpy((caddr_t)ktriov, (caddr_t)auio.uio_iov, iovlen);
557 1.1 cgd }
558 1.1 cgd #endif
559 1.1 cgd len = auio.uio_resid;
560 1.30 matt so = (struct socket *)fp->f_data;
561 1.30 matt error = (*so->so_send)(so, to, &auio, NULL, control, flags);
562 1.18 christos if (error) {
563 1.1 cgd if (auio.uio_resid != len && (error == ERESTART ||
564 1.1 cgd error == EINTR || error == EWOULDBLOCK))
565 1.1 cgd error = 0;
566 1.1 cgd if (error == EPIPE)
567 1.1 cgd psignal(p, SIGPIPE);
568 1.1 cgd }
569 1.1 cgd if (error == 0)
570 1.1 cgd *retsize = len - auio.uio_resid;
571 1.1 cgd #ifdef KTRACE
572 1.1 cgd if (ktriov != NULL) {
573 1.1 cgd if (error == 0)
574 1.52 sommerfe ktrgenio(p, s, UIO_WRITE, ktriov, *retsize, error);
575 1.1 cgd FREE(ktriov, M_TEMP);
576 1.1 cgd }
577 1.1 cgd #endif
578 1.43 thorpej bad:
579 1.1 cgd if (to)
580 1.1 cgd m_freem(to);
581 1.43 thorpej out:
582 1.43 thorpej FILE_UNUSE(fp, p);
583 1.1 cgd return (error);
584 1.1 cgd }
585 1.1 cgd
586 1.7 mycroft int
587 1.16 mycroft sys_recvfrom(p, v, retval)
588 1.1 cgd struct proc *p;
589 1.15 thorpej void *v;
590 1.15 thorpej register_t *retval;
591 1.15 thorpej {
592 1.51 augustss struct sys_recvfrom_args /* {
593 1.9 cgd syscallarg(int) s;
594 1.23 cgd syscallarg(void *) buf;
595 1.9 cgd syscallarg(size_t) len;
596 1.9 cgd syscallarg(int) flags;
597 1.23 cgd syscallarg(struct sockaddr *) from;
598 1.41 kleink syscallarg(unsigned int *) fromlenaddr;
599 1.15 thorpej } */ *uap = v;
600 1.1 cgd struct msghdr msg;
601 1.1 cgd struct iovec aiov;
602 1.1 cgd int error;
603 1.1 cgd
604 1.9 cgd if (SCARG(uap, fromlenaddr)) {
605 1.18 christos error = copyin((caddr_t)SCARG(uap, fromlenaddr),
606 1.18 christos (caddr_t)&msg.msg_namelen,
607 1.34 perry sizeof(msg.msg_namelen));
608 1.18 christos if (error)
609 1.1 cgd return (error);
610 1.1 cgd } else
611 1.1 cgd msg.msg_namelen = 0;
612 1.23 cgd msg.msg_name = (caddr_t)SCARG(uap, from);
613 1.1 cgd msg.msg_iov = &aiov;
614 1.1 cgd msg.msg_iovlen = 1;
615 1.9 cgd aiov.iov_base = SCARG(uap, buf);
616 1.9 cgd aiov.iov_len = SCARG(uap, len);
617 1.1 cgd msg.msg_control = 0;
618 1.9 cgd msg.msg_flags = SCARG(uap, flags);
619 1.9 cgd return (recvit(p, SCARG(uap, s), &msg,
620 1.18 christos (caddr_t)SCARG(uap, fromlenaddr), retval));
621 1.1 cgd }
622 1.1 cgd
623 1.7 mycroft int
624 1.16 mycroft sys_recvmsg(p, v, retval)
625 1.1 cgd struct proc *p;
626 1.15 thorpej void *v;
627 1.15 thorpej register_t *retval;
628 1.15 thorpej {
629 1.51 augustss struct sys_recvmsg_args /* {
630 1.9 cgd syscallarg(int) s;
631 1.9 cgd syscallarg(struct msghdr *) msg;
632 1.9 cgd syscallarg(int) flags;
633 1.15 thorpej } */ *uap = v;
634 1.1 cgd struct msghdr msg;
635 1.1 cgd struct iovec aiov[UIO_SMALLIOV], *uiov, *iov;
636 1.51 augustss int error;
637 1.1 cgd
638 1.18 christos error = copyin((caddr_t)SCARG(uap, msg), (caddr_t)&msg,
639 1.34 perry sizeof(msg));
640 1.18 christos if (error)
641 1.1 cgd return (error);
642 1.41 kleink if ((unsigned int)msg.msg_iovlen > UIO_SMALLIOV) {
643 1.41 kleink if ((unsigned int)msg.msg_iovlen > IOV_MAX)
644 1.1 cgd return (EMSGSIZE);
645 1.1 cgd MALLOC(iov, struct iovec *,
646 1.39 mycroft sizeof(struct iovec) * msg.msg_iovlen, M_IOV, M_WAITOK);
647 1.39 mycroft } else
648 1.1 cgd iov = aiov;
649 1.41 kleink if ((unsigned int)msg.msg_iovlen > 0) {
650 1.39 mycroft error = copyin((caddr_t)msg.msg_iov, (caddr_t)iov,
651 1.39 mycroft (size_t)(msg.msg_iovlen * sizeof(struct iovec)));
652 1.39 mycroft if (error)
653 1.39 mycroft goto done;
654 1.39 mycroft }
655 1.39 mycroft uiov = msg.msg_iov;
656 1.39 mycroft msg.msg_iov = iov;
657 1.7 mycroft #ifdef COMPAT_OLDSOCK
658 1.9 cgd msg.msg_flags = SCARG(uap, flags) &~ MSG_COMPAT;
659 1.1 cgd #else
660 1.9 cgd msg.msg_flags = SCARG(uap, flags);
661 1.1 cgd #endif
662 1.9 cgd if ((error = recvit(p, SCARG(uap, s), &msg, (caddr_t)0, retval)) == 0) {
663 1.1 cgd msg.msg_iov = uiov;
664 1.9 cgd error = copyout((caddr_t)&msg, (caddr_t)SCARG(uap, msg),
665 1.9 cgd sizeof(msg));
666 1.1 cgd }
667 1.1 cgd done:
668 1.1 cgd if (iov != aiov)
669 1.1 cgd FREE(iov, M_IOV);
670 1.1 cgd return (error);
671 1.1 cgd }
672 1.1 cgd
673 1.7 mycroft int
674 1.1 cgd recvit(p, s, mp, namelenp, retsize)
675 1.51 augustss struct proc *p;
676 1.1 cgd int s;
677 1.51 augustss struct msghdr *mp;
678 1.1 cgd caddr_t namelenp;
679 1.9 cgd register_t *retsize;
680 1.1 cgd {
681 1.1 cgd struct file *fp;
682 1.1 cgd struct uio auio;
683 1.51 augustss struct iovec *iov;
684 1.51 augustss int i;
685 1.1 cgd int len, error;
686 1.1 cgd struct mbuf *from = 0, *control = 0;
687 1.30 matt struct socket *so;
688 1.1 cgd #ifdef KTRACE
689 1.1 cgd struct iovec *ktriov = NULL;
690 1.1 cgd #endif
691 1.1 cgd
692 1.43 thorpej /* getsock() will use the descriptor for us */
693 1.18 christos if ((error = getsock(p->p_fd, s, &fp)) != 0)
694 1.1 cgd return (error);
695 1.1 cgd auio.uio_iov = mp->msg_iov;
696 1.1 cgd auio.uio_iovcnt = mp->msg_iovlen;
697 1.1 cgd auio.uio_segflg = UIO_USERSPACE;
698 1.1 cgd auio.uio_rw = UIO_READ;
699 1.1 cgd auio.uio_procp = p;
700 1.1 cgd auio.uio_offset = 0; /* XXX */
701 1.1 cgd auio.uio_resid = 0;
702 1.1 cgd iov = mp->msg_iov;
703 1.1 cgd for (i = 0; i < mp->msg_iovlen; i++, iov++) {
704 1.18 christos #if 0
705 1.18 christos /* cannot happen iov_len is unsigned */
706 1.43 thorpej if (iov->iov_len < 0) {
707 1.43 thorpej error = EINVAL;
708 1.43 thorpej goto out1;
709 1.43 thorpej }
710 1.18 christos #endif
711 1.33 thorpej /*
712 1.33 thorpej * Reads return ssize_t because -1 is returned on error.
713 1.33 thorpej * Therefore we must restrict the length to SSIZE_MAX to
714 1.33 thorpej * avoid garbage return values.
715 1.33 thorpej */
716 1.33 thorpej auio.uio_resid += iov->iov_len;
717 1.43 thorpej if (iov->iov_len > SSIZE_MAX || auio.uio_resid > SSIZE_MAX) {
718 1.43 thorpej error = EINVAL;
719 1.43 thorpej goto out1;
720 1.43 thorpej }
721 1.1 cgd }
722 1.1 cgd #ifdef KTRACE
723 1.1 cgd if (KTRPOINT(p, KTR_GENIO)) {
724 1.34 perry int iovlen = auio.uio_iovcnt * sizeof(struct iovec);
725 1.1 cgd
726 1.1 cgd MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
727 1.36 perry memcpy((caddr_t)ktriov, (caddr_t)auio.uio_iov, iovlen);
728 1.1 cgd }
729 1.1 cgd #endif
730 1.1 cgd len = auio.uio_resid;
731 1.30 matt so = (struct socket *)fp->f_data;
732 1.30 matt error = (*so->so_receive)(so, &from, &auio, NULL,
733 1.30 matt mp->msg_control ? &control : NULL, &mp->msg_flags);
734 1.18 christos if (error) {
735 1.1 cgd if (auio.uio_resid != len && (error == ERESTART ||
736 1.1 cgd error == EINTR || error == EWOULDBLOCK))
737 1.1 cgd error = 0;
738 1.1 cgd }
739 1.1 cgd #ifdef KTRACE
740 1.1 cgd if (ktriov != NULL) {
741 1.1 cgd if (error == 0)
742 1.52 sommerfe ktrgenio(p, s, UIO_READ, ktriov,
743 1.52 sommerfe len - auio.uio_resid, error);
744 1.1 cgd FREE(ktriov, M_TEMP);
745 1.1 cgd }
746 1.1 cgd #endif
747 1.1 cgd if (error)
748 1.1 cgd goto out;
749 1.1 cgd *retsize = len - auio.uio_resid;
750 1.1 cgd if (mp->msg_name) {
751 1.1 cgd len = mp->msg_namelen;
752 1.1 cgd if (len <= 0 || from == 0)
753 1.1 cgd len = 0;
754 1.1 cgd else {
755 1.7 mycroft #ifdef COMPAT_OLDSOCK
756 1.1 cgd if (mp->msg_flags & MSG_COMPAT)
757 1.1 cgd mtod(from, struct osockaddr *)->sa_family =
758 1.1 cgd mtod(from, struct sockaddr *)->sa_family;
759 1.1 cgd #endif
760 1.1 cgd if (len > from->m_len)
761 1.1 cgd len = from->m_len;
762 1.1 cgd /* else if len < from->m_len ??? */
763 1.18 christos error = copyout(mtod(from, caddr_t),
764 1.18 christos (caddr_t)mp->msg_name, (unsigned)len);
765 1.18 christos if (error)
766 1.1 cgd goto out;
767 1.1 cgd }
768 1.1 cgd mp->msg_namelen = len;
769 1.1 cgd if (namelenp &&
770 1.34 perry (error = copyout((caddr_t)&len, namelenp, sizeof(int)))) {
771 1.7 mycroft #ifdef COMPAT_OLDSOCK
772 1.1 cgd if (mp->msg_flags & MSG_COMPAT)
773 1.1 cgd error = 0; /* old recvfrom didn't check */
774 1.1 cgd else
775 1.1 cgd #endif
776 1.1 cgd goto out;
777 1.1 cgd }
778 1.1 cgd }
779 1.1 cgd if (mp->msg_control) {
780 1.7 mycroft #ifdef COMPAT_OLDSOCK
781 1.1 cgd /*
782 1.1 cgd * We assume that old recvmsg calls won't receive access
783 1.1 cgd * rights and other control info, esp. as control info
784 1.1 cgd * is always optional and those options didn't exist in 4.3.
785 1.1 cgd * If we receive rights, trim the cmsghdr; anything else
786 1.1 cgd * is tossed.
787 1.1 cgd */
788 1.1 cgd if (control && mp->msg_flags & MSG_COMPAT) {
789 1.1 cgd if (mtod(control, struct cmsghdr *)->cmsg_level !=
790 1.1 cgd SOL_SOCKET ||
791 1.1 cgd mtod(control, struct cmsghdr *)->cmsg_type !=
792 1.1 cgd SCM_RIGHTS) {
793 1.1 cgd mp->msg_controllen = 0;
794 1.1 cgd goto out;
795 1.1 cgd }
796 1.34 perry control->m_len -= sizeof(struct cmsghdr);
797 1.34 perry control->m_data += sizeof(struct cmsghdr);
798 1.1 cgd }
799 1.1 cgd #endif
800 1.1 cgd len = mp->msg_controllen;
801 1.1 cgd if (len <= 0 || control == 0)
802 1.1 cgd len = 0;
803 1.1 cgd else {
804 1.26 thorpej struct mbuf *m = control;
805 1.26 thorpej caddr_t p = (caddr_t)mp->msg_control;
806 1.26 thorpej
807 1.28 thorpej do {
808 1.26 thorpej i = m->m_len;
809 1.26 thorpej if (len < i) {
810 1.26 thorpej mp->msg_flags |= MSG_CTRUNC;
811 1.26 thorpej i = len;
812 1.26 thorpej }
813 1.26 thorpej error = copyout(mtod(m, caddr_t), p,
814 1.26 thorpej (unsigned)i);
815 1.28 thorpej if (m->m_next)
816 1.28 thorpej i = ALIGN(i);
817 1.26 thorpej p += i;
818 1.26 thorpej len -= i;
819 1.26 thorpej if (error != 0 || len <= 0)
820 1.26 thorpej break;
821 1.28 thorpej } while ((m = m->m_next) != NULL);
822 1.26 thorpej len = p - (caddr_t)mp->msg_control;
823 1.1 cgd }
824 1.1 cgd mp->msg_controllen = len;
825 1.1 cgd }
826 1.43 thorpej out:
827 1.1 cgd if (from)
828 1.1 cgd m_freem(from);
829 1.1 cgd if (control)
830 1.1 cgd m_freem(control);
831 1.43 thorpej out1:
832 1.43 thorpej FILE_UNUSE(fp, p);
833 1.1 cgd return (error);
834 1.1 cgd }
835 1.1 cgd
836 1.1 cgd /* ARGSUSED */
837 1.7 mycroft int
838 1.16 mycroft sys_shutdown(p, v, retval)
839 1.1 cgd struct proc *p;
840 1.15 thorpej void *v;
841 1.15 thorpej register_t *retval;
842 1.15 thorpej {
843 1.51 augustss struct sys_shutdown_args /* {
844 1.9 cgd syscallarg(int) s;
845 1.9 cgd syscallarg(int) how;
846 1.15 thorpej } */ *uap = v;
847 1.1 cgd struct file *fp;
848 1.1 cgd int error;
849 1.1 cgd
850 1.43 thorpej /* getsock() will use the descriptor for us */
851 1.18 christos if ((error = getsock(p->p_fd, SCARG(uap, s), &fp)) != 0)
852 1.1 cgd return (error);
853 1.43 thorpej error = soshutdown((struct socket *)fp->f_data, SCARG(uap, how));
854 1.43 thorpej FILE_UNUSE(fp, p);
855 1.43 thorpej return (error);
856 1.1 cgd }
857 1.1 cgd
858 1.1 cgd /* ARGSUSED */
859 1.7 mycroft int
860 1.16 mycroft sys_setsockopt(p, v, retval)
861 1.1 cgd struct proc *p;
862 1.15 thorpej void *v;
863 1.15 thorpej register_t *retval;
864 1.15 thorpej {
865 1.51 augustss struct sys_setsockopt_args /* {
866 1.9 cgd syscallarg(int) s;
867 1.9 cgd syscallarg(int) level;
868 1.9 cgd syscallarg(int) name;
869 1.23 cgd syscallarg(const void *) val;
870 1.41 kleink syscallarg(unsigned int) valsize;
871 1.15 thorpej } */ *uap = v;
872 1.1 cgd struct file *fp;
873 1.1 cgd struct mbuf *m = NULL;
874 1.1 cgd int error;
875 1.1 cgd
876 1.43 thorpej /* getsock() will use the descriptor for us */
877 1.18 christos if ((error = getsock(p->p_fd, SCARG(uap, s), &fp)) != 0)
878 1.1 cgd return (error);
879 1.43 thorpej if (SCARG(uap, valsize) > MLEN) {
880 1.43 thorpej error = EINVAL;
881 1.43 thorpej goto out;
882 1.43 thorpej }
883 1.9 cgd if (SCARG(uap, val)) {
884 1.1 cgd m = m_get(M_WAIT, MT_SOOPTS);
885 1.18 christos error = copyin(SCARG(uap, val), mtod(m, caddr_t),
886 1.41 kleink SCARG(uap, valsize));
887 1.18 christos if (error) {
888 1.1 cgd (void) m_free(m);
889 1.43 thorpej goto out;
890 1.1 cgd }
891 1.9 cgd m->m_len = SCARG(uap, valsize);
892 1.1 cgd }
893 1.43 thorpej error = sosetopt((struct socket *)fp->f_data, SCARG(uap, level),
894 1.43 thorpej SCARG(uap, name), m);
895 1.43 thorpej out:
896 1.43 thorpej FILE_UNUSE(fp, p);
897 1.43 thorpej return (error);
898 1.1 cgd }
899 1.1 cgd
900 1.1 cgd /* ARGSUSED */
901 1.7 mycroft int
902 1.16 mycroft sys_getsockopt(p, v, retval)
903 1.1 cgd struct proc *p;
904 1.15 thorpej void *v;
905 1.15 thorpej register_t *retval;
906 1.15 thorpej {
907 1.51 augustss struct sys_getsockopt_args /* {
908 1.9 cgd syscallarg(int) s;
909 1.9 cgd syscallarg(int) level;
910 1.9 cgd syscallarg(int) name;
911 1.23 cgd syscallarg(void *) val;
912 1.41 kleink syscallarg(unsigned int *) avalsize;
913 1.15 thorpej } */ *uap = v;
914 1.1 cgd struct file *fp;
915 1.45 itojun struct mbuf *m = NULL, *m0;
916 1.45 itojun unsigned int op, i, valsize;
917 1.41 kleink int error;
918 1.1 cgd
919 1.43 thorpej /* getsock() will use the descriptor for us */
920 1.18 christos if ((error = getsock(p->p_fd, SCARG(uap, s), &fp)) != 0)
921 1.1 cgd return (error);
922 1.9 cgd if (SCARG(uap, val)) {
923 1.18 christos error = copyin((caddr_t)SCARG(uap, avalsize),
924 1.34 perry (caddr_t)&valsize, sizeof(valsize));
925 1.18 christos if (error)
926 1.43 thorpej goto out;
927 1.1 cgd } else
928 1.1 cgd valsize = 0;
929 1.9 cgd if ((error = sogetopt((struct socket *)fp->f_data, SCARG(uap, level),
930 1.9 cgd SCARG(uap, name), &m)) == 0 && SCARG(uap, val) && valsize &&
931 1.9 cgd m != NULL) {
932 1.45 itojun op = 0;
933 1.45 itojun while (m && !error && op < valsize) {
934 1.45 itojun i = min(m->m_len, (valsize - op));
935 1.45 itojun error = copyout(mtod(m, caddr_t), SCARG(uap, val), i);
936 1.45 itojun op += i;
937 1.45 itojun SCARG(uap, val) = ((u_int8_t *)SCARG(uap, val)) + i;
938 1.45 itojun m0 = m;
939 1.45 itojun MFREE(m0, m);
940 1.45 itojun }
941 1.45 itojun valsize = op;
942 1.1 cgd if (error == 0)
943 1.45 itojun error = copyout(&valsize,
944 1.45 itojun SCARG(uap, avalsize), sizeof(valsize));
945 1.1 cgd }
946 1.1 cgd if (m != NULL)
947 1.1 cgd (void) m_free(m);
948 1.43 thorpej out:
949 1.43 thorpej FILE_UNUSE(fp, p);
950 1.1 cgd return (error);
951 1.1 cgd }
952 1.1 cgd
953 1.1 cgd /* ARGSUSED */
954 1.7 mycroft int
955 1.16 mycroft sys_pipe(p, v, retval)
956 1.1 cgd struct proc *p;
957 1.16 mycroft void *v;
958 1.9 cgd register_t *retval;
959 1.1 cgd {
960 1.51 augustss struct filedesc *fdp = p->p_fd;
961 1.1 cgd struct file *rf, *wf;
962 1.1 cgd struct socket *rso, *wso;
963 1.1 cgd int fd, error;
964 1.1 cgd
965 1.32 lukem if ((error = socreate(AF_LOCAL, &rso, SOCK_STREAM, 0)) != 0)
966 1.1 cgd return (error);
967 1.32 lukem if ((error = socreate(AF_LOCAL, &wso, SOCK_STREAM, 0)) != 0)
968 1.1 cgd goto free1;
969 1.43 thorpej /* falloc() will use the descriptor for us */
970 1.18 christos if ((error = falloc(p, &rf, &fd)) != 0)
971 1.1 cgd goto free2;
972 1.1 cgd retval[0] = fd;
973 1.1 cgd rf->f_flag = FREAD;
974 1.1 cgd rf->f_type = DTYPE_SOCKET;
975 1.1 cgd rf->f_ops = &socketops;
976 1.1 cgd rf->f_data = (caddr_t)rso;
977 1.18 christos if ((error = falloc(p, &wf, &fd)) != 0)
978 1.1 cgd goto free3;
979 1.1 cgd wf->f_flag = FWRITE;
980 1.1 cgd wf->f_type = DTYPE_SOCKET;
981 1.1 cgd wf->f_ops = &socketops;
982 1.1 cgd wf->f_data = (caddr_t)wso;
983 1.1 cgd retval[1] = fd;
984 1.18 christos if ((error = unp_connect2(wso, rso)) != 0)
985 1.1 cgd goto free4;
986 1.43 thorpej FILE_UNUSE(rf, p);
987 1.43 thorpej FILE_UNUSE(wf, p);
988 1.1 cgd return (0);
989 1.1 cgd free4:
990 1.43 thorpej FILE_UNUSE(wf, p);
991 1.1 cgd ffree(wf);
992 1.50 thorpej fdremove(fdp, retval[1]);
993 1.1 cgd free3:
994 1.43 thorpej FILE_UNUSE(rf, p);
995 1.1 cgd ffree(rf);
996 1.50 thorpej fdremove(fdp, retval[0]);
997 1.1 cgd free2:
998 1.1 cgd (void)soclose(wso);
999 1.1 cgd free1:
1000 1.1 cgd (void)soclose(rso);
1001 1.1 cgd return (error);
1002 1.1 cgd }
1003 1.1 cgd
1004 1.1 cgd /*
1005 1.1 cgd * Get socket name.
1006 1.1 cgd */
1007 1.13 christos /* ARGSUSED */
1008 1.7 mycroft int
1009 1.16 mycroft sys_getsockname(p, v, retval)
1010 1.1 cgd struct proc *p;
1011 1.15 thorpej void *v;
1012 1.15 thorpej register_t *retval;
1013 1.15 thorpej {
1014 1.51 augustss struct sys_getsockname_args /* {
1015 1.9 cgd syscallarg(int) fdes;
1016 1.23 cgd syscallarg(struct sockaddr *) asa;
1017 1.41 kleink syscallarg(unsigned int *) alen;
1018 1.15 thorpej } */ *uap = v;
1019 1.1 cgd struct file *fp;
1020 1.51 augustss struct socket *so;
1021 1.1 cgd struct mbuf *m;
1022 1.41 kleink unsigned int len;
1023 1.41 kleink int error;
1024 1.1 cgd
1025 1.43 thorpej /* getsock() will use the descriptor for us */
1026 1.18 christos if ((error = getsock(p->p_fd, SCARG(uap, fdes), &fp)) != 0)
1027 1.1 cgd return (error);
1028 1.34 perry error = copyin((caddr_t)SCARG(uap, alen), (caddr_t)&len, sizeof(len));
1029 1.18 christos if (error)
1030 1.43 thorpej goto out;
1031 1.1 cgd so = (struct socket *)fp->f_data;
1032 1.1 cgd m = m_getclr(M_WAIT, MT_SONAME);
1033 1.21 mycroft error = (*so->so_proto->pr_usrreq)(so, PRU_SOCKADDR, (struct mbuf *)0,
1034 1.21 mycroft m, (struct mbuf *)0, (struct proc *)0);
1035 1.18 christos if (error)
1036 1.1 cgd goto bad;
1037 1.1 cgd if (len > m->m_len)
1038 1.1 cgd len = m->m_len;
1039 1.41 kleink error = copyout(mtod(m, caddr_t), (caddr_t)SCARG(uap, asa), len);
1040 1.1 cgd if (error == 0)
1041 1.9 cgd error = copyout((caddr_t)&len, (caddr_t)SCARG(uap, alen),
1042 1.34 perry sizeof(len));
1043 1.43 thorpej bad:
1044 1.1 cgd m_freem(m);
1045 1.43 thorpej out:
1046 1.43 thorpej FILE_UNUSE(fp, p);
1047 1.1 cgd return (error);
1048 1.1 cgd }
1049 1.1 cgd
1050 1.1 cgd /*
1051 1.1 cgd * Get name of peer for connected socket.
1052 1.1 cgd */
1053 1.13 christos /* ARGSUSED */
1054 1.7 mycroft int
1055 1.16 mycroft sys_getpeername(p, v, retval)
1056 1.1 cgd struct proc *p;
1057 1.15 thorpej void *v;
1058 1.15 thorpej register_t *retval;
1059 1.15 thorpej {
1060 1.51 augustss struct sys_getpeername_args /* {
1061 1.9 cgd syscallarg(int) fdes;
1062 1.23 cgd syscallarg(struct sockaddr *) asa;
1063 1.41 kleink syscallarg(unsigned int *) alen;
1064 1.15 thorpej } */ *uap = v;
1065 1.1 cgd struct file *fp;
1066 1.51 augustss struct socket *so;
1067 1.1 cgd struct mbuf *m;
1068 1.41 kleink unsigned int len;
1069 1.41 kleink int error;
1070 1.1 cgd
1071 1.43 thorpej /* getsock() will use the descriptor for us */
1072 1.18 christos if ((error = getsock(p->p_fd, SCARG(uap, fdes), &fp)) != 0)
1073 1.1 cgd return (error);
1074 1.1 cgd so = (struct socket *)fp->f_data;
1075 1.43 thorpej if ((so->so_state & (SS_ISCONNECTED|SS_ISCONFIRMING)) == 0) {
1076 1.43 thorpej error = ENOTCONN;
1077 1.43 thorpej goto out;
1078 1.43 thorpej }
1079 1.34 perry error = copyin((caddr_t)SCARG(uap, alen), (caddr_t)&len, sizeof(len));
1080 1.18 christos if (error)
1081 1.43 thorpej goto out;
1082 1.1 cgd m = m_getclr(M_WAIT, MT_SONAME);
1083 1.21 mycroft error = (*so->so_proto->pr_usrreq)(so, PRU_PEERADDR, (struct mbuf *)0,
1084 1.21 mycroft m, (struct mbuf *)0, (struct proc *)0);
1085 1.18 christos if (error)
1086 1.1 cgd goto bad;
1087 1.1 cgd if (len > m->m_len)
1088 1.1 cgd len = m->m_len;
1089 1.41 kleink error = copyout(mtod(m, caddr_t), (caddr_t)SCARG(uap, asa), len);
1090 1.18 christos if (error)
1091 1.1 cgd goto bad;
1092 1.34 perry error = copyout((caddr_t)&len, (caddr_t)SCARG(uap, alen), sizeof(len));
1093 1.43 thorpej bad:
1094 1.1 cgd m_freem(m);
1095 1.43 thorpej out:
1096 1.43 thorpej FILE_UNUSE(fp, p);
1097 1.1 cgd return (error);
1098 1.1 cgd }
1099 1.1 cgd
1100 1.24 thorpej /*
1101 1.24 thorpej * XXX In a perfect world, we wouldn't pass around socket control
1102 1.24 thorpej * XXX arguments in mbufs, and this could go away.
1103 1.24 thorpej */
1104 1.7 mycroft int
1105 1.1 cgd sockargs(mp, buf, buflen, type)
1106 1.1 cgd struct mbuf **mp;
1107 1.23 cgd const void *buf;
1108 1.1 cgd int buflen, type;
1109 1.1 cgd {
1110 1.51 augustss struct sockaddr *sa;
1111 1.51 augustss struct mbuf *m;
1112 1.1 cgd int error;
1113 1.1 cgd
1114 1.24 thorpej /*
1115 1.25 thorpej * We can't allow socket names > UCHAR_MAX in length, since that
1116 1.24 thorpej * will overflow sa_len.
1117 1.24 thorpej */
1118 1.25 thorpej if (type == MT_SONAME && (u_int)buflen > UCHAR_MAX)
1119 1.24 thorpej return (EINVAL);
1120 1.24 thorpej
1121 1.24 thorpej /* Allocate an mbuf to hold the arguments. */
1122 1.24 thorpej m = m_get(M_WAIT, type);
1123 1.1 cgd if ((u_int)buflen > MLEN) {
1124 1.24 thorpej /*
1125 1.24 thorpej * Won't fit into a regular mbuf, so we allocate just
1126 1.24 thorpej * enough external storage to hold the argument.
1127 1.24 thorpej */
1128 1.24 thorpej MEXTMALLOC(m, buflen, M_WAITOK);
1129 1.1 cgd }
1130 1.1 cgd m->m_len = buflen;
1131 1.1 cgd error = copyin(buf, mtod(m, caddr_t), (u_int)buflen);
1132 1.1 cgd if (error) {
1133 1.1 cgd (void) m_free(m);
1134 1.7 mycroft return (error);
1135 1.1 cgd }
1136 1.1 cgd *mp = m;
1137 1.1 cgd if (type == MT_SONAME) {
1138 1.7 mycroft sa = mtod(m, struct sockaddr *);
1139 1.1 cgd
1140 1.7 mycroft #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1141 1.1 cgd if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
1142 1.1 cgd sa->sa_family = sa->sa_len;
1143 1.1 cgd #endif
1144 1.1 cgd sa->sa_len = buflen;
1145 1.1 cgd }
1146 1.1 cgd return (0);
1147 1.1 cgd }
1148 1.1 cgd
1149 1.7 mycroft int
1150 1.1 cgd getsock(fdp, fdes, fpp)
1151 1.1 cgd struct filedesc *fdp;
1152 1.1 cgd int fdes;
1153 1.1 cgd struct file **fpp;
1154 1.1 cgd {
1155 1.51 augustss struct file *fp;
1156 1.1 cgd
1157 1.1 cgd if ((unsigned)fdes >= fdp->fd_nfiles ||
1158 1.43 thorpej (fp = fdp->fd_ofiles[fdes]) == NULL ||
1159 1.43 thorpej (fp->f_iflags & FIF_WANTCLOSE) != 0)
1160 1.1 cgd return (EBADF);
1161 1.43 thorpej
1162 1.43 thorpej FILE_USE(fp);
1163 1.43 thorpej
1164 1.43 thorpej if (fp->f_type != DTYPE_SOCKET) {
1165 1.43 thorpej FILE_UNUSE(fp, NULL);
1166 1.1 cgd return (ENOTSOCK);
1167 1.43 thorpej }
1168 1.1 cgd *fpp = fp;
1169 1.1 cgd return (0);
1170 1.1 cgd }
1171