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