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