network - Move socket from netmsg ext to netmsg header, add port to socket
[dragonfly.git] / sys / kern / uipc_syscalls.c
CommitLineData
984263bc
MD
1/*
2 * Copyright (c) 1982, 1986, 1989, 1990, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * sendfile(2) and related extensions:
6 * Copyright (c) 1998, David Greenman. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * @(#)uipc_syscalls.c 8.4 (Berkeley) 2/21/94
37 * $FreeBSD: src/sys/kern/uipc_syscalls.c,v 1.65.2.17 2003/04/04 17:11:16 tegge Exp $
aca22a94 38 * $DragonFly: src/sys/kern/uipc_syscalls.c,v 1.92 2008/11/26 13:10:56 sephe Exp $
984263bc
MD
39 */
40
984263bc 41#include "opt_ktrace.h"
78812139 42#include "opt_sctp.h"
984263bc
MD
43
44#include <sys/param.h>
45#include <sys/systm.h>
46#include <sys/kernel.h>
47#include <sys/sysproto.h>
48#include <sys/malloc.h>
49#include <sys/filedesc.h>
50#include <sys/event.h>
51#include <sys/proc.h>
52#include <sys/fcntl.h>
53#include <sys/file.h>
54#include <sys/filio.h>
5969a6f1 55#include <sys/kern_syscall.h>
984263bc
MD
56#include <sys/mbuf.h>
57#include <sys/protosw.h>
4860553a 58#include <sys/sfbuf.h>
984263bc
MD
59#include <sys/socket.h>
60#include <sys/socketvar.h>
6b6e0885 61#include <sys/socketops.h>
984263bc
MD
62#include <sys/uio.h>
63#include <sys/vnode.h>
64#include <sys/lock.h>
65#include <sys/mount.h>
66#ifdef KTRACE
67#include <sys/ktrace.h>
68#endif
69#include <vm/vm.h>
70#include <vm/vm_object.h>
71#include <vm/vm_page.h>
72#include <vm/vm_pageout.h>
73#include <vm/vm_kern.h>
74#include <vm/vm_extern.h>
dadab5e9 75#include <sys/file2.h>
770d4c4d 76#include <sys/signalvar.h>
df8d1020 77#include <sys/serialize.h>
984263bc 78
b44419cb
MD
79#include <sys/thread2.h>
80#include <sys/msgport2.h>
d6cb521d 81#include <sys/socketvar2.h>
4599cf19 82#include <net/netmsg2.h>
b44419cb 83
78812139
EN
84#ifdef SCTP
85#include <netinet/sctp_peeloff.h>
86#endif /* SCTP */
87
e66bab2b
MD
88struct sfbuf_mref {
89 struct sf_buf *sf;
90 int mref_count;
91};
92
93static MALLOC_DEFINE(M_SENDFILE, "sendfile", "sendfile sfbuf ref structures");
94
984263bc
MD
95/*
96 * System call interface to the socket abstraction.
97 */
984263bc
MD
98
99extern struct fileops socketops;
100
41c20dac
MD
101/*
102 * socket_args(int domain, int type, int protocol)
103 */
984263bc 104int
75a872f8 105kern_socket(int domain, int type, int protocol, int *res)
984263bc 106{
dadab5e9
MD
107 struct thread *td = curthread;
108 struct proc *p = td->td_proc;
984263bc
MD
109 struct socket *so;
110 struct file *fp;
111 int fd, error;
112
dadab5e9 113 KKASSERT(p);
dadab5e9 114
984263bc
MD
115 error = falloc(p, &fp, &fd);
116 if (error)
117 return (error);
75a872f8 118 error = socreate(domain, &so, type, protocol, td);
984263bc 119 if (error) {
259b8ea0 120 fsetfd(p, NULL, fd);
984263bc 121 } else {
984263bc 122 fp->f_type = DTYPE_SOCKET;
fbb4eeab
JH
123 fp->f_flag = FREAD | FWRITE;
124 fp->f_ops = &socketops;
125 fp->f_data = so;
75a872f8 126 *res = fd;
259b8ea0 127 fsetfd(p, fp, fd);
984263bc 128 }
9f87144f 129 fdrop(fp);
984263bc
MD
130 return (error);
131}
132
75a872f8 133int
753fd850 134sys_socket(struct socket_args *uap)
75a872f8
DRJ
135{
136 int error;
137
138 error = kern_socket(uap->domain, uap->type, uap->protocol,
e54488bb 139 &uap->sysmsg_iresult);
75a872f8
DRJ
140
141 return (error);
142}
6b6e0885 143
02844a31 144int
5969a6f1 145kern_bind(int s, struct sockaddr *sa)
984263bc 146{
dadab5e9
MD
147 struct thread *td = curthread;
148 struct proc *p = td->td_proc;
984263bc 149 struct file *fp;
984263bc
MD
150 int error;
151
dadab5e9 152 KKASSERT(p);
d83b97b9 153 error = holdsock(p->p_fd, s, &fp);
984263bc
MD
154 if (error)
155 return (error);
d83b97b9 156 error = sobind((struct socket *)fp->f_data, sa, td);
9f87144f 157 fdrop(fp);
d83b97b9
MD
158 return (error);
159}
160
161/*
162 * bind_args(int s, caddr_t name, int namelen)
163 */
164int
753fd850 165sys_bind(struct bind_args *uap)
d83b97b9
MD
166{
167 struct sockaddr *sa;
168 int error;
169
984263bc 170 error = getsockaddr(&sa, uap->name, uap->namelen);
d83b97b9 171 if (error)
984263bc 172 return (error);
5969a6f1 173 error = kern_bind(uap->s, sa);
984263bc 174 FREE(sa, M_SONAME);
d83b97b9 175
984263bc
MD
176 return (error);
177}
178
984263bc 179int
5969a6f1 180kern_listen(int s, int backlog)
984263bc 181{
dadab5e9
MD
182 struct thread *td = curthread;
183 struct proc *p = td->td_proc;
984263bc
MD
184 struct file *fp;
185 int error;
186
dadab5e9 187 KKASSERT(p);
5969a6f1 188 error = holdsock(p->p_fd, s, &fp);
984263bc
MD
189 if (error)
190 return (error);
5969a6f1 191 error = solisten((struct socket *)fp->f_data, backlog, td);
9f87144f 192 fdrop(fp);
984263bc
MD
193 return(error);
194}
195
41c20dac 196/*
5969a6f1
DRJ
197 * listen_args(int s, int backlog)
198 */
199int
753fd850 200sys_listen(struct listen_args *uap)
5969a6f1
DRJ
201{
202 int error;
203
204 error = kern_listen(uap->s, uap->backlog);
205 return (error);
206}
207
f172717f
JH
208/*
209 * Returns the accepted socket as well.
210 */
211static boolean_t
212soaccept_predicate(struct netmsg *msg0)
213{
214 struct netmsg_so_notify *msg = (struct netmsg_so_notify *)msg0;
215 struct socket *head = msg->nm_so;
216
217 if (head->so_error != 0) {
4599cf19 218 msg->nm_netmsg.nm_lmsg.ms_error = head->so_error;
f172717f
JH
219 return (TRUE);
220 }
221 if (!TAILQ_EMPTY(&head->so_comp)) {
222 /* Abuse nm_so field as copy in/copy out parameter. XXX JH */
223 msg->nm_so = TAILQ_FIRST(&head->so_comp);
224 TAILQ_REMOVE(&head->so_comp, msg->nm_so, so_list);
225 head->so_qlen--;
226
4599cf19 227 msg->nm_netmsg.nm_lmsg.ms_error = 0;
f172717f
JH
228 return (TRUE);
229 }
230 if (head->so_state & SS_CANTRCVMORE) {
4599cf19 231 msg->nm_netmsg.nm_lmsg.ms_error = ECONNABORTED;
f172717f
JH
232 return (TRUE);
233 }
9ba76b73 234 if (msg->nm_fflags & FNONBLOCK) {
4599cf19 235 msg->nm_netmsg.nm_lmsg.ms_error = EWOULDBLOCK;
f172717f
JH
236 return (TRUE);
237 }
238
239 return (FALSE);
240}
241
5969a6f1
DRJ
242/*
243 * The second argument to kern_accept() is a handle to a struct sockaddr.
244 * This allows kern_accept() to return a pointer to an allocated struct
d83b97b9
MD
245 * sockaddr which must be freed later with FREE(). The caller must
246 * initialize *name to NULL.
41c20dac 247 */
02844a31 248int
358e1f78 249kern_accept(int s, int fflags, struct sockaddr **name, int *namelen, int *res)
984263bc 250{
dadab5e9
MD
251 struct thread *td = curthread;
252 struct proc *p = td->td_proc;
984263bc
MD
253 struct file *lfp = NULL;
254 struct file *nfp = NULL;
255 struct sockaddr *sa;
984263bc 256 struct socket *head, *so;
f172717f 257 struct netmsg_so_notify msg;
984263bc
MD
258 int fd;
259 u_int fflag; /* type must match fp->f_flag */
f172717f 260 int error, tmp;
984263bc 261
259b8ea0 262 *res = -1;
d83b97b9
MD
263 if (name && namelen && *namelen < 0)
264 return (EINVAL);
265
fa541be6 266 error = holdsock(p->p_fd, s, &lfp);
984263bc
MD
267 if (error)
268 return (error);
f172717f
JH
269
270 error = falloc(p, &nfp, &fd);
271 if (error) { /* Probably ran out of file descriptors. */
9f87144f 272 fdrop(lfp);
f172717f
JH
273 return (error);
274 }
984263bc
MD
275 head = (struct socket *)lfp->f_data;
276 if ((head->so_options & SO_ACCEPTCONN) == 0) {
984263bc
MD
277 error = EINVAL;
278 goto done;
279 }
f172717f 280
358e1f78
MD
281 if (fflags & O_FBLOCKING)
282 fflags |= lfp->f_flag & ~FNONBLOCK;
283 else if (fflags & O_FNONBLOCKING)
284 fflags |= lfp->f_flag | FNONBLOCK;
285 else
286 fflags = lfp->f_flag;
287
f172717f 288 /* optimize for uniprocessor case later XXX JH */
48e7b118
MD
289 netmsg_init_abortable(&msg.nm_netmsg, head, &curthread->td_msgport,
290 0, netmsg_so_notify, netmsg_so_notify_doabort);
f172717f 291 msg.nm_predicate = soaccept_predicate;
358e1f78 292 msg.nm_fflags = fflags;
f172717f
JH
293 msg.nm_so = head;
294 msg.nm_etype = NM_REVENT;
48e7b118 295 error = lwkt_domsg(head->so_port, &msg.nm_netmsg.nm_lmsg, PCATCH);
f172717f 296 if (error)
984263bc 297 goto done;
984263bc
MD
298
299 /*
f172717f 300 * At this point we have the connection that's ready to be accepted.
984263bc 301 */
f172717f 302 so = msg.nm_so;
984263bc
MD
303
304 fflag = lfp->f_flag;
984263bc
MD
305
306 /* connection has been removed from the listen queue */
6d49aa6f 307 KNOTE(&head->so_rcv.ssb_sel.si_note, 0);
984263bc
MD
308
309 so->so_state &= ~SS_COMP;
310 so->so_head = NULL;
311 if (head->so_sigio != NULL)
312 fsetown(fgetown(head->so_sigio), &so->so_sigio);
313
fbb4eeab 314 nfp->f_type = DTYPE_SOCKET;
984263bc
MD
315 nfp->f_flag = fflag;
316 nfp->f_ops = &socketops;
fbb4eeab 317 nfp->f_data = so;
984263bc
MD
318 /* Sync socket nonblocking/async state with file flags */
319 tmp = fflag & FNONBLOCK;
87baaf0c 320 fo_ioctl(nfp, FIONBIO, (caddr_t)&tmp, p->p_ucred, NULL);
984263bc 321 tmp = fflag & FASYNC;
87baaf0c 322 fo_ioctl(nfp, FIOASYNC, (caddr_t)&tmp, p->p_ucred, NULL);
d83b97b9
MD
323
324 sa = NULL;
984263bc 325 error = soaccept(so, &sa);
d83b97b9
MD
326
327 /*
328 * Set the returned name and namelen as applicable. Set the returned
329 * namelen to 0 for older code which might ignore the return value
330 * from accept.
331 */
332 if (error == 0) {
333 if (sa && name && namelen) {
334 if (*namelen > sa->sa_len)
335 *namelen = sa->sa_len;
336 *name = sa;
337 } else {
338 if (sa)
339 FREE(sa, M_SONAME);
984263bc 340 }
984263bc 341 }
984263bc 342
f172717f 343done:
984263bc 344 /*
259b8ea0
MD
345 * If an error occured clear the reserved descriptor, else associate
346 * nfp with it.
347 *
348 * Note that *res is normally ignored if an error is returned but
349 * a syscall message will still have access to the result code.
984263bc
MD
350 */
351 if (error) {
259b8ea0
MD
352 fsetfd(p, NULL, fd);
353 } else {
354 *res = fd;
355 fsetfd(p, nfp, fd);
984263bc 356 }
259b8ea0 357 fdrop(nfp);
9f87144f 358 fdrop(lfp);
984263bc
MD
359 return (error);
360}
361
d83b97b9 362/*
358e1f78 363 * accept(int s, caddr_t name, int *anamelen)
d83b97b9 364 */
984263bc 365int
753fd850 366sys_accept(struct accept_args *uap)
984263bc 367{
d83b97b9
MD
368 struct sockaddr *sa = NULL;
369 int sa_len;
370 int error;
371
372 if (uap->name) {
373 error = copyin(uap->anamelen, &sa_len, sizeof(sa_len));
374 if (error)
375 return (error);
376
e54488bb
MD
377 error = kern_accept(uap->s, 0, &sa, &sa_len,
378 &uap->sysmsg_iresult);
358e1f78
MD
379
380 if (error == 0)
381 error = copyout(sa, uap->name, sa_len);
382 if (error == 0) {
383 error = copyout(&sa_len, uap->anamelen,
384 sizeof(*uap->anamelen));
385 }
386 if (sa)
387 FREE(sa, M_SONAME);
388 } else {
e54488bb
MD
389 error = kern_accept(uap->s, 0, NULL, 0,
390 &uap->sysmsg_iresult);
358e1f78
MD
391 }
392 return (error);
393}
394
395/*
b09fd398 396 * extaccept(int s, int fflags, caddr_t name, int *anamelen)
358e1f78
MD
397 */
398int
b09fd398 399sys_extaccept(struct extaccept_args *uap)
358e1f78
MD
400{
401 struct sockaddr *sa = NULL;
402 int sa_len;
403 int error;
404 int fflags = uap->flags & O_FMASK;
405
406 if (uap->name) {
407 error = copyin(uap->anamelen, &sa_len, sizeof(sa_len));
408 if (error)
409 return (error);
410
e54488bb
MD
411 error = kern_accept(uap->s, fflags, &sa, &sa_len,
412 &uap->sysmsg_iresult);
d83b97b9
MD
413
414 if (error == 0)
415 error = copyout(sa, uap->name, sa_len);
416 if (error == 0) {
417 error = copyout(&sa_len, uap->anamelen,
418 sizeof(*uap->anamelen));
419 }
420 if (sa)
421 FREE(sa, M_SONAME);
422 } else {
e54488bb
MD
423 error = kern_accept(uap->s, fflags, NULL, 0,
424 &uap->sysmsg_iresult);
d83b97b9
MD
425 }
426 return (error);
984263bc
MD
427}
428
358e1f78 429
b44419cb
MD
430/*
431 * Returns TRUE if predicate satisfied.
432 */
433static boolean_t
434soconnected_predicate(struct netmsg *msg0)
435{
436 struct netmsg_so_notify *msg = (struct netmsg_so_notify *)msg0;
437 struct socket *so = msg->nm_so;
438
439 /* check predicate */
440 if (!(so->so_state & SS_ISCONNECTING) || so->so_error != 0) {
4599cf19 441 msg->nm_netmsg.nm_lmsg.ms_error = so->so_error;
b44419cb
MD
442 return (TRUE);
443 }
444
445 return (FALSE);
446}
447
02844a31 448int
358e1f78 449kern_connect(int s, int fflags, struct sockaddr *sa)
984263bc 450{
dadab5e9
MD
451 struct thread *td = curthread;
452 struct proc *p = td->td_proc;
984263bc 453 struct file *fp;
dadab5e9 454 struct socket *so;
8765eadc 455 int error, interrupted = 0;
984263bc 456
d83b97b9 457 error = holdsock(p->p_fd, s, &fp);
984263bc
MD
458 if (error)
459 return (error);
460 so = (struct socket *)fp->f_data;
358e1f78
MD
461
462 if (fflags & O_FBLOCKING)
463 /* fflags &= ~FNONBLOCK; */;
464 else if (fflags & O_FNONBLOCKING)
465 fflags |= FNONBLOCK;
466 else
467 fflags = fp->f_flag;
468
8765eadc 469 if (so->so_state & SS_ISCONNECTING) {
984263bc
MD
470 error = EALREADY;
471 goto done;
472 }
dadab5e9 473 error = soconnect(so, sa, td);
984263bc
MD
474 if (error)
475 goto bad;
358e1f78 476 if ((fflags & FNONBLOCK) && (so->so_state & SS_ISCONNECTING)) {
984263bc
MD
477 error = EINPROGRESS;
478 goto done;
479 }
b44419cb
MD
480 if ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
481 struct netmsg_so_notify msg;
b44419cb 482
48e7b118 483 netmsg_init_abortable(&msg.nm_netmsg, so,
4599cf19 484 &curthread->td_msgport,
a22c590e 485 0,
4599cf19
MD
486 netmsg_so_notify,
487 netmsg_so_notify_doabort);
b44419cb
MD
488 msg.nm_predicate = soconnected_predicate;
489 msg.nm_so = so;
490 msg.nm_etype = NM_REVENT;
48e7b118 491 error = lwkt_domsg(so->so_port, &msg.nm_netmsg.nm_lmsg, PCATCH);
8765eadc
SZ
492 if (error == EINTR || error == ERESTART)
493 interrupted = 1;
984263bc
MD
494 }
495 if (error == 0) {
496 error = so->so_error;
497 so->so_error = 0;
498 }
984263bc 499bad:
8765eadc
SZ
500 if (!interrupted)
501 so->so_state &= ~SS_ISCONNECTING;
984263bc
MD
502 if (error == ERESTART)
503 error = EINTR;
504done:
9f87144f 505 fdrop(fp);
984263bc
MD
506 return (error);
507}
508
d83b97b9
MD
509/*
510 * connect_args(int s, caddr_t name, int namelen)
511 */
512int
753fd850 513sys_connect(struct connect_args *uap)
d83b97b9
MD
514{
515 struct sockaddr *sa;
516 int error;
517
518 error = getsockaddr(&sa, uap->name, uap->namelen);
519 if (error)
520 return (error);
358e1f78
MD
521 error = kern_connect(uap->s, 0, sa);
522 FREE(sa, M_SONAME);
523
524 return (error);
525}
526
527/*
528 * connect_args(int s, int fflags, caddr_t name, int namelen)
529 */
530int
b09fd398 531sys_extconnect(struct extconnect_args *uap)
358e1f78
MD
532{
533 struct sockaddr *sa;
534 int error;
535 int fflags = uap->flags & O_FMASK;
536
537 error = getsockaddr(&sa, uap->name, uap->namelen);
538 if (error)
539 return (error);
540 error = kern_connect(uap->s, fflags, sa);
d83b97b9
MD
541 FREE(sa, M_SONAME);
542
543 return (error);
544}
545
984263bc 546int
5969a6f1 547kern_socketpair(int domain, int type, int protocol, int *sv)
984263bc 548{
dadab5e9
MD
549 struct thread *td = curthread;
550 struct proc *p = td->td_proc;
984263bc
MD
551 struct file *fp1, *fp2;
552 struct socket *so1, *so2;
259b8ea0 553 int fd1, fd2, error;
984263bc 554
dadab5e9 555 KKASSERT(p);
5969a6f1 556 error = socreate(domain, &so1, type, protocol, td);
984263bc
MD
557 if (error)
558 return (error);
5969a6f1 559 error = socreate(domain, &so2, type, protocol, td);
984263bc
MD
560 if (error)
561 goto free1;
259b8ea0 562 error = falloc(p, &fp1, &fd1);
984263bc
MD
563 if (error)
564 goto free2;
259b8ea0 565 sv[0] = fd1;
fbb4eeab 566 fp1->f_data = so1;
259b8ea0 567 error = falloc(p, &fp2, &fd2);
984263bc
MD
568 if (error)
569 goto free3;
fbb4eeab 570 fp2->f_data = so2;
259b8ea0 571 sv[1] = fd2;
984263bc
MD
572 error = soconnect2(so1, so2);
573 if (error)
574 goto free4;
5969a6f1 575 if (type == SOCK_DGRAM) {
984263bc
MD
576 /*
577 * Datagram socket connection is asymmetric.
578 */
579 error = soconnect2(so2, so1);
580 if (error)
581 goto free4;
582 }
fbb4eeab 583 fp1->f_type = fp2->f_type = DTYPE_SOCKET;
984263bc
MD
584 fp1->f_flag = fp2->f_flag = FREAD|FWRITE;
585 fp1->f_ops = fp2->f_ops = &socketops;
259b8ea0
MD
586 fsetfd(p, fp1, fd1);
587 fsetfd(p, fp2, fd2);
9f87144f
MD
588 fdrop(fp1);
589 fdrop(fp2);
984263bc
MD
590 return (error);
591free4:
259b8ea0 592 fsetfd(p, NULL, fd2);
9f87144f 593 fdrop(fp2);
984263bc 594free3:
259b8ea0 595 fsetfd(p, NULL, fd1);
9f87144f 596 fdrop(fp1);
984263bc 597free2:
9ba76b73 598 (void)soclose(so2, 0);
984263bc 599free1:
9ba76b73 600 (void)soclose(so1, 0);
984263bc
MD
601 return (error);
602}
603
5969a6f1
DRJ
604/*
605 * socketpair(int domain, int type, int protocol, int *rsv)
606 */
607int
753fd850 608sys_socketpair(struct socketpair_args *uap)
5969a6f1
DRJ
609{
610 int error, sockv[2];
611
612 error = kern_socketpair(uap->domain, uap->type, uap->protocol, sockv);
613
614 if (error == 0)
615 error = copyout(sockv, uap->rsv, sizeof(sockv));
616 return (error);
617}
618
35fbb1d9 619int
3e1837ce 620kern_sendmsg(int s, struct sockaddr *sa, struct uio *auio,
e54488bb 621 struct mbuf *control, int flags, size_t *res)
984263bc 622{
dadab5e9 623 struct thread *td = curthread;
7278a846 624 struct lwp *lp = td->td_lwp;
dadab5e9 625 struct proc *p = td->td_proc;
984263bc 626 struct file *fp;
e54488bb
MD
627 size_t len;
628 int error;
984263bc
MD
629 struct socket *so;
630#ifdef KTRACE
631 struct iovec *ktriov = NULL;
632 struct uio ktruio;
633#endif
634
635 error = holdsock(p->p_fd, s, &fp);
636 if (error)
637 return (error);
984263bc 638#ifdef KTRACE
dadab5e9 639 if (KTRPOINT(td, KTR_GENIO)) {
3e1837ce 640 int iovlen = auio->uio_iovcnt * sizeof (struct iovec);
984263bc
MD
641
642 MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
3e1837ce
DRJ
643 bcopy((caddr_t)auio->uio_iov, (caddr_t)ktriov, iovlen);
644 ktruio = *auio;
984263bc
MD
645 }
646#endif
3e1837ce 647 len = auio->uio_resid;
984263bc 648 so = (struct socket *)fp->f_data;
9ba76b73
MD
649 if ((flags & (MSG_FNONBLOCKING|MSG_FBLOCKING)) == 0) {
650 if (fp->f_flag & FNONBLOCK)
651 flags |= MSG_FNONBLOCKING;
652 }
6b6e0885 653 error = so_pru_sosend(so, sa, auio, NULL, control, flags, td);
984263bc 654 if (error) {
3e1837ce 655 if (auio->uio_resid != len && (error == ERESTART ||
984263bc
MD
656 error == EINTR || error == EWOULDBLOCK))
657 error = 0;
658 if (error == EPIPE)
7278a846 659 lwpsignal(p, lp, SIGPIPE);
984263bc 660 }
984263bc
MD
661#ifdef KTRACE
662 if (ktriov != NULL) {
663 if (error == 0) {
664 ktruio.uio_iov = ktriov;
3e1837ce 665 ktruio.uio_resid = len - auio->uio_resid;
9fb04d14 666 ktrgenio(lp, s, UIO_WRITE, &ktruio, error);
984263bc
MD
667 }
668 FREE(ktriov, M_TEMP);
669 }
670#endif
35fbb1d9 671 if (error == 0)
3e1837ce 672 *res = len - auio->uio_resid;
9f87144f 673 fdrop(fp);
984263bc
MD
674 return (error);
675}
676
41c20dac
MD
677/*
678 * sendto_args(int s, caddr_t buf, size_t len, int flags, caddr_t to, int tolen)
679 */
984263bc 680int
753fd850 681sys_sendto(struct sendto_args *uap)
984263bc 682{
3e1837ce
DRJ
683 struct thread *td = curthread;
684 struct uio auio;
984263bc 685 struct iovec aiov;
35fbb1d9
DRJ
686 struct sockaddr *sa = NULL;
687 int error;
984263bc 688
35fbb1d9
DRJ
689 if (uap->to) {
690 error = getsockaddr(&sa, uap->to, uap->tolen);
691 if (error)
692 return (error);
35fbb1d9 693 }
984263bc
MD
694 aiov.iov_base = uap->buf;
695 aiov.iov_len = uap->len;
3e1837ce
DRJ
696 auio.uio_iov = &aiov;
697 auio.uio_iovcnt = 1;
698 auio.uio_offset = 0;
699 auio.uio_resid = uap->len;
700 auio.uio_segflg = UIO_USERSPACE;
701 auio.uio_rw = UIO_WRITE;
702 auio.uio_td = td;
984263bc 703
3e1837ce 704 error = kern_sendmsg(uap->s, sa, &auio, NULL, uap->flags,
e54488bb 705 &uap->sysmsg_szresult);
984263bc 706
35fbb1d9
DRJ
707 if (sa)
708 FREE(sa, M_SONAME);
709 return (error);
984263bc
MD
710}
711
41c20dac 712/*
35fbb1d9 713 * sendmsg_args(int s, caddr_t msg, int flags)
41c20dac 714 */
984263bc 715int
753fd850 716sys_sendmsg(struct sendmsg_args *uap)
984263bc 717{
3e1837ce 718 struct thread *td = curthread;
984263bc 719 struct msghdr msg;
3e1837ce 720 struct uio auio;
75a872f8 721 struct iovec aiov[UIO_SMALLIOV], *iov = NULL;
35fbb1d9
DRJ
722 struct sockaddr *sa = NULL;
723 struct mbuf *control = NULL;
75a872f8 724 int error;
984263bc 725
35fbb1d9 726 error = copyin(uap->msg, (caddr_t)&msg, sizeof(msg));
984263bc
MD
727 if (error)
728 return (error);
35fbb1d9
DRJ
729
730 /*
731 * Conditionally copyin msg.msg_name.
732 */
733 if (msg.msg_name) {
734 error = getsockaddr(&sa, msg.msg_name, msg.msg_namelen);
735 if (error)
736 return (error);
35fbb1d9
DRJ
737 }
738
739 /*
3e1837ce 740 * Populate auio.
35fbb1d9 741 */
75a872f8 742 error = iovec_copyin(msg.msg_iov, &iov, aiov, msg.msg_iovlen,
ef5c76d7 743 &auio.uio_resid);
984263bc 744 if (error)
8130f673 745 goto cleanup2;
3e1837ce
DRJ
746 auio.uio_iov = iov;
747 auio.uio_iovcnt = msg.msg_iovlen;
748 auio.uio_offset = 0;
3e1837ce
DRJ
749 auio.uio_segflg = UIO_USERSPACE;
750 auio.uio_rw = UIO_WRITE;
751 auio.uio_td = td;
35fbb1d9
DRJ
752
753 /*
754 * Conditionally copyin msg.msg_control.
755 */
756 if (msg.msg_control) {
3e1837ce
DRJ
757 if (msg.msg_controllen < sizeof(struct cmsghdr) ||
758 msg.msg_controllen > MLEN) {
35fbb1d9
DRJ
759 error = EINVAL;
760 goto cleanup;
761 }
74f1caca 762 control = m_get(MB_WAIT, MT_CONTROL);
3e1837ce
DRJ
763 if (control == NULL) {
764 error = ENOBUFS;
35fbb1d9 765 goto cleanup;
3e1837ce
DRJ
766 }
767 control->m_len = msg.msg_controllen;
768 error = copyin(msg.msg_control, mtod(control, caddr_t),
769 msg.msg_controllen);
770 if (error) {
771 m_free(control);
772 goto cleanup;
773 }
35fbb1d9
DRJ
774 }
775
3e1837ce 776 error = kern_sendmsg(uap->s, sa, &auio, control, uap->flags,
e54488bb 777 &uap->sysmsg_szresult);
35fbb1d9
DRJ
778
779cleanup:
8130f673
MD
780 iovec_free(&iov, aiov);
781cleanup2:
35fbb1d9
DRJ
782 if (sa)
783 FREE(sa, M_SONAME);
984263bc
MD
784 return (error);
785}
984263bc 786
41c20dac 787/*
3e1837ce
DRJ
788 * kern_recvmsg() takes a handle to sa and control. If the handle is non-
789 * null, it returns a dynamically allocated struct sockaddr and an mbuf.
790 * Don't forget to FREE() and m_free() these if they are returned.
41c20dac 791 */
984263bc 792int
3e1837ce 793kern_recvmsg(int s, struct sockaddr **sa, struct uio *auio,
e54488bb 794 struct mbuf **control, int *flags, size_t *res)
984263bc 795{
dadab5e9
MD
796 struct thread *td = curthread;
797 struct proc *p = td->td_proc;
984263bc 798 struct file *fp;
e54488bb
MD
799 size_t len;
800 int error;
9ba76b73 801 int lflags;
984263bc 802 struct socket *so;
984263bc
MD
803#ifdef KTRACE
804 struct iovec *ktriov = NULL;
805 struct uio ktruio;
806#endif
807
808 error = holdsock(p->p_fd, s, &fp);
809 if (error)
810 return (error);
984263bc 811#ifdef KTRACE
dadab5e9 812 if (KTRPOINT(td, KTR_GENIO)) {
3e1837ce 813 int iovlen = auio->uio_iovcnt * sizeof (struct iovec);
984263bc
MD
814
815 MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
3e1837ce
DRJ
816 bcopy(auio->uio_iov, ktriov, iovlen);
817 ktruio = *auio;
984263bc
MD
818 }
819#endif
3e1837ce 820 len = auio->uio_resid;
984263bc 821 so = (struct socket *)fp->f_data;
9ba76b73
MD
822
823 if (flags == NULL || (*flags & (MSG_FNONBLOCKING|MSG_FBLOCKING)) == 0) {
824 if (fp->f_flag & FNONBLOCK) {
825 if (flags) {
826 *flags |= MSG_FNONBLOCKING;
827 } else {
828 lflags = MSG_FNONBLOCKING;
829 flags = &lflags;
830 }
831 }
832 }
833
6b6e0885 834 error = so_pru_soreceive(so, sa, auio, NULL, control, flags);
984263bc 835 if (error) {
3e1837ce 836 if (auio->uio_resid != len && (error == ERESTART ||
984263bc
MD
837 error == EINTR || error == EWOULDBLOCK))
838 error = 0;
839 }
840#ifdef KTRACE
841 if (ktriov != NULL) {
842 if (error == 0) {
843 ktruio.uio_iov = ktriov;
3e1837ce 844 ktruio.uio_resid = len - auio->uio_resid;
9fb04d14 845 ktrgenio(td->td_lwp, s, UIO_READ, &ktruio, error);
984263bc
MD
846 }
847 FREE(ktriov, M_TEMP);
848 }
849#endif
35fbb1d9 850 if (error == 0)
3e1837ce 851 *res = len - auio->uio_resid;
9f87144f 852 fdrop(fp);
984263bc
MD
853 return (error);
854}
855
41c20dac
MD
856/*
857 * recvfrom_args(int s, caddr_t buf, size_t len, int flags,
858 * caddr_t from, int *fromlenaddr)
859 */
984263bc 860int
753fd850 861sys_recvfrom(struct recvfrom_args *uap)
984263bc 862{
3e1837ce
DRJ
863 struct thread *td = curthread;
864 struct uio auio;
984263bc 865 struct iovec aiov;
3e1837ce 866 struct sockaddr *sa = NULL;
35fbb1d9 867 int error, fromlen;
984263bc 868
3e1837ce 869 if (uap->from && uap->fromlenaddr) {
35fbb1d9 870 error = copyin(uap->fromlenaddr, &fromlen, sizeof(fromlen));
984263bc
MD
871 if (error)
872 return (error);
3e1837ce
DRJ
873 if (fromlen < 0)
874 return (EINVAL);
35fbb1d9
DRJ
875 } else {
876 fromlen = 0;
877 }
984263bc
MD
878 aiov.iov_base = uap->buf;
879 aiov.iov_len = uap->len;
3e1837ce
DRJ
880 auio.uio_iov = &aiov;
881 auio.uio_iovcnt = 1;
882 auio.uio_offset = 0;
883 auio.uio_resid = uap->len;
884 auio.uio_segflg = UIO_USERSPACE;
885 auio.uio_rw = UIO_READ;
886 auio.uio_td = td;
984263bc 887
3e1837ce 888 error = kern_recvmsg(uap->s, uap->from ? &sa : NULL, &auio, NULL,
e54488bb 889 &uap->flags, &uap->sysmsg_szresult);
984263bc 890
3e1837ce 891 if (error == 0 && uap->from) {
c3996757
MD
892 /* note: sa may still be NULL */
893 if (sa) {
894 fromlen = MIN(fromlen, sa->sa_len);
895 error = copyout(sa, uap->from, fromlen);
896 } else {
897 fromlen = 0;
898 }
899 if (error == 0) {
35fbb1d9 900 error = copyout(&fromlen, uap->fromlenaddr,
c3996757
MD
901 sizeof(fromlen));
902 }
35fbb1d9 903 }
3e1837ce
DRJ
904 if (sa)
905 FREE(sa, M_SONAME);
984263bc 906
984263bc
MD
907 return (error);
908}
984263bc 909
41c20dac
MD
910/*
911 * recvmsg_args(int s, struct msghdr *msg, int flags)
912 */
984263bc 913int
753fd850 914sys_recvmsg(struct recvmsg_args *uap)
984263bc 915{
3e1837ce 916 struct thread *td = curthread;
984263bc 917 struct msghdr msg;
3e1837ce 918 struct uio auio;
75a872f8
DRJ
919 struct iovec aiov[UIO_SMALLIOV], *iov = NULL;
920 struct mbuf *m, *control = NULL;
3e1837ce 921 struct sockaddr *sa = NULL;
35fbb1d9 922 caddr_t ctlbuf;
3e1837ce 923 socklen_t *ufromlenp, *ucontrollenp;
75a872f8 924 int error, fromlen, controllen, len, flags, *uflagsp;
984263bc 925
35fbb1d9
DRJ
926 /*
927 * This copyin handles everything except the iovec.
928 */
929 error = copyin(uap->msg, &msg, sizeof(msg));
984263bc
MD
930 if (error)
931 return (error);
35fbb1d9 932
3e1837ce
DRJ
933 if (msg.msg_name && msg.msg_namelen < 0)
934 return (EINVAL);
935 if (msg.msg_control && msg.msg_controllen < 0)
936 return (EINVAL);
937
938 ufromlenp = (socklen_t *)((caddr_t)uap->msg + offsetof(struct msghdr,
35fbb1d9 939 msg_namelen));
35fbb1d9
DRJ
940 ucontrollenp = (socklen_t *)((caddr_t)uap->msg + offsetof(struct msghdr,
941 msg_controllen));
3e1837ce
DRJ
942 uflagsp = (int *)((caddr_t)uap->msg + offsetof(struct msghdr,
943 msg_flags));
35fbb1d9
DRJ
944
945 /*
3e1837ce 946 * Populate auio.
35fbb1d9 947 */
75a872f8 948 error = iovec_copyin(msg.msg_iov, &iov, aiov, msg.msg_iovlen,
ef5c76d7 949 &auio.uio_resid);
984263bc 950 if (error)
75a872f8 951 return (error);
3e1837ce
DRJ
952 auio.uio_iov = iov;
953 auio.uio_iovcnt = msg.msg_iovlen;
954 auio.uio_offset = 0;
3e1837ce
DRJ
955 auio.uio_segflg = UIO_USERSPACE;
956 auio.uio_rw = UIO_READ;
957 auio.uio_td = td;
35fbb1d9 958
b7ccd728 959 flags = uap->flags;
35fbb1d9 960
e54488bb
MD
961 error = kern_recvmsg(uap->s,
962 (msg.msg_name ? &sa : NULL), &auio,
963 (msg.msg_control ? &control : NULL), &flags,
964 &uap->sysmsg_szresult);
35fbb1d9
DRJ
965
966 /*
3e1837ce 967 * Conditionally copyout the name and populate the namelen field.
35fbb1d9 968 */
3e1837ce 969 if (error == 0 && msg.msg_name) {
b9cd15b9
YT
970 /* note: sa may still be NULL */
971 if (sa != NULL) {
972 fromlen = MIN(msg.msg_namelen, sa->sa_len);
973 error = copyout(sa, msg.msg_name, fromlen);
b4354d10 974 } else {
b9cd15b9 975 fromlen = 0;
b4354d10 976 }
35fbb1d9 977 if (error == 0)
3e1837ce
DRJ
978 error = copyout(&fromlen, ufromlenp,
979 sizeof(*ufromlenp));
984263bc 980 }
35fbb1d9
DRJ
981
982 /*
983 * Copyout msg.msg_control and msg.msg_controllen.
984 */
3e1837ce 985 if (error == 0 && msg.msg_control) {
35fbb1d9 986 len = msg.msg_controllen;
3e1837ce
DRJ
987 m = control;
988 ctlbuf = (caddr_t)msg.msg_control;
35fbb1d9
DRJ
989
990 while(m && len > 0) {
991 unsigned int tocopy;
992
993 if (len >= m->m_len) {
994 tocopy = m->m_len;
995 } else {
996 msg.msg_flags |= MSG_CTRUNC;
997 tocopy = len;
998 }
999
1000 error = copyout(mtod(m, caddr_t), ctlbuf, tocopy);
1001 if (error)
1002 goto cleanup;
1003
1004 ctlbuf += tocopy;
1005 len -= tocopy;
1006 m = m->m_next;
1007 }
3e1837ce
DRJ
1008 controllen = ctlbuf - (caddr_t)msg.msg_control;
1009 error = copyout(&controllen, ucontrollenp,
35fbb1d9
DRJ
1010 sizeof(*ucontrollenp));
1011 }
1012
3e1837ce
DRJ
1013 if (error == 0)
1014 error = copyout(&flags, uflagsp, sizeof(*uflagsp));
1015
35fbb1d9 1016cleanup:
3e1837ce
DRJ
1017 if (sa)
1018 FREE(sa, M_SONAME);
75a872f8 1019 iovec_free(&iov, aiov);
3e1837ce
DRJ
1020 if (control)
1021 m_freem(control);
984263bc
MD
1022 return (error);
1023}
1024
41c20dac 1025/*
201305ad
DRJ
1026 * If sopt->sopt_td == NULL, then sopt->sopt_val is treated as an
1027 * in kernel pointer instead of a userland pointer. This allows us
1028 * to manipulate socket options in the emulation code.
41c20dac 1029 */
984263bc 1030int
201305ad 1031kern_setsockopt(int s, struct sockopt *sopt)
984263bc 1032{
dadab5e9
MD
1033 struct thread *td = curthread;
1034 struct proc *p = td->td_proc;
984263bc 1035 struct file *fp;
984263bc
MD
1036 int error;
1037
b4354d10 1038 if (sopt->sopt_val == NULL && sopt->sopt_valsize != 0)
984263bc 1039 return (EFAULT);
201305ad 1040 if (sopt->sopt_valsize < 0)
984263bc
MD
1041 return (EINVAL);
1042
201305ad 1043 error = holdsock(p->p_fd, s, &fp);
984263bc
MD
1044 if (error)
1045 return (error);
1046
201305ad 1047 error = sosetopt((struct socket *)fp->f_data, sopt);
9f87144f 1048 fdrop(fp);
201305ad
DRJ
1049 return (error);
1050}
1051
1052/*
1053 * setsockopt_args(int s, int level, int name, caddr_t val, int valsize)
1054 */
1055int
753fd850 1056sys_setsockopt(struct setsockopt_args *uap)
201305ad
DRJ
1057{
1058 struct thread *td = curthread;
1059 struct sockopt sopt;
1060 int error;
1061
984263bc
MD
1062 sopt.sopt_level = uap->level;
1063 sopt.sopt_name = uap->name;
984263bc 1064 sopt.sopt_valsize = uap->valsize;
dadab5e9 1065 sopt.sopt_td = td;
aca22a94 1066 sopt.sopt_val = NULL;
201305ad 1067
b4354d10
MD
1068 if (sopt.sopt_valsize < 0 || sopt.sopt_valsize > SOMAXOPT_SIZE)
1069 return (EINVAL);
792239df
AE
1070 if (uap->val) {
1071 sopt.sopt_val = kmalloc(sopt.sopt_valsize, M_TEMP, M_WAITOK);
1072 error = copyin(uap->val, sopt.sopt_val, sopt.sopt_valsize);
1073 if (error)
1074 goto out;
792239df 1075 }
aca22a94 1076
201305ad 1077 error = kern_setsockopt(uap->s, &sopt);
de0003fe 1078out:
792239df
AE
1079 if (uap->val)
1080 kfree(sopt.sopt_val, M_TEMP);
984263bc
MD
1081 return(error);
1082}
1083
41c20dac 1084/*
201305ad
DRJ
1085 * If sopt->sopt_td == NULL, then sopt->sopt_val is treated as an
1086 * in kernel pointer instead of a userland pointer. This allows us
1087 * to manipulate socket options in the emulation code.
41c20dac 1088 */
984263bc 1089int
201305ad 1090kern_getsockopt(int s, struct sockopt *sopt)
984263bc 1091{
dadab5e9
MD
1092 struct thread *td = curthread;
1093 struct proc *p = td->td_proc;
201305ad
DRJ
1094 struct file *fp;
1095 int error;
984263bc 1096
b4354d10 1097 if (sopt->sopt_val == NULL && sopt->sopt_valsize != 0)
201305ad 1098 return (EFAULT);
b4354d10 1099 if (sopt->sopt_valsize < 0 || sopt->sopt_valsize > SOMAXOPT_SIZE)
201305ad
DRJ
1100 return (EINVAL);
1101
1102 error = holdsock(p->p_fd, s, &fp);
984263bc
MD
1103 if (error)
1104 return (error);
201305ad
DRJ
1105
1106 error = sogetopt((struct socket *)fp->f_data, sopt);
9f87144f 1107 fdrop(fp);
201305ad
DRJ
1108 return (error);
1109}
1110
1111/*
1112 * getsockopt_Args(int s, int level, int name, caddr_t val, int *avalsize)
1113 */
1114int
753fd850 1115sys_getsockopt(struct getsockopt_args *uap)
201305ad
DRJ
1116{
1117 struct thread *td = curthread;
1118 struct sockopt sopt;
1119 int error, valsize;
1120
984263bc 1121 if (uap->val) {
201305ad
DRJ
1122 error = copyin(uap->avalsize, &valsize, sizeof(valsize));
1123 if (error)
984263bc 1124 return (error);
984263bc
MD
1125 } else {
1126 valsize = 0;
1127 }
1128
984263bc
MD
1129 sopt.sopt_level = uap->level;
1130 sopt.sopt_name = uap->name;
201305ad 1131 sopt.sopt_valsize = valsize;
dadab5e9 1132 sopt.sopt_td = td;
aca22a94 1133 sopt.sopt_val = NULL;
984263bc 1134
aca22a94 1135 if (sopt.sopt_valsize < 0 || sopt.sopt_valsize > SOMAXOPT_SIZE)
b4354d10 1136 return (EINVAL);
792239df
AE
1137 if (uap->val) {
1138 sopt.sopt_val = kmalloc(sopt.sopt_valsize, M_TEMP, M_WAITOK);
1139 error = copyin(uap->val, sopt.sopt_val, sopt.sopt_valsize);
1140 if (error)
1141 goto out;
792239df 1142 }
aca22a94 1143
201305ad 1144 error = kern_getsockopt(uap->s, &sopt);
de0003fe
AE
1145 if (error)
1146 goto out;
1147 valsize = sopt.sopt_valsize;
1148 error = copyout(&valsize, uap->avalsize, sizeof(valsize));
1149 if (error)
1150 goto out;
792239df
AE
1151 if (uap->val)
1152 error = copyout(sopt.sopt_val, uap->val, sopt.sopt_valsize);
de0003fe 1153out:
792239df
AE
1154 if (uap->val)
1155 kfree(sopt.sopt_val, M_TEMP);
984263bc
MD
1156 return (error);
1157}
1158
1159/*
5969a6f1
DRJ
1160 * The second argument to kern_getsockname() is a handle to a struct sockaddr.
1161 * This allows kern_getsockname() to return a pointer to an allocated struct
1162 * sockaddr which must be freed later with FREE(). The caller must
1163 * initialize *name to NULL.
984263bc 1164 */
5969a6f1
DRJ
1165int
1166kern_getsockname(int s, struct sockaddr **name, int *namelen)
984263bc 1167{
dadab5e9
MD
1168 struct thread *td = curthread;
1169 struct proc *p = td->td_proc;
984263bc 1170 struct file *fp;
dadab5e9 1171 struct socket *so;
5969a6f1
DRJ
1172 struct sockaddr *sa = NULL;
1173 int error;
984263bc 1174
5969a6f1 1175 error = holdsock(p->p_fd, s, &fp);
984263bc
MD
1176 if (error)
1177 return (error);
5969a6f1 1178 if (*namelen < 0) {
9f87144f 1179 fdrop(fp);
984263bc
MD
1180 return (EINVAL);
1181 }
1182 so = (struct socket *)fp->f_data;
6b6e0885 1183 error = so_pru_sockaddr(so, &sa);
5969a6f1 1184 if (error == 0) {
b4354d10 1185 if (sa == NULL) {
5969a6f1
DRJ
1186 *namelen = 0;
1187 } else {
1188 *namelen = MIN(*namelen, sa->sa_len);
1189 *name = sa;
1190 }
984263bc
MD
1191 }
1192
9f87144f 1193 fdrop(fp);
984263bc
MD
1194 return (error);
1195}
1196
5969a6f1
DRJ
1197/*
1198 * getsockname_args(int fdes, caddr_t asa, int *alen)
1199 *
1200 * Get socket name.
1201 */
984263bc 1202int
753fd850 1203sys_getsockname(struct getsockname_args *uap)
984263bc 1204{
5969a6f1
DRJ
1205 struct sockaddr *sa = NULL;
1206 int error, sa_len;
1207
1208 error = copyin(uap->alen, &sa_len, sizeof(sa_len));
1209 if (error)
1210 return (error);
1211
1212 error = kern_getsockname(uap->fdes, &sa, &sa_len);
984263bc 1213
5969a6f1
DRJ
1214 if (error == 0)
1215 error = copyout(sa, uap->asa, sa_len);
1216 if (error == 0)
1217 error = copyout(&sa_len, uap->alen, sizeof(*uap->alen));
1218 if (sa)
1219 FREE(sa, M_SONAME);
1220 return (error);
984263bc
MD
1221}
1222
984263bc 1223/*
5969a6f1
DRJ
1224 * The second argument to kern_getpeername() is a handle to a struct sockaddr.
1225 * This allows kern_getpeername() to return a pointer to an allocated struct
1226 * sockaddr which must be freed later with FREE(). The caller must
1227 * initialize *name to NULL.
984263bc 1228 */
5969a6f1
DRJ
1229int
1230kern_getpeername(int s, struct sockaddr **name, int *namelen)
984263bc 1231{
dadab5e9
MD
1232 struct thread *td = curthread;
1233 struct proc *p = td->td_proc;
984263bc 1234 struct file *fp;
dadab5e9 1235 struct socket *so;
5969a6f1
DRJ
1236 struct sockaddr *sa = NULL;
1237 int error;
984263bc 1238
5969a6f1 1239 error = holdsock(p->p_fd, s, &fp);
984263bc
MD
1240 if (error)
1241 return (error);
5969a6f1 1242 if (*namelen < 0) {
9f87144f 1243 fdrop(fp);
5969a6f1
DRJ
1244 return (EINVAL);
1245 }
984263bc
MD
1246 so = (struct socket *)fp->f_data;
1247 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONFIRMING)) == 0) {
9f87144f 1248 fdrop(fp);
984263bc
MD
1249 return (ENOTCONN);
1250 }
6b6e0885 1251 error = so_pru_peeraddr(so, &sa);
5969a6f1 1252 if (error == 0) {
b4354d10 1253 if (sa == NULL) {
5969a6f1
DRJ
1254 *namelen = 0;
1255 } else {
1256 *namelen = MIN(*namelen, sa->sa_len);
1257 *name = sa;
1258 }
984263bc 1259 }
5969a6f1 1260
9f87144f 1261 fdrop(fp);
984263bc
MD
1262 return (error);
1263}
1264
5969a6f1
DRJ
1265/*
1266 * getpeername_args(int fdes, caddr_t asa, int *alen)
1267 *
1268 * Get name of peer for connected socket.
1269 */
984263bc 1270int
753fd850 1271sys_getpeername(struct getpeername_args *uap)
984263bc 1272{
5969a6f1
DRJ
1273 struct sockaddr *sa = NULL;
1274 int error, sa_len;
1275
1276 error = copyin(uap->alen, &sa_len, sizeof(sa_len));
1277 if (error)
1278 return (error);
1279
1280 error = kern_getpeername(uap->fdes, &sa, &sa_len);
1281
1282 if (error == 0)
1283 error = copyout(sa, uap->asa, sa_len);
1284 if (error == 0)
1285 error = copyout(&sa_len, uap->alen, sizeof(*uap->alen));
1286 if (sa)
1287 FREE(sa, M_SONAME);
1288 return (error);
984263bc
MD
1289}
1290
984263bc 1291int
02844a31 1292getsockaddr(struct sockaddr **namp, caddr_t uaddr, size_t len)
984263bc
MD
1293{
1294 struct sockaddr *sa;
1295 int error;
1296
02844a31 1297 *namp = NULL;
984263bc
MD
1298 if (len > SOCK_MAXADDRLEN)
1299 return ENAMETOOLONG;
02844a31
MD
1300 if (len < offsetof(struct sockaddr, sa_data[0]))
1301 return EDOM;
984263bc
MD
1302 MALLOC(sa, struct sockaddr *, len, M_SONAME, M_WAITOK);
1303 error = copyin(uaddr, sa, len);
1304 if (error) {
1305 FREE(sa, M_SONAME);
1306 } else {
75a872f8
DRJ
1307#if BYTE_ORDER != BIG_ENDIAN
1308 /*
1309 * The bind(), connect(), and sendto() syscalls were not
1310 * versioned for COMPAT_43. Thus, this check must stay.
1311 */
984263bc
MD
1312 if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
1313 sa->sa_family = sa->sa_len;
1314#endif
1315 sa->sa_len = len;
1316 *namp = sa;
1317 }
1318 return error;
1319}
1320
b4caac98
MD
1321/*
1322 * Detach a mapped page and release resources back to the system.
1323 * We must release our wiring and if the object is ripped out
1324 * from under the vm_page we become responsible for freeing the
df8d1020 1325 * page. These routines must be MPSAFE.
b4caac98
MD
1326 *
1327 * XXX HACK XXX TEMPORARY UNTIL WE IMPLEMENT EXT MBUF REFERENCE COUNTING
df8d1020
MD
1328 *
1329 * XXX vm_page_*() routines are not MPSAFE yet, the MP lock is required.
b4caac98
MD
1330 */
1331static void
013a4c0e 1332sf_buf_mref(void *arg)
b4caac98 1333{
e66bab2b
MD
1334 struct sfbuf_mref *sfm = arg;
1335
df8d1020
MD
1336 /*
1337 * We must already hold a ref so there is no race to 0, just
1338 * atomically increment the count.
1339 */
1340 atomic_add_int(&sfm->mref_count, 1);
b4caac98
MD
1341}
1342
1343static void
013a4c0e 1344sf_buf_mfree(void *arg)
b4caac98 1345{
e66bab2b 1346 struct sfbuf_mref *sfm = arg;
b4caac98 1347 vm_page_t m;
e66bab2b
MD
1348
1349 KKASSERT(sfm->mref_count > 0);
321e057f 1350 if (atomic_fetchadd_int(&sfm->mref_count, -1) == 1) {
df8d1020 1351 /*
df8d1020
MD
1352 * XXX vm_page_*() and SFBUF routines not MPSAFE yet.
1353 */
321e057f
SZ
1354 get_mplock();
1355 crit_enter();
1356 m = sf_buf_page(sfm->sf);
1357 sf_buf_free(sfm->sf);
1358 vm_page_unwire(m, 0);
1359 if (m->wire_count == 0 && m->object == NULL)
1360 vm_page_try_to_free(m);
1361 crit_exit();
1362 rel_mplock();
1363 kfree(sfm, M_SENDFILE);
b4caac98
MD
1364 }
1365}
1366
984263bc
MD
1367/*
1368 * sendfile(2).
1369 * int sendfile(int fd, int s, off_t offset, size_t nbytes,
1370 * struct sf_hdtr *hdtr, off_t *sbytes, int flags)
1371 *
1372 * Send a file specified by 'fd' and starting at 'offset' to a socket
1373 * specified by 's'. Send only 'nbytes' of the file or until EOF if
1374 * nbytes == 0. Optionally add a header and/or trailer to the socket
1375 * output. If specified, write the total number of bytes sent into *sbytes.
75a872f8
DRJ
1376 *
1377 * In FreeBSD kern/uipc_syscalls.c,v 1.103, a bug was fixed that caused
1378 * the headers to count against the remaining bytes to be sent from
1379 * the file descriptor. We may wish to implement a compatibility syscall
1380 * in the future.
984263bc
MD
1381 */
1382int
753fd850 1383sys_sendfile(struct sendfile_args *uap)
984263bc 1384{
dadab5e9
MD
1385 struct thread *td = curthread;
1386 struct proc *p = td->td_proc;
984263bc 1387 struct file *fp;
75a872f8 1388 struct vnode *vp = NULL;
984263bc 1389 struct sf_hdtr hdtr;
75a872f8
DRJ
1390 struct iovec aiov[UIO_SMALLIOV], *iov = NULL;
1391 struct uio auio;
30eeba44 1392 struct mbuf *mheader = NULL;
e54488bb
MD
1393 size_t hbytes = 0;
1394 size_t tbytes;
1395 off_t hdtr_size = 0;
1396 off_t sbytes;
1397 int error;
984263bc 1398
dadab5e9 1399 KKASSERT(p);
dadab5e9 1400
984263bc
MD
1401 /*
1402 * Do argument checking. Must be a regular file in, stream
1403 * type and connected socket out, positive offset.
1404 */
fa541be6 1405 fp = holdfp(p->p_fd, uap->fd, FREAD);
984263bc 1406 if (fp == NULL) {
f0846490 1407 return (EBADF);
984263bc
MD
1408 }
1409 if (fp->f_type != DTYPE_VNODE) {
9f87144f 1410 fdrop(fp);
f0846490 1411 return (EINVAL);
984263bc
MD
1412 }
1413 vp = (struct vnode *)fp->f_data;
1414 vref(vp);
9f87144f 1415 fdrop(fp);
75a872f8
DRJ
1416
1417 /*
1418 * If specified, get the pointer to the sf_hdtr struct for
1419 * any headers/trailers.
1420 */
1421 if (uap->hdtr) {
1422 error = copyin(uap->hdtr, &hdtr, sizeof(hdtr));
1423 if (error)
1424 goto done;
1425 /*
1426 * Send any headers.
1427 */
1428 if (hdtr.headers) {
1429 error = iovec_copyin(hdtr.headers, &iov, aiov,
ef5c76d7 1430 hdtr.hdr_cnt, &hbytes);
75a872f8
DRJ
1431 if (error)
1432 goto done;
1433 auio.uio_iov = iov;
1434 auio.uio_iovcnt = hdtr.hdr_cnt;
1435 auio.uio_offset = 0;
1436 auio.uio_segflg = UIO_USERSPACE;
1437 auio.uio_rw = UIO_WRITE;
1438 auio.uio_td = td;
30eeba44 1439 auio.uio_resid = hbytes;
75a872f8 1440
e12241e1 1441 mheader = m_uiomove(&auio);
75a872f8
DRJ
1442
1443 iovec_free(&iov, aiov);
30eeba44 1444 if (mheader == NULL)
75a872f8 1445 goto done;
75a872f8
DRJ
1446 }
1447 }
1448
30eeba44 1449 error = kern_sendfile(vp, uap->s, uap->offset, uap->nbytes, mheader,
75a872f8
DRJ
1450 &sbytes, uap->flags);
1451 if (error)
1452 goto done;
1453
1454 /*
1455 * Send trailers. Wimp out and use writev(2).
1456 */
1457 if (uap->hdtr != NULL && hdtr.trailers != NULL) {
1458 error = iovec_copyin(hdtr.trailers, &iov, aiov,
ef5c76d7 1459 hdtr.trl_cnt, &auio.uio_resid);
75a872f8
DRJ
1460 if (error)
1461 goto done;
1462 auio.uio_iov = iov;
1463 auio.uio_iovcnt = hdtr.trl_cnt;
1464 auio.uio_offset = 0;
1465 auio.uio_segflg = UIO_USERSPACE;
1466 auio.uio_rw = UIO_WRITE;
1467 auio.uio_td = td;
1468
30eeba44 1469 error = kern_sendmsg(uap->s, NULL, &auio, NULL, 0, &tbytes);
75a872f8
DRJ
1470
1471 iovec_free(&iov, aiov);
1472 if (error)
1473 goto done;
30eeba44 1474 hdtr_size += tbytes; /* trailer bytes successfully sent */
75a872f8
DRJ
1475 }
1476
1477done:
1478 if (uap->sbytes != NULL) {
1479 sbytes += hdtr_size;
1480 copyout(&sbytes, uap->sbytes, sizeof(off_t));
1481 }
1482 if (vp)
1483 vrele(vp);
1484 return (error);
1485}
1486
1487int
06ecca5a 1488kern_sendfile(struct vnode *vp, int sfd, off_t offset, size_t nbytes,
30eeba44 1489 struct mbuf *mheader, off_t *sbytes, int flags)
75a872f8
DRJ
1490{
1491 struct thread *td = curthread;
1492 struct proc *p = td->td_proc;
1493 struct vm_object *obj;
1494 struct socket *so;
285332f0 1495 struct file *fp;
75a872f8
DRJ
1496 struct mbuf *m;
1497 struct sf_buf *sf;
e66bab2b 1498 struct sfbuf_mref *sfm;
75a872f8
DRJ
1499 struct vm_page *pg;
1500 off_t off, xfsize;
39b3370f 1501 off_t hbytes = 0;
75a872f8
DRJ
1502 int error = 0;
1503
7540ab49
MD
1504 if (vp->v_type != VREG) {
1505 error = EINVAL;
1506 goto done0;
1507 }
1508 if ((obj = vp->v_object) == NULL) {
984263bc 1509 error = EINVAL;
285332f0 1510 goto done0;
984263bc 1511 }
06ecca5a 1512 error = holdsock(p->p_fd, sfd, &fp);
984263bc 1513 if (error)
285332f0 1514 goto done0;
984263bc
MD
1515 so = (struct socket *)fp->f_data;
1516 if (so->so_type != SOCK_STREAM) {
1517 error = EINVAL;
1518 goto done;
1519 }
1520 if ((so->so_state & SS_ISCONNECTED) == 0) {
1521 error = ENOTCONN;
1522 goto done;
1523 }
75a872f8 1524 if (offset < 0) {
984263bc
MD
1525 error = EINVAL;
1526 goto done;
1527 }
1528
75a872f8 1529 *sbytes = 0;
984263bc
MD
1530 /*
1531 * Protect against multiple writers to the socket.
1532 */
6d49aa6f 1533 ssb_lock(&so->so_snd, M_WAITOK);
984263bc
MD
1534
1535 /*
1536 * Loop through the pages in the file, starting with the requested
1537 * offset. Get a file page (do I/O if necessary), map the file page
1538 * into an sf_buf, attach an mbuf header to the sf_buf, and queue
1539 * it on the socket.
1540 */
39b3370f 1541 for (off = offset; ; off += xfsize, *sbytes += xfsize + hbytes) {
984263bc
MD
1542 vm_pindex_t pindex;
1543 vm_offset_t pgoff;
1544
1545 pindex = OFF_TO_IDX(off);
1546retry_lookup:
1547 /*
1548 * Calculate the amount to transfer. Not to exceed a page,
1549 * the EOF, or the passed in nbytes.
1550 */
57f7b636 1551 xfsize = vp->v_filesize - off;
984263bc
MD
1552 if (xfsize > PAGE_SIZE)
1553 xfsize = PAGE_SIZE;
1554 pgoff = (vm_offset_t)(off & PAGE_MASK);
1555 if (PAGE_SIZE - pgoff < xfsize)
1556 xfsize = PAGE_SIZE - pgoff;
75a872f8
DRJ
1557 if (nbytes && xfsize > (nbytes - *sbytes))
1558 xfsize = nbytes - *sbytes;
984263bc
MD
1559 if (xfsize <= 0)
1560 break;
1561 /*
1562 * Optimize the non-blocking case by looking at the socket space
1563 * before going to the extra work of constituting the sf_buf.
1564 */
6d49aa6f 1565 if ((fp->f_flag & FNONBLOCK) && ssb_space(&so->so_snd) <= 0) {
984263bc
MD
1566 if (so->so_state & SS_CANTSENDMORE)
1567 error = EPIPE;
1568 else
1569 error = EAGAIN;
6d49aa6f 1570 ssb_unlock(&so->so_snd);
984263bc
MD
1571 goto done;
1572 }
1573 /*
1574 * Attempt to look up the page.
1575 *
06ecca5a 1576 * Allocate if not found, wait and loop if busy, then
5fd012e0
MD
1577 * wire the page. critical section protection is
1578 * required to maintain the object association (an
1579 * interrupt can free the page) through to the
1580 * vm_page_wire() call.
984263bc 1581 */
5fd012e0 1582 crit_enter();
984263bc 1583 pg = vm_page_lookup(obj, pindex);
984263bc
MD
1584 if (pg == NULL) {
1585 pg = vm_page_alloc(obj, pindex, VM_ALLOC_NORMAL);
1586 if (pg == NULL) {
4ecf7cc9 1587 vm_wait(0);
5fd012e0 1588 crit_exit();
984263bc
MD
1589 goto retry_lookup;
1590 }
1591 vm_page_wakeup(pg);
1592 } else if (vm_page_sleep_busy(pg, TRUE, "sfpbsy")) {
5fd012e0 1593 crit_exit();
984263bc
MD
1594 goto retry_lookup;
1595 }
984263bc 1596 vm_page_wire(pg);
5fd012e0 1597 crit_exit();
984263bc
MD
1598
1599 /*
1600 * If page is not valid for what we need, initiate I/O
1601 */
1602
1603 if (!pg->valid || !vm_page_is_valid(pg, pgoff, xfsize)) {
1604 struct uio auio;
1605 struct iovec aiov;
1606 int bsize;
1607
1608 /*
1609 * Ensure that our page is still around when the I/O
1610 * completes.
1611 */
1612 vm_page_io_start(pg);
1613
1614 /*
1615 * Get the page from backing store.
1616 */
1617 bsize = vp->v_mount->mnt_stat.f_iosize;
1618 auio.uio_iov = &aiov;
1619 auio.uio_iovcnt = 1;
1620 aiov.iov_base = 0;
1621 aiov.iov_len = MAXBSIZE;
1622 auio.uio_resid = MAXBSIZE;
1623 auio.uio_offset = trunc_page(off);
1624 auio.uio_segflg = UIO_NOCOPY;
1625 auio.uio_rw = UIO_READ;
dadab5e9 1626 auio.uio_td = td;
ab6f251b 1627 vn_lock(vp, LK_SHARED | LK_RETRY);
dadab5e9
MD
1628 error = VOP_READ(vp, &auio,
1629 IO_VMIO | ((MAXBSIZE / bsize) << 16),
1630 p->p_ucred);
a11aaa81 1631 vn_unlock(vp);
984263bc
MD
1632 vm_page_flag_clear(pg, PG_ZERO);
1633 vm_page_io_finish(pg);
1634 if (error) {
f2555cdd 1635 crit_enter();
984263bc 1636 vm_page_unwire(pg, 0);
f2555cdd
MD
1637 vm_page_try_to_free(pg);
1638 crit_exit();
6d49aa6f 1639 ssb_unlock(&so->so_snd);
984263bc
MD
1640 goto done;
1641 }
1642 }
1643
1644
1645 /*
1646 * Get a sendfile buf. We usually wait as long as necessary,
1647 * but this wait can be interrupted.
1648 */
4f1640d6 1649 if ((sf = sf_buf_alloc(pg, SFB_CATCH)) == NULL) {
5fd012e0 1650 crit_enter();
984263bc 1651 vm_page_unwire(pg, 0);
f2555cdd 1652 vm_page_try_to_free(pg);
5fd012e0 1653 crit_exit();
6d49aa6f 1654 ssb_unlock(&so->so_snd);
984263bc
MD
1655 error = EINTR;
1656 goto done;
1657 }
1658
984263bc
MD
1659 /*
1660 * Get an mbuf header and set it up as having external storage.
1661 */
74f1caca 1662 MGETHDR(m, MB_WAIT, MT_DATA);
984263bc
MD
1663 if (m == NULL) {
1664 error = ENOBUFS;
b4caac98 1665 sf_buf_free(sf);
6d49aa6f 1666 ssb_unlock(&so->so_snd);
984263bc
MD
1667 goto done;
1668 }
e66bab2b
MD
1669
1670 /*
1671 * sfm is a temporary hack, use a per-cpu cache for this.
1672 */
efda3bd0 1673 sfm = kmalloc(sizeof(struct sfbuf_mref), M_SENDFILE, M_WAITOK);
e66bab2b
MD
1674 sfm->sf = sf;
1675 sfm->mref_count = 1;
1676
b542cd49
JS
1677 m->m_ext.ext_free = sf_buf_mfree;
1678 m->m_ext.ext_ref = sf_buf_mref;
e66bab2b 1679 m->m_ext.ext_arg = sfm;
984263bc
MD
1680 m->m_ext.ext_buf = (void *)sf->kva;
1681 m->m_ext.ext_size = PAGE_SIZE;
1682 m->m_data = (char *) sf->kva + pgoff;
013a4c0e 1683 m->m_flags |= M_EXT;
984263bc 1684 m->m_pkthdr.len = m->m_len = xfsize;
b542cd49 1685 KKASSERT((m->m_flags & (M_EXT_CLUSTER)) == 0);
30eeba44 1686
39b3370f
JH
1687 if (mheader != NULL) {
1688 hbytes = mheader->m_pkthdr.len;
30eeba44
JH
1689 mheader->m_pkthdr.len += m->m_pkthdr.len;
1690 m_cat(mheader, m);
1691 m = mheader;
1692 mheader = NULL;
39b3370f
JH
1693 } else
1694 hbytes = 0;
30eeba44 1695
984263bc
MD
1696 /*
1697 * Add the buffer to the socket buffer chain.
1698 */
5fd012e0 1699 crit_enter();
984263bc
MD
1700retry_space:
1701 /*
1702 * Make sure that the socket is still able to take more data.
1703 * CANTSENDMORE being true usually means that the connection
1704 * was closed. so_error is true when an error was sensed after
1705 * a previous send.
1706 * The state is checked after the page mapping and buffer
1707 * allocation above since those operations may block and make
1708 * any socket checks stale. From this point forward, nothing
1709 * blocks before the pru_send (or more accurately, any blocking
1710 * results in a loop back to here to re-check).
1711 */
1712 if ((so->so_state & SS_CANTSENDMORE) || so->so_error) {
1713 if (so->so_state & SS_CANTSENDMORE) {
1714 error = EPIPE;
1715 } else {
1716 error = so->so_error;
1717 so->so_error = 0;
1718 }
1719 m_freem(m);
6d49aa6f 1720 ssb_unlock(&so->so_snd);
5fd012e0 1721 crit_exit();
984263bc
MD
1722 goto done;
1723 }
1724 /*
1725 * Wait for socket space to become available. We do this just
1726 * after checking the connection state above in order to avoid
6d49aa6f 1727 * a race condition with ssb_wait().
984263bc 1728 */
6d49aa6f 1729 if (ssb_space(&so->so_snd) < so->so_snd.ssb_lowat) {
9ba76b73 1730 if (fp->f_flag & FNONBLOCK) {
984263bc 1731 m_freem(m);
6d49aa6f 1732 ssb_unlock(&so->so_snd);
5fd012e0 1733 crit_exit();
984263bc
MD
1734 error = EAGAIN;
1735 goto done;
1736 }
6d49aa6f 1737 error = ssb_wait(&so->so_snd);
984263bc 1738 /*
6d49aa6f 1739 * An error from ssb_wait usually indicates that we've
984263bc
MD
1740 * been interrupted by a signal. If we've sent anything
1741 * then return bytes sent, otherwise return the error.
1742 */
1743 if (error) {
1744 m_freem(m);
6d49aa6f 1745 ssb_unlock(&so->so_snd);
5fd012e0 1746 crit_exit();
984263bc
MD
1747 goto done;
1748 }
1749 goto retry_space;
1750 }
3c6b2883 1751 error = so_pru_send(so, 0, m, NULL, NULL, td);
5fd012e0 1752 crit_exit();
984263bc 1753 if (error) {
6d49aa6f 1754 ssb_unlock(&so->so_snd);
984263bc
MD
1755 goto done;
1756 }
1757 }
d785f69d 1758 if (mheader != NULL) {
39b3370f 1759 *sbytes += mheader->m_pkthdr.len;
d785f69d
JH
1760 error = so_pru_send(so, 0, mheader, NULL, NULL, td);
1761 mheader = NULL;
1762 }
6d49aa6f 1763 ssb_unlock(&so->so_snd);
984263bc 1764
984263bc 1765done:
9f87144f 1766 fdrop(fp);
285332f0 1767done0:
30eeba44
JH
1768 if (mheader != NULL)
1769 m_freem(mheader);
984263bc
MD
1770 return (error);
1771}
78812139
EN
1772
1773int
753fd850 1774sys_sctp_peeloff(struct sctp_peeloff_args *uap)
78812139
EN
1775{
1776#ifdef SCTP
1777 struct thread *td = curthread;
1778 struct proc *p = td->td_proc;
78812139
EN
1779 struct file *lfp = NULL;
1780 struct file *nfp = NULL;
1781 int error;
1782 struct socket *head, *so;
1783 caddr_t assoc_id;
1784 int fd;
1785 short fflag; /* type must match fp->f_flag */
1786
1787 assoc_id = uap->name;
fa541be6 1788 error = holdsock(p->p_fd, uap->sd, &lfp);
78812139
EN
1789 if (error) {
1790 return (error);
1791 }
1792 crit_enter();
1793 head = (struct socket *)lfp->f_data;
1794 error = sctp_can_peel_off(head, assoc_id);
1795 if (error) {
1796 crit_exit();
1797 goto done;
1798 }
1799 /*
1800 * At this point we know we do have a assoc to pull
1801 * we proceed to get the fd setup. This may block
1802 * but that is ok.
1803 */
1804
1805 fflag = lfp->f_flag;
1806 error = falloc(p, &nfp, &fd);
1807 if (error) {
1808 /*
1809 * Probably ran out of file descriptors. Put the
1810 * unaccepted connection back onto the queue and
1811 * do another wakeup so some other process might
1812 * have a chance at it.
1813 */
1814 crit_exit();
1815 goto done;
1816 }
e54488bb 1817 uap->sysmsg_iresult = fd;
78812139
EN
1818
1819 so = sctp_get_peeloff(head, assoc_id, &error);
1820 if (so == NULL) {
1821 /*
1822 * Either someone else peeled it off OR
1823 * we can't get a socket.
1824 */
1825 goto noconnection;
1826 }
1827 so->so_state &= ~SS_COMP;
1828 so->so_state &= ~SS_NOFDREF;
1829 so->so_head = NULL;
1830 if (head->so_sigio != NULL)
1831 fsetown(fgetown(head->so_sigio), &so->so_sigio);
1832
fbb4eeab 1833 nfp->f_type = DTYPE_SOCKET;
78812139
EN
1834 nfp->f_flag = fflag;
1835 nfp->f_ops = &socketops;
fbb4eeab 1836 nfp->f_data = so;
78812139
EN
1837
1838noconnection:
1839 /*
259b8ea0
MD
1840 * Assign the file pointer to the reserved descriptor, or clear
1841 * the reserved descriptor if an error occured.
78812139 1842 */
fa541be6 1843 if (error)
259b8ea0
MD
1844 fsetfd(p, NULL, fd);
1845 else
1846 fsetfd(p, nfp, fd);
78812139
EN
1847 crit_exit();
1848 /*
1849 * Release explicitly held references before returning.
1850 */
1851done:
1852 if (nfp != NULL)
9f87144f
MD
1853 fdrop(nfp);
1854 fdrop(lfp);
78812139
EN
1855 return (error);
1856#else /* SCTP */
1857 return(EOPNOTSUPP);
1858#endif /* SCTP */
1859}