uipc_syscalls.c revision 1.65 1 1.65 jdolecek /* $NetBSD: uipc_syscalls.c,v 1.65 2001/07/17 11:49:41 jdolecek 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.60 jdolecek #include "opt_new_pipe.h"
40 1.55 jdolecek
41 1.6 mycroft #include <sys/param.h>
42 1.9 cgd #include <sys/systm.h>
43 1.6 mycroft #include <sys/filedesc.h>
44 1.6 mycroft #include <sys/proc.h>
45 1.6 mycroft #include <sys/file.h>
46 1.6 mycroft #include <sys/buf.h>
47 1.6 mycroft #include <sys/malloc.h>
48 1.6 mycroft #include <sys/mbuf.h>
49 1.6 mycroft #include <sys/protosw.h>
50 1.6 mycroft #include <sys/socket.h>
51 1.6 mycroft #include <sys/socketvar.h>
52 1.18 christos #include <sys/signalvar.h>
53 1.18 christos #include <sys/un.h>
54 1.1 cgd #ifdef KTRACE
55 1.6 mycroft #include <sys/ktrace.h>
56 1.1 cgd #endif
57 1.1 cgd
58 1.9 cgd #include <sys/mount.h>
59 1.9 cgd #include <sys/syscallargs.h>
60 1.9 cgd
61 1.44 darrenr #include <uvm/uvm_extern.h>
62 1.44 darrenr
63 1.1 cgd /*
64 1.1 cgd * System call interface to the socket abstraction.
65 1.1 cgd */
66 1.1 cgd extern struct fileops socketops;
67 1.1 cgd
68 1.7 mycroft int
69 1.57 lukem sys_socket(struct proc *p, void *v, register_t *retval)
70 1.15 thorpej {
71 1.51 augustss struct sys_socket_args /* {
72 1.57 lukem syscallarg(int) domain;
73 1.57 lukem syscallarg(int) type;
74 1.57 lukem syscallarg(int) protocol;
75 1.15 thorpej } */ *uap = v;
76 1.57 lukem struct filedesc *fdp;
77 1.57 lukem struct socket *so;
78 1.57 lukem struct file *fp;
79 1.57 lukem int fd, error;
80 1.1 cgd
81 1.57 lukem fdp = p->p_fd;
82 1.43 thorpej /* falloc() will use the desciptor for us */
83 1.18 christos if ((error = falloc(p, &fp, &fd)) != 0)
84 1.1 cgd return (error);
85 1.1 cgd fp->f_flag = FREAD|FWRITE;
86 1.1 cgd fp->f_type = DTYPE_SOCKET;
87 1.1 cgd fp->f_ops = &socketops;
88 1.18 christos error = socreate(SCARG(uap, domain), &so, SCARG(uap, type),
89 1.18 christos SCARG(uap, protocol));
90 1.18 christos if (error) {
91 1.43 thorpej FILE_UNUSE(fp, p);
92 1.50 thorpej fdremove(fdp, fd);
93 1.1 cgd ffree(fp);
94 1.1 cgd } else {
95 1.1 cgd fp->f_data = (caddr_t)so;
96 1.59 thorpej FILE_SET_MATURE(fp);
97 1.43 thorpej FILE_UNUSE(fp, p);
98 1.1 cgd *retval = fd;
99 1.1 cgd }
100 1.1 cgd return (error);
101 1.1 cgd }
102 1.1 cgd
103 1.1 cgd /* ARGSUSED */
104 1.7 mycroft int
105 1.57 lukem sys_bind(struct proc *p, void *v, register_t *retval)
106 1.15 thorpej {
107 1.51 augustss struct sys_bind_args /* {
108 1.57 lukem syscallarg(int) s;
109 1.57 lukem syscallarg(const struct sockaddr *) name;
110 1.57 lukem syscallarg(unsigned int) namelen;
111 1.15 thorpej } */ *uap = v;
112 1.57 lukem struct file *fp;
113 1.57 lukem struct mbuf *nam;
114 1.57 lukem int error;
115 1.1 cgd
116 1.43 thorpej /* getsock() will use the descriptor for us */
117 1.18 christos if ((error = getsock(p->p_fd, SCARG(uap, s), &fp)) != 0)
118 1.1 cgd return (error);
119 1.18 christos error = sockargs(&nam, SCARG(uap, name), SCARG(uap, namelen),
120 1.23 cgd MT_SONAME);
121 1.43 thorpej if (error) {
122 1.43 thorpej FILE_UNUSE(fp, p);
123 1.1 cgd return (error);
124 1.43 thorpej }
125 1.56 fvdl error = sobind((struct socket *)fp->f_data, nam, p);
126 1.1 cgd m_freem(nam);
127 1.43 thorpej FILE_UNUSE(fp, p);
128 1.1 cgd return (error);
129 1.1 cgd }
130 1.1 cgd
131 1.1 cgd /* ARGSUSED */
132 1.7 mycroft int
133 1.57 lukem sys_listen(struct proc *p, void *v, register_t *retval)
134 1.15 thorpej {
135 1.51 augustss struct sys_listen_args /* {
136 1.57 lukem syscallarg(int) s;
137 1.57 lukem syscallarg(int) backlog;
138 1.15 thorpej } */ *uap = v;
139 1.57 lukem struct file *fp;
140 1.57 lukem int error;
141 1.1 cgd
142 1.43 thorpej /* getsock() will use the descriptor for us */
143 1.18 christos if ((error = getsock(p->p_fd, SCARG(uap, s), &fp)) != 0)
144 1.1 cgd return (error);
145 1.43 thorpej error = solisten((struct socket *)fp->f_data, SCARG(uap, backlog));
146 1.43 thorpej FILE_UNUSE(fp, p);
147 1.43 thorpej return (error);
148 1.1 cgd }
149 1.1 cgd
150 1.7 mycroft int
151 1.57 lukem sys_accept(struct proc *p, void *v, register_t *retval)
152 1.15 thorpej {
153 1.51 augustss struct sys_accept_args /* {
154 1.57 lukem syscallarg(int) s;
155 1.57 lukem syscallarg(struct sockaddr *) name;
156 1.57 lukem syscallarg(unsigned int *) anamelen;
157 1.15 thorpej } */ *uap = v;
158 1.57 lukem struct filedesc *fdp;
159 1.57 lukem struct file *fp;
160 1.57 lukem struct mbuf *nam;
161 1.57 lukem unsigned int namelen;
162 1.57 lukem int error, s, fd;
163 1.57 lukem struct socket *so;
164 1.1 cgd
165 1.57 lukem fdp = p->p_fd;
166 1.9 cgd if (SCARG(uap, name) && (error = copyin((caddr_t)SCARG(uap, anamelen),
167 1.34 perry (caddr_t)&namelen, sizeof(namelen))))
168 1.1 cgd return (error);
169 1.44 darrenr if (SCARG(uap, name) != NULL &&
170 1.44 darrenr uvm_useracc((caddr_t)SCARG(uap, name), sizeof(struct sockaddr),
171 1.44 darrenr B_WRITE) == FALSE)
172 1.44 darrenr return (EFAULT);
173 1.44 darrenr
174 1.43 thorpej /* getsock() will use the descriptor for us */
175 1.49 mycroft if ((error = getsock(fdp, SCARG(uap, s), &fp)) != 0)
176 1.1 cgd return (error);
177 1.14 mycroft s = splsoftnet();
178 1.1 cgd so = (struct socket *)fp->f_data;
179 1.43 thorpej FILE_UNUSE(fp, p);
180 1.44 darrenr if (!(so->so_proto->pr_flags & PR_LISTEN)) {
181 1.44 darrenr splx(s);
182 1.44 darrenr return (EOPNOTSUPP);
183 1.44 darrenr }
184 1.1 cgd if ((so->so_options & SO_ACCEPTCONN) == 0) {
185 1.1 cgd splx(s);
186 1.1 cgd return (EINVAL);
187 1.1 cgd }
188 1.1 cgd if ((so->so_state & SS_NBIO) && so->so_qlen == 0) {
189 1.1 cgd splx(s);
190 1.1 cgd return (EWOULDBLOCK);
191 1.1 cgd }
192 1.1 cgd while (so->so_qlen == 0 && so->so_error == 0) {
193 1.1 cgd if (so->so_state & SS_CANTRCVMORE) {
194 1.1 cgd so->so_error = ECONNABORTED;
195 1.1 cgd break;
196 1.1 cgd }
197 1.18 christos error = tsleep((caddr_t)&so->so_timeo, PSOCK | PCATCH,
198 1.18 christos netcon, 0);
199 1.18 christos if (error) {
200 1.1 cgd splx(s);
201 1.1 cgd return (error);
202 1.1 cgd }
203 1.1 cgd }
204 1.1 cgd if (so->so_error) {
205 1.1 cgd error = so->so_error;
206 1.1 cgd so->so_error = 0;
207 1.1 cgd splx(s);
208 1.1 cgd return (error);
209 1.1 cgd }
210 1.43 thorpej /* falloc() will use the descriptor for us */
211 1.49 mycroft if ((error = falloc(p, &fp, &fd)) != 0) {
212 1.1 cgd splx(s);
213 1.1 cgd return (error);
214 1.1 cgd }
215 1.49 mycroft *retval = fd;
216 1.27 thorpej { struct socket *aso = so->so_q.tqh_first;
217 1.1 cgd if (soqremque(aso, 1) == 0)
218 1.1 cgd panic("accept");
219 1.1 cgd so = aso;
220 1.1 cgd }
221 1.1 cgd fp->f_type = DTYPE_SOCKET;
222 1.1 cgd fp->f_flag = FREAD|FWRITE;
223 1.1 cgd fp->f_ops = &socketops;
224 1.1 cgd fp->f_data = (caddr_t)so;
225 1.43 thorpej FILE_UNUSE(fp, p);
226 1.1 cgd nam = m_get(M_WAIT, MT_SONAME);
227 1.47 jdolecek if ((error = soaccept(so, nam)) == 0 && SCARG(uap, name)) {
228 1.1 cgd if (namelen > nam->m_len)
229 1.1 cgd namelen = nam->m_len;
230 1.1 cgd /* SHOULD COPY OUT A CHAIN HERE */
231 1.9 cgd if ((error = copyout(mtod(nam, caddr_t),
232 1.48 enami (caddr_t)SCARG(uap, name), namelen)) == 0)
233 1.47 jdolecek error = copyout((caddr_t)&namelen,
234 1.48 enami (caddr_t)SCARG(uap, anamelen),
235 1.48 enami sizeof(*SCARG(uap, anamelen)));
236 1.1 cgd }
237 1.47 jdolecek /* if an error occured, free the file descriptor */
238 1.49 mycroft if (error) {
239 1.50 thorpej fdremove(fdp, fd);
240 1.47 jdolecek ffree(fp);
241 1.49 mycroft }
242 1.46 darrenr m_freem(nam);
243 1.46 darrenr splx(s);
244 1.59 thorpej FILE_SET_MATURE(fp);
245 1.46 darrenr return (error);
246 1.1 cgd }
247 1.1 cgd
248 1.1 cgd /* ARGSUSED */
249 1.7 mycroft int
250 1.57 lukem sys_connect(struct proc *p, void *v, register_t *retval)
251 1.15 thorpej {
252 1.51 augustss struct sys_connect_args /* {
253 1.57 lukem syscallarg(int) s;
254 1.57 lukem syscallarg(const struct sockaddr *) name;
255 1.57 lukem syscallarg(unsigned int) namelen;
256 1.15 thorpej } */ *uap = v;
257 1.57 lukem struct file *fp;
258 1.57 lukem struct socket *so;
259 1.57 lukem struct mbuf *nam;
260 1.57 lukem int error, s;
261 1.1 cgd
262 1.43 thorpej /* getsock() will use the descriptor for us */
263 1.18 christos if ((error = getsock(p->p_fd, SCARG(uap, s), &fp)) != 0)
264 1.1 cgd return (error);
265 1.1 cgd so = (struct socket *)fp->f_data;
266 1.62 jdolecek if ((so->so_state & SS_NBIO) && (so->so_state & SS_ISCONNECTING)) {
267 1.62 jdolecek error = EALREADY;
268 1.62 jdolecek goto out;
269 1.62 jdolecek }
270 1.18 christos error = sockargs(&nam, SCARG(uap, name), SCARG(uap, namelen),
271 1.23 cgd MT_SONAME);
272 1.18 christos if (error)
273 1.62 jdolecek goto out;
274 1.1 cgd error = soconnect(so, nam);
275 1.1 cgd if (error)
276 1.1 cgd goto bad;
277 1.1 cgd if ((so->so_state & SS_NBIO) && (so->so_state & SS_ISCONNECTING)) {
278 1.1 cgd m_freem(nam);
279 1.62 jdolecek error = EINPROGRESS;
280 1.62 jdolecek goto out;
281 1.1 cgd }
282 1.14 mycroft s = splsoftnet();
283 1.18 christos while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
284 1.18 christos error = tsleep((caddr_t)&so->so_timeo, PSOCK | PCATCH,
285 1.18 christos netcon, 0);
286 1.18 christos if (error)
287 1.1 cgd break;
288 1.18 christos }
289 1.1 cgd if (error == 0) {
290 1.1 cgd error = so->so_error;
291 1.1 cgd so->so_error = 0;
292 1.1 cgd }
293 1.1 cgd splx(s);
294 1.57 lukem bad:
295 1.1 cgd so->so_state &= ~SS_ISCONNECTING;
296 1.1 cgd m_freem(nam);
297 1.1 cgd if (error == ERESTART)
298 1.1 cgd error = EINTR;
299 1.62 jdolecek out:
300 1.62 jdolecek FILE_UNUSE(fp, p);
301 1.1 cgd return (error);
302 1.1 cgd }
303 1.1 cgd
304 1.7 mycroft int
305 1.57 lukem sys_socketpair(struct proc *p, void *v, register_t *retval)
306 1.15 thorpej {
307 1.51 augustss struct sys_socketpair_args /* {
308 1.57 lukem syscallarg(int) domain;
309 1.57 lukem syscallarg(int) type;
310 1.57 lukem syscallarg(int) protocol;
311 1.57 lukem syscallarg(int *) rsv;
312 1.15 thorpej } */ *uap = v;
313 1.57 lukem struct filedesc *fdp;
314 1.57 lukem struct file *fp1, *fp2;
315 1.57 lukem struct socket *so1, *so2;
316 1.57 lukem int fd, error, sv[2];
317 1.1 cgd
318 1.57 lukem fdp = p->p_fd;
319 1.18 christos error = socreate(SCARG(uap, domain), &so1, SCARG(uap, type),
320 1.18 christos SCARG(uap, protocol));
321 1.18 christos if (error)
322 1.1 cgd return (error);
323 1.18 christos error = socreate(SCARG(uap, domain), &so2, SCARG(uap, type),
324 1.18 christos SCARG(uap, protocol));
325 1.18 christos if (error)
326 1.1 cgd goto free1;
327 1.43 thorpej /* falloc() will use the descriptor for us */
328 1.18 christos if ((error = falloc(p, &fp1, &fd)) != 0)
329 1.1 cgd goto free2;
330 1.1 cgd sv[0] = fd;
331 1.1 cgd fp1->f_flag = FREAD|FWRITE;
332 1.1 cgd fp1->f_type = DTYPE_SOCKET;
333 1.1 cgd fp1->f_ops = &socketops;
334 1.1 cgd fp1->f_data = (caddr_t)so1;
335 1.18 christos if ((error = falloc(p, &fp2, &fd)) != 0)
336 1.1 cgd goto free3;
337 1.1 cgd fp2->f_flag = FREAD|FWRITE;
338 1.1 cgd fp2->f_type = DTYPE_SOCKET;
339 1.1 cgd fp2->f_ops = &socketops;
340 1.1 cgd fp2->f_data = (caddr_t)so2;
341 1.1 cgd sv[1] = fd;
342 1.18 christos if ((error = soconnect2(so1, so2)) != 0)
343 1.1 cgd goto free4;
344 1.9 cgd if (SCARG(uap, type) == SOCK_DGRAM) {
345 1.1 cgd /*
346 1.1 cgd * Datagram socket connection is asymmetric.
347 1.1 cgd */
348 1.18 christos if ((error = soconnect2(so2, so1)) != 0)
349 1.1 cgd goto free4;
350 1.1 cgd }
351 1.9 cgd error = copyout((caddr_t)sv, (caddr_t)SCARG(uap, rsv),
352 1.34 perry 2 * sizeof(int));
353 1.59 thorpej FILE_SET_MATURE(fp1);
354 1.59 thorpej FILE_SET_MATURE(fp2);
355 1.43 thorpej FILE_UNUSE(fp1, p);
356 1.43 thorpej FILE_UNUSE(fp2, p);
357 1.1 cgd return (error);
358 1.57 lukem free4:
359 1.43 thorpej FILE_UNUSE(fp2, p);
360 1.1 cgd ffree(fp2);
361 1.50 thorpej fdremove(fdp, sv[1]);
362 1.57 lukem free3:
363 1.43 thorpej FILE_UNUSE(fp1, p);
364 1.1 cgd ffree(fp1);
365 1.50 thorpej fdremove(fdp, sv[0]);
366 1.57 lukem free2:
367 1.1 cgd (void)soclose(so2);
368 1.57 lukem free1:
369 1.1 cgd (void)soclose(so1);
370 1.1 cgd return (error);
371 1.1 cgd }
372 1.1 cgd
373 1.7 mycroft int
374 1.57 lukem sys_sendto(struct proc *p, void *v, register_t *retval)
375 1.15 thorpej {
376 1.51 augustss struct sys_sendto_args /* {
377 1.57 lukem syscallarg(int) s;
378 1.57 lukem syscallarg(const void *) buf;
379 1.57 lukem syscallarg(size_t) len;
380 1.57 lukem syscallarg(int) flags;
381 1.57 lukem syscallarg(const struct sockaddr *) to;
382 1.57 lukem syscallarg(unsigned int) tolen;
383 1.15 thorpej } */ *uap = v;
384 1.57 lukem struct msghdr msg;
385 1.57 lukem struct iovec aiov;
386 1.1 cgd
387 1.23 cgd msg.msg_name = (caddr_t)SCARG(uap, to); /* XXX kills const */
388 1.9 cgd msg.msg_namelen = SCARG(uap, tolen);
389 1.1 cgd msg.msg_iov = &aiov;
390 1.1 cgd msg.msg_iovlen = 1;
391 1.1 cgd msg.msg_control = 0;
392 1.1 cgd msg.msg_flags = 0;
393 1.23 cgd aiov.iov_base = (char *)SCARG(uap, buf); /* XXX kills const */
394 1.9 cgd aiov.iov_len = SCARG(uap, len);
395 1.63 jdolecek return (sendit(p, SCARG(uap, s), &msg, SCARG(uap, flags), retval));
396 1.1 cgd }
397 1.1 cgd
398 1.7 mycroft int
399 1.57 lukem sys_sendmsg(struct proc *p, void *v, register_t *retval)
400 1.15 thorpej {
401 1.51 augustss struct sys_sendmsg_args /* {
402 1.57 lukem syscallarg(int) s;
403 1.57 lukem syscallarg(const struct msghdr *) msg;
404 1.57 lukem syscallarg(int) flags;
405 1.15 thorpej } */ *uap = v;
406 1.57 lukem struct msghdr msg;
407 1.57 lukem struct iovec aiov[UIO_SMALLIOV], *iov;
408 1.57 lukem int error;
409 1.1 cgd
410 1.34 perry error = copyin(SCARG(uap, msg), (caddr_t)&msg, sizeof(msg));
411 1.18 christos if (error)
412 1.1 cgd return (error);
413 1.41 kleink if ((unsigned int)msg.msg_iovlen > UIO_SMALLIOV) {
414 1.41 kleink if ((unsigned int)msg.msg_iovlen > IOV_MAX)
415 1.1 cgd return (EMSGSIZE);
416 1.54 thorpej iov = malloc(sizeof(struct iovec) * msg.msg_iovlen,
417 1.54 thorpej M_IOV, M_WAITOK);
418 1.39 mycroft } else
419 1.1 cgd iov = aiov;
420 1.41 kleink if ((unsigned int)msg.msg_iovlen > 0) {
421 1.39 mycroft error = copyin((caddr_t)msg.msg_iov, (caddr_t)iov,
422 1.39 mycroft (size_t)(msg.msg_iovlen * sizeof(struct iovec)));
423 1.39 mycroft if (error)
424 1.39 mycroft goto done;
425 1.39 mycroft }
426 1.1 cgd msg.msg_iov = iov;
427 1.1 cgd msg.msg_flags = 0;
428 1.63 jdolecek error = sendit(p, SCARG(uap, s), &msg, SCARG(uap, flags), retval);
429 1.1 cgd done:
430 1.1 cgd if (iov != aiov)
431 1.54 thorpej free(iov, M_IOV);
432 1.1 cgd return (error);
433 1.1 cgd }
434 1.1 cgd
435 1.7 mycroft int
436 1.63 jdolecek sendit(struct proc *p, int s, struct msghdr *mp, int flags, register_t *retsize)
437 1.1 cgd {
438 1.57 lukem struct file *fp;
439 1.57 lukem struct uio auio;
440 1.57 lukem struct iovec *iov;
441 1.57 lukem int i, len, error;
442 1.57 lukem struct mbuf *to, *control;
443 1.57 lukem struct socket *so;
444 1.1 cgd #ifdef KTRACE
445 1.57 lukem struct iovec *ktriov;
446 1.1 cgd #endif
447 1.1 cgd
448 1.57 lukem #ifdef KTRACE
449 1.57 lukem ktriov = NULL;
450 1.57 lukem #endif
451 1.43 thorpej /* getsock() will use the descriptor for us */
452 1.18 christos if ((error = getsock(p->p_fd, s, &fp)) != 0)
453 1.1 cgd return (error);
454 1.1 cgd auio.uio_iov = mp->msg_iov;
455 1.1 cgd auio.uio_iovcnt = mp->msg_iovlen;
456 1.1 cgd auio.uio_segflg = UIO_USERSPACE;
457 1.1 cgd auio.uio_rw = UIO_WRITE;
458 1.1 cgd auio.uio_procp = p;
459 1.1 cgd auio.uio_offset = 0; /* XXX */
460 1.1 cgd auio.uio_resid = 0;
461 1.1 cgd iov = mp->msg_iov;
462 1.1 cgd for (i = 0; i < mp->msg_iovlen; i++, iov++) {
463 1.18 christos #if 0
464 1.18 christos /* cannot happen; iov_len is unsigned */
465 1.43 thorpej if (iov->iov_len < 0) {
466 1.43 thorpej error = EINVAL;
467 1.43 thorpej goto out;
468 1.43 thorpej }
469 1.18 christos #endif
470 1.33 thorpej /*
471 1.33 thorpej * Writes return ssize_t because -1 is returned on error.
472 1.33 thorpej * Therefore, we must restrict the length to SSIZE_MAX to
473 1.33 thorpej * avoid garbage return values.
474 1.33 thorpej */
475 1.33 thorpej auio.uio_resid += iov->iov_len;
476 1.43 thorpej if (iov->iov_len > SSIZE_MAX || auio.uio_resid > SSIZE_MAX) {
477 1.43 thorpej error = EINVAL;
478 1.43 thorpej goto out;
479 1.43 thorpej }
480 1.1 cgd }
481 1.1 cgd if (mp->msg_name) {
482 1.63 jdolecek error = sockargs(&to, mp->msg_name, mp->msg_namelen,
483 1.18 christos MT_SONAME);
484 1.63 jdolecek if (error)
485 1.63 jdolecek goto out;
486 1.1 cgd } else
487 1.1 cgd to = 0;
488 1.1 cgd if (mp->msg_control) {
489 1.65 jdolecek if (mp->msg_controllen < sizeof(struct cmsghdr)) {
490 1.1 cgd error = EINVAL;
491 1.1 cgd goto bad;
492 1.1 cgd }
493 1.18 christos error = sockargs(&control, mp->msg_control,
494 1.18 christos mp->msg_controllen, MT_CONTROL);
495 1.18 christos if (error)
496 1.1 cgd goto bad;
497 1.1 cgd } else
498 1.1 cgd control = 0;
499 1.1 cgd #ifdef KTRACE
500 1.1 cgd if (KTRPOINT(p, KTR_GENIO)) {
501 1.34 perry int iovlen = auio.uio_iovcnt * sizeof(struct iovec);
502 1.1 cgd
503 1.54 thorpej ktriov = malloc(iovlen, M_TEMP, M_WAITOK);
504 1.36 perry memcpy((caddr_t)ktriov, (caddr_t)auio.uio_iov, iovlen);
505 1.1 cgd }
506 1.1 cgd #endif
507 1.1 cgd len = auio.uio_resid;
508 1.30 matt so = (struct socket *)fp->f_data;
509 1.30 matt error = (*so->so_send)(so, to, &auio, NULL, control, flags);
510 1.18 christos if (error) {
511 1.1 cgd if (auio.uio_resid != len && (error == ERESTART ||
512 1.1 cgd error == EINTR || error == EWOULDBLOCK))
513 1.1 cgd error = 0;
514 1.1 cgd if (error == EPIPE)
515 1.1 cgd psignal(p, SIGPIPE);
516 1.1 cgd }
517 1.1 cgd if (error == 0)
518 1.1 cgd *retsize = len - auio.uio_resid;
519 1.1 cgd #ifdef KTRACE
520 1.1 cgd if (ktriov != NULL) {
521 1.1 cgd if (error == 0)
522 1.52 sommerfe ktrgenio(p, s, UIO_WRITE, ktriov, *retsize, error);
523 1.54 thorpej free(ktriov, M_TEMP);
524 1.1 cgd }
525 1.1 cgd #endif
526 1.43 thorpej bad:
527 1.1 cgd if (to)
528 1.1 cgd m_freem(to);
529 1.43 thorpej out:
530 1.43 thorpej FILE_UNUSE(fp, p);
531 1.1 cgd return (error);
532 1.1 cgd }
533 1.1 cgd
534 1.7 mycroft int
535 1.57 lukem sys_recvfrom(struct proc *p, void *v, register_t *retval)
536 1.15 thorpej {
537 1.51 augustss struct sys_recvfrom_args /* {
538 1.57 lukem syscallarg(int) s;
539 1.57 lukem syscallarg(void *) buf;
540 1.57 lukem syscallarg(size_t) len;
541 1.57 lukem syscallarg(int) flags;
542 1.57 lukem syscallarg(struct sockaddr *) from;
543 1.57 lukem syscallarg(unsigned int *) fromlenaddr;
544 1.15 thorpej } */ *uap = v;
545 1.57 lukem struct msghdr msg;
546 1.57 lukem struct iovec aiov;
547 1.57 lukem int error;
548 1.1 cgd
549 1.9 cgd if (SCARG(uap, fromlenaddr)) {
550 1.18 christos error = copyin((caddr_t)SCARG(uap, fromlenaddr),
551 1.18 christos (caddr_t)&msg.msg_namelen,
552 1.34 perry sizeof(msg.msg_namelen));
553 1.18 christos if (error)
554 1.1 cgd return (error);
555 1.1 cgd } else
556 1.1 cgd msg.msg_namelen = 0;
557 1.23 cgd msg.msg_name = (caddr_t)SCARG(uap, from);
558 1.1 cgd msg.msg_iov = &aiov;
559 1.1 cgd msg.msg_iovlen = 1;
560 1.9 cgd aiov.iov_base = SCARG(uap, buf);
561 1.9 cgd aiov.iov_len = SCARG(uap, len);
562 1.1 cgd msg.msg_control = 0;
563 1.9 cgd msg.msg_flags = SCARG(uap, flags);
564 1.9 cgd return (recvit(p, SCARG(uap, s), &msg,
565 1.63 jdolecek (caddr_t)SCARG(uap, fromlenaddr), retval));
566 1.1 cgd }
567 1.1 cgd
568 1.7 mycroft int
569 1.57 lukem sys_recvmsg(struct proc *p, void *v, register_t *retval)
570 1.15 thorpej {
571 1.51 augustss struct sys_recvmsg_args /* {
572 1.57 lukem syscallarg(int) s;
573 1.57 lukem syscallarg(struct msghdr *) msg;
574 1.57 lukem syscallarg(int) flags;
575 1.15 thorpej } */ *uap = v;
576 1.57 lukem struct msghdr msg;
577 1.57 lukem struct iovec aiov[UIO_SMALLIOV], *uiov, *iov;
578 1.57 lukem int error;
579 1.1 cgd
580 1.18 christos error = copyin((caddr_t)SCARG(uap, msg), (caddr_t)&msg,
581 1.34 perry sizeof(msg));
582 1.18 christos if (error)
583 1.1 cgd return (error);
584 1.41 kleink if ((unsigned int)msg.msg_iovlen > UIO_SMALLIOV) {
585 1.41 kleink if ((unsigned int)msg.msg_iovlen > IOV_MAX)
586 1.1 cgd return (EMSGSIZE);
587 1.54 thorpej iov = malloc(sizeof(struct iovec) * msg.msg_iovlen,
588 1.54 thorpej M_IOV, M_WAITOK);
589 1.39 mycroft } else
590 1.1 cgd iov = aiov;
591 1.41 kleink if ((unsigned int)msg.msg_iovlen > 0) {
592 1.39 mycroft error = copyin((caddr_t)msg.msg_iov, (caddr_t)iov,
593 1.39 mycroft (size_t)(msg.msg_iovlen * sizeof(struct iovec)));
594 1.39 mycroft if (error)
595 1.39 mycroft goto done;
596 1.39 mycroft }
597 1.39 mycroft uiov = msg.msg_iov;
598 1.39 mycroft msg.msg_iov = iov;
599 1.9 cgd msg.msg_flags = SCARG(uap, flags);
600 1.63 jdolecek if ((error = recvit(p, SCARG(uap, s), &msg, (caddr_t)0, retval)) == 0) {
601 1.1 cgd msg.msg_iov = uiov;
602 1.9 cgd error = copyout((caddr_t)&msg, (caddr_t)SCARG(uap, msg),
603 1.9 cgd sizeof(msg));
604 1.1 cgd }
605 1.1 cgd done:
606 1.1 cgd if (iov != aiov)
607 1.54 thorpej free(iov, M_IOV);
608 1.1 cgd return (error);
609 1.1 cgd }
610 1.1 cgd
611 1.7 mycroft int
612 1.57 lukem recvit(struct proc *p, int s, struct msghdr *mp, caddr_t namelenp,
613 1.63 jdolecek register_t *retsize)
614 1.1 cgd {
615 1.57 lukem struct file *fp;
616 1.57 lukem struct uio auio;
617 1.57 lukem struct iovec *iov;
618 1.57 lukem int i, len, error;
619 1.57 lukem struct mbuf *from, *control;
620 1.57 lukem struct socket *so;
621 1.57 lukem #ifdef KTRACE
622 1.57 lukem struct iovec *ktriov;
623 1.57 lukem #endif
624 1.57 lukem
625 1.57 lukem from = 0;
626 1.57 lukem control = 0;
627 1.1 cgd #ifdef KTRACE
628 1.57 lukem ktriov = NULL;
629 1.1 cgd #endif
630 1.1 cgd
631 1.43 thorpej /* getsock() will use the descriptor for us */
632 1.18 christos if ((error = getsock(p->p_fd, s, &fp)) != 0)
633 1.1 cgd return (error);
634 1.1 cgd auio.uio_iov = mp->msg_iov;
635 1.1 cgd auio.uio_iovcnt = mp->msg_iovlen;
636 1.1 cgd auio.uio_segflg = UIO_USERSPACE;
637 1.1 cgd auio.uio_rw = UIO_READ;
638 1.1 cgd auio.uio_procp = p;
639 1.1 cgd auio.uio_offset = 0; /* XXX */
640 1.1 cgd auio.uio_resid = 0;
641 1.1 cgd iov = mp->msg_iov;
642 1.1 cgd for (i = 0; i < mp->msg_iovlen; i++, iov++) {
643 1.18 christos #if 0
644 1.18 christos /* cannot happen iov_len is unsigned */
645 1.43 thorpej if (iov->iov_len < 0) {
646 1.43 thorpej error = EINVAL;
647 1.43 thorpej goto out1;
648 1.43 thorpej }
649 1.18 christos #endif
650 1.33 thorpej /*
651 1.33 thorpej * Reads return ssize_t because -1 is returned on error.
652 1.33 thorpej * Therefore we must restrict the length to SSIZE_MAX to
653 1.33 thorpej * avoid garbage return values.
654 1.33 thorpej */
655 1.33 thorpej auio.uio_resid += iov->iov_len;
656 1.43 thorpej if (iov->iov_len > SSIZE_MAX || auio.uio_resid > SSIZE_MAX) {
657 1.43 thorpej error = EINVAL;
658 1.43 thorpej goto out1;
659 1.43 thorpej }
660 1.1 cgd }
661 1.1 cgd #ifdef KTRACE
662 1.1 cgd if (KTRPOINT(p, KTR_GENIO)) {
663 1.34 perry int iovlen = auio.uio_iovcnt * sizeof(struct iovec);
664 1.1 cgd
665 1.54 thorpej ktriov = malloc(iovlen, M_TEMP, M_WAITOK);
666 1.36 perry memcpy((caddr_t)ktriov, (caddr_t)auio.uio_iov, iovlen);
667 1.1 cgd }
668 1.1 cgd #endif
669 1.1 cgd len = auio.uio_resid;
670 1.30 matt so = (struct socket *)fp->f_data;
671 1.30 matt error = (*so->so_receive)(so, &from, &auio, NULL,
672 1.30 matt mp->msg_control ? &control : NULL, &mp->msg_flags);
673 1.18 christos if (error) {
674 1.1 cgd if (auio.uio_resid != len && (error == ERESTART ||
675 1.1 cgd error == EINTR || error == EWOULDBLOCK))
676 1.1 cgd error = 0;
677 1.1 cgd }
678 1.1 cgd #ifdef KTRACE
679 1.1 cgd if (ktriov != NULL) {
680 1.1 cgd if (error == 0)
681 1.52 sommerfe ktrgenio(p, s, UIO_READ, ktriov,
682 1.52 sommerfe len - auio.uio_resid, error);
683 1.54 thorpej free(ktriov, M_TEMP);
684 1.1 cgd }
685 1.1 cgd #endif
686 1.1 cgd if (error)
687 1.1 cgd goto out;
688 1.1 cgd *retsize = len - auio.uio_resid;
689 1.1 cgd if (mp->msg_name) {
690 1.1 cgd len = mp->msg_namelen;
691 1.1 cgd if (len <= 0 || from == 0)
692 1.1 cgd len = 0;
693 1.1 cgd else {
694 1.1 cgd if (len > from->m_len)
695 1.1 cgd len = from->m_len;
696 1.1 cgd /* else if len < from->m_len ??? */
697 1.63 jdolecek error = copyout(mtod(from, caddr_t),
698 1.18 christos (caddr_t)mp->msg_name, (unsigned)len);
699 1.63 jdolecek if (error)
700 1.63 jdolecek goto out;
701 1.1 cgd }
702 1.1 cgd mp->msg_namelen = len;
703 1.1 cgd if (namelenp &&
704 1.65 jdolecek (error = copyout((caddr_t)&len, namelenp, sizeof(int))))
705 1.1 cgd goto out;
706 1.1 cgd }
707 1.1 cgd if (mp->msg_control) {
708 1.1 cgd len = mp->msg_controllen;
709 1.1 cgd if (len <= 0 || control == 0)
710 1.1 cgd len = 0;
711 1.1 cgd else {
712 1.26 thorpej struct mbuf *m = control;
713 1.26 thorpej caddr_t p = (caddr_t)mp->msg_control;
714 1.26 thorpej
715 1.28 thorpej do {
716 1.26 thorpej i = m->m_len;
717 1.26 thorpej if (len < i) {
718 1.26 thorpej mp->msg_flags |= MSG_CTRUNC;
719 1.26 thorpej i = len;
720 1.26 thorpej }
721 1.26 thorpej error = copyout(mtod(m, caddr_t), p,
722 1.26 thorpej (unsigned)i);
723 1.28 thorpej if (m->m_next)
724 1.28 thorpej i = ALIGN(i);
725 1.26 thorpej p += i;
726 1.26 thorpej len -= i;
727 1.26 thorpej if (error != 0 || len <= 0)
728 1.26 thorpej break;
729 1.28 thorpej } while ((m = m->m_next) != NULL);
730 1.26 thorpej len = p - (caddr_t)mp->msg_control;
731 1.1 cgd }
732 1.1 cgd mp->msg_controllen = len;
733 1.1 cgd }
734 1.43 thorpej out:
735 1.1 cgd if (from)
736 1.1 cgd m_freem(from);
737 1.1 cgd if (control)
738 1.1 cgd m_freem(control);
739 1.43 thorpej out1:
740 1.43 thorpej FILE_UNUSE(fp, p);
741 1.1 cgd return (error);
742 1.1 cgd }
743 1.1 cgd
744 1.1 cgd /* ARGSUSED */
745 1.7 mycroft int
746 1.57 lukem sys_shutdown(struct proc *p, void *v, register_t *retval)
747 1.15 thorpej {
748 1.51 augustss struct sys_shutdown_args /* {
749 1.57 lukem syscallarg(int) s;
750 1.57 lukem syscallarg(int) how;
751 1.15 thorpej } */ *uap = v;
752 1.57 lukem struct file *fp;
753 1.57 lukem int error;
754 1.1 cgd
755 1.43 thorpej /* getsock() will use the descriptor for us */
756 1.18 christos if ((error = getsock(p->p_fd, SCARG(uap, s), &fp)) != 0)
757 1.1 cgd return (error);
758 1.43 thorpej error = soshutdown((struct socket *)fp->f_data, SCARG(uap, how));
759 1.43 thorpej FILE_UNUSE(fp, p);
760 1.43 thorpej return (error);
761 1.1 cgd }
762 1.1 cgd
763 1.1 cgd /* ARGSUSED */
764 1.7 mycroft int
765 1.57 lukem sys_setsockopt(struct proc *p, void *v, register_t *retval)
766 1.15 thorpej {
767 1.51 augustss struct sys_setsockopt_args /* {
768 1.57 lukem syscallarg(int) s;
769 1.57 lukem syscallarg(int) level;
770 1.57 lukem syscallarg(int) name;
771 1.57 lukem syscallarg(const void *) val;
772 1.57 lukem syscallarg(unsigned int) valsize;
773 1.15 thorpej } */ *uap = v;
774 1.57 lukem struct file *fp;
775 1.57 lukem struct mbuf *m;
776 1.57 lukem int error;
777 1.1 cgd
778 1.57 lukem m = NULL;
779 1.43 thorpej /* getsock() will use the descriptor for us */
780 1.18 christos if ((error = getsock(p->p_fd, SCARG(uap, s), &fp)) != 0)
781 1.1 cgd return (error);
782 1.43 thorpej if (SCARG(uap, valsize) > MLEN) {
783 1.43 thorpej error = EINVAL;
784 1.43 thorpej goto out;
785 1.43 thorpej }
786 1.9 cgd if (SCARG(uap, val)) {
787 1.1 cgd m = m_get(M_WAIT, MT_SOOPTS);
788 1.18 christos error = copyin(SCARG(uap, val), mtod(m, caddr_t),
789 1.41 kleink SCARG(uap, valsize));
790 1.18 christos if (error) {
791 1.1 cgd (void) m_free(m);
792 1.43 thorpej goto out;
793 1.1 cgd }
794 1.9 cgd m->m_len = SCARG(uap, valsize);
795 1.1 cgd }
796 1.43 thorpej error = sosetopt((struct socket *)fp->f_data, SCARG(uap, level),
797 1.43 thorpej SCARG(uap, name), m);
798 1.43 thorpej out:
799 1.43 thorpej FILE_UNUSE(fp, p);
800 1.43 thorpej return (error);
801 1.1 cgd }
802 1.1 cgd
803 1.1 cgd /* ARGSUSED */
804 1.7 mycroft int
805 1.57 lukem sys_getsockopt(struct proc *p, void *v, register_t *retval)
806 1.15 thorpej {
807 1.51 augustss struct sys_getsockopt_args /* {
808 1.57 lukem syscallarg(int) s;
809 1.57 lukem syscallarg(int) level;
810 1.57 lukem syscallarg(int) name;
811 1.57 lukem syscallarg(void *) val;
812 1.57 lukem syscallarg(unsigned int *) avalsize;
813 1.15 thorpej } */ *uap = v;
814 1.57 lukem struct file *fp;
815 1.57 lukem struct mbuf *m, *m0;
816 1.57 lukem unsigned int op, i, valsize;
817 1.57 lukem int error;
818 1.1 cgd
819 1.57 lukem m = NULL;
820 1.43 thorpej /* getsock() will use the descriptor for us */
821 1.18 christos if ((error = getsock(p->p_fd, SCARG(uap, s), &fp)) != 0)
822 1.1 cgd return (error);
823 1.9 cgd if (SCARG(uap, val)) {
824 1.18 christos error = copyin((caddr_t)SCARG(uap, avalsize),
825 1.34 perry (caddr_t)&valsize, sizeof(valsize));
826 1.18 christos if (error)
827 1.43 thorpej goto out;
828 1.1 cgd } else
829 1.1 cgd valsize = 0;
830 1.9 cgd if ((error = sogetopt((struct socket *)fp->f_data, SCARG(uap, level),
831 1.9 cgd SCARG(uap, name), &m)) == 0 && SCARG(uap, val) && valsize &&
832 1.9 cgd m != NULL) {
833 1.45 itojun op = 0;
834 1.45 itojun while (m && !error && op < valsize) {
835 1.45 itojun i = min(m->m_len, (valsize - op));
836 1.45 itojun error = copyout(mtod(m, caddr_t), SCARG(uap, val), i);
837 1.45 itojun op += i;
838 1.45 itojun SCARG(uap, val) = ((u_int8_t *)SCARG(uap, val)) + i;
839 1.45 itojun m0 = m;
840 1.45 itojun MFREE(m0, m);
841 1.45 itojun }
842 1.45 itojun valsize = op;
843 1.1 cgd if (error == 0)
844 1.45 itojun error = copyout(&valsize,
845 1.45 itojun SCARG(uap, avalsize), sizeof(valsize));
846 1.1 cgd }
847 1.1 cgd if (m != NULL)
848 1.1 cgd (void) m_free(m);
849 1.43 thorpej out:
850 1.43 thorpej FILE_UNUSE(fp, p);
851 1.1 cgd return (error);
852 1.1 cgd }
853 1.1 cgd
854 1.60 jdolecek #ifndef NEW_PIPE
855 1.1 cgd /* ARGSUSED */
856 1.7 mycroft int
857 1.57 lukem sys_pipe(struct proc *p, void *v, register_t *retval)
858 1.1 cgd {
859 1.57 lukem struct filedesc *fdp;
860 1.57 lukem struct file *rf, *wf;
861 1.57 lukem struct socket *rso, *wso;
862 1.57 lukem int fd, error;
863 1.1 cgd
864 1.57 lukem fdp = p->p_fd;
865 1.32 lukem if ((error = socreate(AF_LOCAL, &rso, SOCK_STREAM, 0)) != 0)
866 1.1 cgd return (error);
867 1.32 lukem if ((error = socreate(AF_LOCAL, &wso, SOCK_STREAM, 0)) != 0)
868 1.1 cgd goto free1;
869 1.58 manu /* remember this socket pair implements a pipe */
870 1.58 manu wso->so_state |= SS_ISAPIPE;
871 1.58 manu rso->so_state |= SS_ISAPIPE;
872 1.43 thorpej /* falloc() will use the descriptor for us */
873 1.18 christos if ((error = falloc(p, &rf, &fd)) != 0)
874 1.1 cgd goto free2;
875 1.1 cgd retval[0] = fd;
876 1.1 cgd rf->f_flag = FREAD;
877 1.1 cgd rf->f_type = DTYPE_SOCKET;
878 1.1 cgd rf->f_ops = &socketops;
879 1.1 cgd rf->f_data = (caddr_t)rso;
880 1.18 christos if ((error = falloc(p, &wf, &fd)) != 0)
881 1.1 cgd goto free3;
882 1.1 cgd wf->f_flag = FWRITE;
883 1.1 cgd wf->f_type = DTYPE_SOCKET;
884 1.1 cgd wf->f_ops = &socketops;
885 1.1 cgd wf->f_data = (caddr_t)wso;
886 1.1 cgd retval[1] = fd;
887 1.18 christos if ((error = unp_connect2(wso, rso)) != 0)
888 1.1 cgd goto free4;
889 1.59 thorpej FILE_SET_MATURE(rf);
890 1.59 thorpej FILE_SET_MATURE(wf);
891 1.43 thorpej FILE_UNUSE(rf, p);
892 1.43 thorpej FILE_UNUSE(wf, p);
893 1.1 cgd return (0);
894 1.57 lukem free4:
895 1.43 thorpej FILE_UNUSE(wf, p);
896 1.1 cgd ffree(wf);
897 1.50 thorpej fdremove(fdp, retval[1]);
898 1.57 lukem free3:
899 1.43 thorpej FILE_UNUSE(rf, p);
900 1.1 cgd ffree(rf);
901 1.50 thorpej fdremove(fdp, retval[0]);
902 1.57 lukem free2:
903 1.1 cgd (void)soclose(wso);
904 1.57 lukem free1:
905 1.1 cgd (void)soclose(rso);
906 1.1 cgd return (error);
907 1.1 cgd }
908 1.60 jdolecek #endif /* !NEW_PIPE */
909 1.1 cgd
910 1.1 cgd /*
911 1.1 cgd * Get socket name.
912 1.1 cgd */
913 1.13 christos /* ARGSUSED */
914 1.7 mycroft int
915 1.57 lukem sys_getsockname(struct proc *p, void *v, register_t *retval)
916 1.15 thorpej {
917 1.51 augustss struct sys_getsockname_args /* {
918 1.57 lukem syscallarg(int) fdes;
919 1.57 lukem syscallarg(struct sockaddr *) asa;
920 1.57 lukem syscallarg(unsigned int *) alen;
921 1.15 thorpej } */ *uap = v;
922 1.57 lukem struct file *fp;
923 1.57 lukem struct socket *so;
924 1.57 lukem struct mbuf *m;
925 1.57 lukem unsigned int len;
926 1.57 lukem int error;
927 1.1 cgd
928 1.43 thorpej /* getsock() will use the descriptor for us */
929 1.18 christos if ((error = getsock(p->p_fd, SCARG(uap, fdes), &fp)) != 0)
930 1.1 cgd return (error);
931 1.34 perry error = copyin((caddr_t)SCARG(uap, alen), (caddr_t)&len, sizeof(len));
932 1.18 christos if (error)
933 1.43 thorpej goto out;
934 1.1 cgd so = (struct socket *)fp->f_data;
935 1.1 cgd m = m_getclr(M_WAIT, MT_SONAME);
936 1.21 mycroft error = (*so->so_proto->pr_usrreq)(so, PRU_SOCKADDR, (struct mbuf *)0,
937 1.21 mycroft m, (struct mbuf *)0, (struct proc *)0);
938 1.18 christos if (error)
939 1.1 cgd goto bad;
940 1.1 cgd if (len > m->m_len)
941 1.1 cgd len = m->m_len;
942 1.41 kleink error = copyout(mtod(m, caddr_t), (caddr_t)SCARG(uap, asa), len);
943 1.1 cgd if (error == 0)
944 1.9 cgd error = copyout((caddr_t)&len, (caddr_t)SCARG(uap, alen),
945 1.34 perry sizeof(len));
946 1.43 thorpej bad:
947 1.1 cgd m_freem(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 /*
954 1.1 cgd * Get name of peer for connected socket.
955 1.1 cgd */
956 1.13 christos /* ARGSUSED */
957 1.7 mycroft int
958 1.57 lukem sys_getpeername(struct proc *p, void *v, register_t *retval)
959 1.15 thorpej {
960 1.51 augustss struct sys_getpeername_args /* {
961 1.57 lukem syscallarg(int) fdes;
962 1.57 lukem syscallarg(struct sockaddr *) asa;
963 1.57 lukem syscallarg(unsigned int *) alen;
964 1.15 thorpej } */ *uap = v;
965 1.57 lukem struct file *fp;
966 1.57 lukem struct socket *so;
967 1.57 lukem struct mbuf *m;
968 1.57 lukem unsigned int len;
969 1.57 lukem int error;
970 1.1 cgd
971 1.43 thorpej /* getsock() will use the descriptor for us */
972 1.18 christos if ((error = getsock(p->p_fd, SCARG(uap, fdes), &fp)) != 0)
973 1.1 cgd return (error);
974 1.1 cgd so = (struct socket *)fp->f_data;
975 1.43 thorpej if ((so->so_state & (SS_ISCONNECTED|SS_ISCONFIRMING)) == 0) {
976 1.43 thorpej error = ENOTCONN;
977 1.43 thorpej goto out;
978 1.43 thorpej }
979 1.34 perry error = copyin((caddr_t)SCARG(uap, alen), (caddr_t)&len, sizeof(len));
980 1.18 christos if (error)
981 1.43 thorpej goto out;
982 1.1 cgd m = m_getclr(M_WAIT, MT_SONAME);
983 1.21 mycroft error = (*so->so_proto->pr_usrreq)(so, PRU_PEERADDR, (struct mbuf *)0,
984 1.21 mycroft m, (struct mbuf *)0, (struct proc *)0);
985 1.18 christos if (error)
986 1.1 cgd goto bad;
987 1.1 cgd if (len > m->m_len)
988 1.1 cgd len = m->m_len;
989 1.41 kleink error = copyout(mtod(m, caddr_t), (caddr_t)SCARG(uap, asa), len);
990 1.18 christos if (error)
991 1.1 cgd goto bad;
992 1.34 perry error = copyout((caddr_t)&len, (caddr_t)SCARG(uap, alen), sizeof(len));
993 1.43 thorpej bad:
994 1.1 cgd m_freem(m);
995 1.43 thorpej out:
996 1.43 thorpej FILE_UNUSE(fp, p);
997 1.1 cgd return (error);
998 1.1 cgd }
999 1.1 cgd
1000 1.24 thorpej /*
1001 1.24 thorpej * XXX In a perfect world, we wouldn't pass around socket control
1002 1.24 thorpej * XXX arguments in mbufs, and this could go away.
1003 1.24 thorpej */
1004 1.7 mycroft int
1005 1.64 matt sockargs(struct mbuf **mp, const void *buf, size_t buflen, int type)
1006 1.1 cgd {
1007 1.57 lukem struct sockaddr *sa;
1008 1.57 lukem struct mbuf *m;
1009 1.57 lukem int error;
1010 1.1 cgd
1011 1.24 thorpej /*
1012 1.25 thorpej * We can't allow socket names > UCHAR_MAX in length, since that
1013 1.64 matt * will overflow sa_len. Control data more than a page size in
1014 1.64 matt * length is just too much.
1015 1.24 thorpej */
1016 1.64 matt if (buflen > (type == MT_SONAME ? UCHAR_MAX : PAGE_SIZE))
1017 1.24 thorpej return (EINVAL);
1018 1.24 thorpej
1019 1.24 thorpej /* Allocate an mbuf to hold the arguments. */
1020 1.24 thorpej m = m_get(M_WAIT, type);
1021 1.64 matt if (buflen > MLEN) {
1022 1.24 thorpej /*
1023 1.24 thorpej * Won't fit into a regular mbuf, so we allocate just
1024 1.24 thorpej * enough external storage to hold the argument.
1025 1.24 thorpej */
1026 1.24 thorpej MEXTMALLOC(m, buflen, M_WAITOK);
1027 1.1 cgd }
1028 1.1 cgd m->m_len = buflen;
1029 1.64 matt error = copyin(buf, mtod(m, caddr_t), buflen);
1030 1.1 cgd if (error) {
1031 1.1 cgd (void) m_free(m);
1032 1.7 mycroft return (error);
1033 1.1 cgd }
1034 1.1 cgd *mp = m;
1035 1.1 cgd if (type == MT_SONAME) {
1036 1.7 mycroft sa = mtod(m, struct sockaddr *);
1037 1.65 jdolecek #if BYTE_ORDER != BIG_ENDIAN
1038 1.65 jdolecek /*
1039 1.65 jdolecek * 4.3BSD compat thing - need to stay, since bind(2),
1040 1.65 jdolecek * connect(2), sendto(2) were not versioned for COMPAT_43.
1041 1.65 jdolecek */
1042 1.1 cgd if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
1043 1.1 cgd sa->sa_family = sa->sa_len;
1044 1.1 cgd #endif
1045 1.1 cgd sa->sa_len = buflen;
1046 1.1 cgd }
1047 1.1 cgd return (0);
1048 1.1 cgd }
1049 1.1 cgd
1050 1.7 mycroft int
1051 1.57 lukem getsock(struct filedesc *fdp, int fdes, struct file **fpp)
1052 1.1 cgd {
1053 1.57 lukem struct file *fp;
1054 1.1 cgd
1055 1.59 thorpej if ((fp = fd_getfile(fdp, fdes)) == NULL)
1056 1.1 cgd return (EBADF);
1057 1.43 thorpej
1058 1.43 thorpej FILE_USE(fp);
1059 1.43 thorpej
1060 1.43 thorpej if (fp->f_type != DTYPE_SOCKET) {
1061 1.43 thorpej FILE_UNUSE(fp, NULL);
1062 1.1 cgd return (ENOTSOCK);
1063 1.43 thorpej }
1064 1.1 cgd *fpp = fp;
1065 1.1 cgd return (0);
1066 1.1 cgd }
1067