]> git.saurik.com Git - apple/xnu.git/blob - bsd/kern/uipc_syscalls.c
e2902e519555e3f3652121b38ba7f26c40a00f34
[apple/xnu.git] / bsd / kern / uipc_syscalls.c
1 /*
2 * Copyright (c) 2000-2003 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 /*
26 * Copyright (c) 1982, 1986, 1989, 1990, 1993
27 * The Regents of the University of California. All rights reserved.
28 *
29 * sendfile(2) and related extensions:
30 * Copyright (c) 1998, David Greenman. All rights reserved.
31 *
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
34 * are met:
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
59 *
60 * @(#)uipc_syscalls.c 8.4 (Berkeley) 2/21/94
61 */
62
63
64
65 #include <sys/param.h>
66 #include <sys/systm.h>
67 #include <sys/filedesc.h>
68 #include <sys/proc.h>
69 #include <sys/file.h>
70 #include <sys/buf.h>
71 #include <sys/malloc.h>
72 #include <sys/mbuf.h>
73 #include <sys/protosw.h>
74 #include <sys/socket.h>
75 #include <sys/socketvar.h>
76 #if KTRACE
77 #include <sys/ktrace.h>
78 #endif
79 #include <sys/kernel.h>
80 #include <sys/kern_audit.h>
81
82 #include <sys/kdebug.h>
83
84 #if KDEBUG
85
86 #define DBG_LAYER_IN_BEG NETDBG_CODE(DBG_NETSOCK, 0)
87 #define DBG_LAYER_IN_END NETDBG_CODE(DBG_NETSOCK, 2)
88 #define DBG_LAYER_OUT_BEG NETDBG_CODE(DBG_NETSOCK, 1)
89 #define DBG_LAYER_OUT_END NETDBG_CODE(DBG_NETSOCK, 3)
90 #define DBG_FNC_SENDMSG NETDBG_CODE(DBG_NETSOCK, (1 << 8) | 1)
91 #define DBG_FNC_SENDTO NETDBG_CODE(DBG_NETSOCK, (2 << 8) | 1)
92 #define DBG_FNC_SENDIT NETDBG_CODE(DBG_NETSOCK, (3 << 8) | 1)
93 #define DBG_FNC_RECVFROM NETDBG_CODE(DBG_NETSOCK, (5 << 8))
94 #define DBG_FNC_RECVMSG NETDBG_CODE(DBG_NETSOCK, (6 << 8))
95 #define DBG_FNC_RECVIT NETDBG_CODE(DBG_NETSOCK, (7 << 8))
96
97 #endif
98
99 struct getsockname_args {
100 int fdes;
101 caddr_t asa;
102 socklen_t *alen;
103 };
104
105 struct getsockopt_args {
106 int s;
107 int level;
108 int name;
109 caddr_t val;
110 socklen_t *avalsize;
111 } ;
112
113 struct accept_args {
114 int s;
115 caddr_t name;
116 socklen_t *anamelen;
117 };
118
119 struct getpeername_args {
120 int fdes;
121 caddr_t asa;
122 socklen_t *alen;
123 };
124
125
126 /* ARGSUSED */
127
128 #if SENDFILE
129 static void sf_buf_init(void *arg);
130 SYSINIT(sock_sf, SI_SUB_MBUF, SI_ORDER_ANY, sf_buf_init, NULL)
131 static struct sf_buf *sf_buf_alloc(void);
132 static void sf_buf_ref(caddr_t addr, u_int size);
133 static void sf_buf_free(caddr_t addr, u_int size);
134
135 static SLIST_HEAD(, sf_buf) sf_freelist;
136 static vm_offset_t sf_base;
137 static struct sf_buf *sf_bufs;
138 static int sf_buf_alloc_want;
139 #endif
140
141 static int sendit __P((struct proc *p, int s, struct msghdr *mp, int flags, register_t *retval));
142 static int recvit __P((struct proc *p, int s, struct msghdr *mp,
143 caddr_t namelenp, register_t *retval));
144
145 static int accept1 __P((struct proc *p, struct accept_args *uap, register_t *retval, int compat));
146 static int getsockname1 __P((struct proc *p, struct getsockname_args *uap,
147 register_t *retval, int compat));
148 static int getpeername1 __P((struct proc *p, struct getpeername_args *uap,
149 register_t *retval, int compat));
150
151 /*
152 * System call interface to the socket abstraction.
153 */
154 #if COMPAT_43 || defined(COMPAT_SUNOS)
155 #define COMPAT_OLDSOCK
156 #endif
157
158 extern struct fileops socketops;
159
160 struct socket_args {
161 int domain;
162 int type;
163 int protocol;
164 };
165 int
166 socket(p, uap, retval)
167 struct proc *p;
168 register struct socket_args *uap;
169 register_t *retval;
170 {
171 struct filedesc *fdp = p->p_fd;
172 struct socket *so;
173 struct file *fp;
174 int fd, error;
175
176 AUDIT_ARG(socket, uap->domain, uap->type, uap->protocol);
177 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
178 error = falloc(p, &fp, &fd);
179 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
180
181 if (error)
182 return (error);
183 fp->f_flag = FREAD|FWRITE;
184 fp->f_type = DTYPE_SOCKET;
185 fp->f_ops = &socketops;
186 if (error = socreate(uap->domain, &so, uap->type,
187 uap->protocol)) {
188 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
189 fdrelse(p, fd);
190 ffree(fp);
191 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
192 } else {
193 fp->f_data = (caddr_t)so;
194 *fdflags(p, fd) &= ~UF_RESERVED;
195 *retval = fd;
196 }
197 return (error);
198 }
199
200 struct bind_args {
201 int s;
202 caddr_t name;
203 socklen_t namelen;
204 };
205
206 /* ARGSUSED */
207 int
208 bind(p, uap, retval)
209 struct proc *p;
210 register struct bind_args *uap;
211 register_t *retval;
212 {
213 struct file *fp;
214 struct sockaddr *sa;
215 int error;
216
217 AUDIT_ARG(fd, uap->s);
218 error = getsock(p->p_fd, uap->s, &fp);
219 if (error)
220 return (error);
221 error = getsockaddr(&sa, uap->name, uap->namelen);
222 if (error)
223 return (error);
224 AUDIT_ARG(sockaddr, p, sa);
225 if (fp->f_data != NULL)
226 error = sobind((struct socket *)fp->f_data, sa);
227 else
228 error = EBADF;
229 FREE(sa, M_SONAME);
230 return (error);
231 }
232
233 struct listen_args {
234 int s;
235 int backlog;
236 };
237
238
239
240 int
241 listen(p, uap, retval)
242 struct proc *p;
243 register struct listen_args *uap;
244 register_t *retval;
245 {
246 struct file *fp;
247 int error;
248
249 AUDIT_ARG(fd, uap->s);
250 error = getsock(p->p_fd, uap->s, &fp);
251 if (error)
252 return (error);
253 if (fp->f_data != NULL)
254 return (solisten((struct socket *)fp->f_data, uap->backlog));
255 else
256 return (EBADF);
257 }
258
259 #ifndef COMPAT_OLDSOCK
260 #define accept1 accept
261 #endif
262
263
264
265 int
266 accept1(p, uap, retval, compat)
267 struct proc *p;
268 register struct accept_args *uap;
269 register_t *retval;
270 int compat;
271 {
272 struct file *fp;
273 struct sockaddr *sa;
274 u_int namelen;
275 int error, s;
276 struct socket *head, *so;
277 int fd;
278 short fflag; /* type must match fp->f_flag */
279 int tmpfd;
280
281 AUDIT_ARG(fd, uap->s);
282 if (uap->name) {
283 error = copyin((caddr_t)uap->anamelen, (caddr_t)&namelen,
284 sizeof (namelen));
285 if(error)
286 return (error);
287 }
288 error = getsock(p->p_fd, uap->s, &fp);
289 if (error)
290 return (error);
291 s = splnet();
292 head = (struct socket *)fp->f_data;
293 if (head == NULL) {
294 splx(s);
295 return (EBADF);
296 }
297 if ((head->so_options & SO_ACCEPTCONN) == 0) {
298 splx(s);
299 return (EINVAL);
300 }
301 if ((head->so_state & SS_NBIO) && head->so_comp.tqh_first == NULL) {
302 splx(s);
303 return (EWOULDBLOCK);
304 }
305 while (TAILQ_EMPTY(&head->so_comp) && head->so_error == 0) {
306 if (head->so_state & SS_CANTRCVMORE) {
307 head->so_error = ECONNABORTED;
308 break;
309 }
310 error = tsleep((caddr_t)&head->so_timeo, PSOCK | PCATCH,
311 "accept", 0);
312 if (error) {
313 splx(s);
314 return (error);
315 }
316 }
317 if (head->so_error) {
318 error = head->so_error;
319 head->so_error = 0;
320 splx(s);
321 return (error);
322 }
323
324
325 /*
326 * At this point we know that there is at least one connection
327 * ready to be accepted. Remove it from the queue prior to
328 * allocating the file descriptor for it since falloc() may
329 * block allowing another process to accept the connection
330 * instead.
331 */
332 so = TAILQ_FIRST(&head->so_comp);
333 TAILQ_REMOVE(&head->so_comp, so, so_list);
334 head->so_qlen--;
335
336 fflag = fp->f_flag;
337 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
338 error = falloc(p, &fp, &fd);
339 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
340 if (error) {
341 /*
342 * Probably ran out of file descriptors. Put the
343 * unaccepted connection back onto the queue and
344 * do another wakeup so some other process might
345 * have a chance at it.
346 */
347 TAILQ_INSERT_HEAD(&head->so_comp, so, so_list);
348 head->so_qlen++;
349 wakeup_one(&head->so_timeo);
350 splx(s);
351 return (error);
352 } else {
353 *fdflags(p, fd) &= ~UF_RESERVED;
354 *retval = fd;
355 }
356
357 so->so_state &= ~SS_COMP;
358 so->so_head = NULL;
359 fp->f_type = DTYPE_SOCKET;
360 fp->f_flag = fflag;
361 fp->f_ops = &socketops;
362 fp->f_data = (caddr_t)so;
363 sa = 0;
364 (void) soaccept(so, &sa);
365 if (sa == 0) {
366 namelen = 0;
367 if (uap->name)
368 goto gotnoname;
369 return 0;
370 }
371 AUDIT_ARG(sockaddr, p, sa);
372 if (uap->name) {
373 /* check sa_len before it is destroyed */
374 if (namelen > sa->sa_len)
375 namelen = sa->sa_len;
376 #ifdef COMPAT_OLDSOCK
377 if (compat)
378 ((struct osockaddr *)sa)->sa_family =
379 sa->sa_family;
380 #endif
381 error = copyout(sa, (caddr_t)uap->name, (u_int)namelen);
382 if (!error)
383 gotnoname:
384 error = copyout((caddr_t)&namelen,
385 (caddr_t)uap->anamelen, sizeof (*uap->anamelen));
386 }
387 FREE(sa, M_SONAME);
388 splx(s);
389 return (error);
390 }
391
392 int
393 accept(p, uap, retval)
394 struct proc *p;
395 struct accept_args *uap;
396 register_t *retval;
397 {
398
399 return (accept1(p, uap, retval, 0));
400 }
401
402 #ifdef COMPAT_OLDSOCK
403 int
404 oaccept(p, uap, retval)
405 struct proc *p;
406 struct accept_args *uap;
407 register_t *retval;
408 {
409
410 return (accept1(p, uap, retval, 1));
411 }
412 #endif /* COMPAT_OLDSOCK */
413
414 struct connect_args {
415 int s;
416 caddr_t name;
417 socklen_t namelen;
418 };
419 /* ARGSUSED */
420 int
421 connect(p, uap, retval)
422 struct proc *p;
423 register struct connect_args *uap;
424 register_t *retval;
425 {
426 struct file *fp;
427 register struct socket *so;
428 struct sockaddr *sa;
429 int error, s;
430
431 AUDIT_ARG(fd, uap->s);
432 error = getsock(p->p_fd, uap->s, &fp);
433 if (error)
434 return (error);
435 so = (struct socket *)fp->f_data;
436 if (so == NULL)
437 return (EBADF);
438 if ((so->so_state & SS_NBIO) && (so->so_state & SS_ISCONNECTING))
439 return (EALREADY);
440 error = getsockaddr(&sa, uap->name, uap->namelen);
441 if (error)
442 return (error);
443 AUDIT_ARG(sockaddr, p, sa);
444 error = soconnect(so, sa);
445 if (error)
446 goto bad;
447 if ((so->so_state & SS_NBIO) && (so->so_state & SS_ISCONNECTING)) {
448 FREE(sa, M_SONAME);
449 return (EINPROGRESS);
450 }
451 s = splnet();
452 while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
453 error = tsleep((caddr_t)&so->so_timeo, PSOCK | PCATCH,
454 "connec", 0);
455 if (error)
456 break;
457 }
458 if (error == 0) {
459 error = so->so_error;
460 so->so_error = 0;
461 }
462 splx(s);
463 bad:
464 so->so_state &= ~SS_ISCONNECTING;
465 FREE(sa, M_SONAME);
466 if (error == ERESTART)
467 error = EINTR;
468 return (error);
469 }
470
471 struct socketpair_args {
472 int domain;
473 int type;
474 int protocol;
475 int *rsv;
476 };
477 int
478 socketpair(p, uap, retval)
479 struct proc *p;
480 register struct socketpair_args *uap;
481 register_t *retval;
482 {
483 register struct filedesc *fdp = p->p_fd;
484 struct file *fp1, *fp2;
485 struct socket *so1, *so2;
486 int fd, error, sv[2];
487
488 AUDIT_ARG(socket, uap->domain, uap->type, uap->protocol);
489 error = socreate(uap->domain, &so1, uap->type, uap->protocol);
490 if (error)
491 return (error);
492 error = socreate(uap->domain, &so2, uap->type, uap->protocol);
493 if (error)
494 goto free1;
495 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
496 error = falloc(p, &fp1, &fd);
497 if (error)
498 goto free2;
499 sv[0] = fd;
500 fp1->f_flag = FREAD|FWRITE;
501 fp1->f_type = DTYPE_SOCKET;
502 fp1->f_ops = &socketops;
503 fp1->f_data = (caddr_t)so1;
504 error = falloc(p, &fp2, &fd);
505 if (error)
506 goto free3;
507 fp2->f_flag = FREAD|FWRITE;
508 fp2->f_type = DTYPE_SOCKET;
509 fp2->f_ops = &socketops;
510 fp2->f_data = (caddr_t)so2;
511 sv[1] = fd;
512 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
513 error = soconnect2(so1, so2);
514 if (error) {
515 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
516 goto free4;
517 }
518
519 if (uap->type == SOCK_DGRAM) {
520 /*
521 * Datagram socket connection is asymmetric.
522 */
523 error = soconnect2(so2, so1);
524 if (error) {
525 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
526 goto free4;
527 }
528 }
529 *fdflags(p, sv[0]) &= ~UF_RESERVED;
530 *fdflags(p, sv[1]) &= ~UF_RESERVED;
531 error = copyout((caddr_t)sv, (caddr_t)uap->rsv,
532 2 * sizeof (int));
533 #if 0 /* old pipe(2) syscall compatability, unused these days */
534 retval[0] = sv[0]; /* XXX ??? */
535 retval[1] = sv[1]; /* XXX ??? */
536 #endif /* 0 */
537 return (error);
538 free4:
539 fdrelse(p, sv[1]);
540 ffree(fp2);
541 free3:
542 fdrelse(p, sv[0]);
543 ffree(fp1);
544 free2:
545 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
546 (void)soclose(so2);
547 free1:
548 (void)soclose(so1);
549 return (error);
550 }
551
552 static int
553 sendit(p, s, mp, flags, retsize)
554 register struct proc *p;
555 int s;
556 register struct msghdr *mp;
557 int flags;
558 register_t *retsize;
559 {
560 struct file *fp;
561 struct uio auio;
562 register struct iovec *iov;
563 register int i;
564 struct mbuf *control;
565 struct sockaddr *to;
566 int len, error;
567 struct socket *so;
568 #if KTRACE
569 struct iovec *ktriov = NULL;
570 struct uio ktruio;
571 #endif
572
573 KERNEL_DEBUG(DBG_FNC_SENDIT | DBG_FUNC_START, 0,0,0,0,0);
574
575 if (error = getsock(p->p_fd, s, &fp))
576 {
577 KERNEL_DEBUG(DBG_FNC_SENDIT | DBG_FUNC_END, error,0,0,0,0);
578 return (error);
579 }
580
581 auio.uio_iov = mp->msg_iov;
582 auio.uio_iovcnt = mp->msg_iovlen;
583 auio.uio_segflg = UIO_USERSPACE;
584 auio.uio_rw = UIO_WRITE;
585 auio.uio_procp = p;
586 auio.uio_offset = 0; /* XXX */
587 auio.uio_resid = 0;
588 iov = mp->msg_iov;
589 for (i = 0; i < mp->msg_iovlen; i++, iov++) {
590 if (iov->iov_len < 0)
591 {
592 KERNEL_DEBUG(DBG_FNC_SENDIT | DBG_FUNC_END, EINVAL,0,0,0,0);
593 return (EINVAL);
594 }
595
596 if ((auio.uio_resid += iov->iov_len) < 0)
597 {
598 KERNEL_DEBUG(DBG_FNC_SENDIT | DBG_FUNC_END, EINVAL,0,0,0,0);
599 return (EINVAL);
600 }
601 }
602 if (mp->msg_name) {
603 error = getsockaddr(&to, mp->msg_name, mp->msg_namelen);
604 if (error) {
605 KERNEL_DEBUG(DBG_FNC_SENDIT | DBG_FUNC_END, error,0,0,0,0);
606 return (error);
607 }
608 AUDIT_ARG(sockaddr, p, to);
609 } else
610 to = 0;
611 if (mp->msg_control) {
612 if (mp->msg_controllen < sizeof(struct cmsghdr)
613 #ifdef COMPAT_OLDSOCK
614 && mp->msg_flags != MSG_COMPAT
615 #endif
616 ) {
617 error = EINVAL;
618 goto bad;
619 }
620 error = sockargs(&control, mp->msg_control,
621 mp->msg_controllen, MT_CONTROL);
622 if (error)
623 goto bad;
624 #ifdef COMPAT_OLDSOCK
625 if (mp->msg_flags == MSG_COMPAT) {
626 register struct cmsghdr *cm;
627
628 M_PREPEND(control, sizeof(*cm), M_WAIT);
629 if (control == 0) {
630 error = ENOBUFS;
631 goto bad;
632 } else {
633 cm = mtod(control, struct cmsghdr *);
634 cm->cmsg_len = control->m_len;
635 cm->cmsg_level = SOL_SOCKET;
636 cm->cmsg_type = SCM_RIGHTS;
637 }
638 }
639 #endif
640 } else
641 control = 0;
642
643 #if KTRACE
644 if (KTRPOINT(p, KTR_GENIO)) {
645 int iovlen = auio.uio_iovcnt * sizeof (struct iovec);
646
647 MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
648 bcopy((caddr_t)auio.uio_iov, (caddr_t)ktriov, iovlen);
649 ktruio = auio;
650 }
651 #endif
652 len = auio.uio_resid;
653 so = (struct socket *)fp->f_data;
654 if (so == NULL)
655 error = EBADF;
656 else
657 error = so->so_proto->pr_usrreqs->pru_sosend(so, to, &auio, 0, control,
658 flags);
659 if (error) {
660 if (auio.uio_resid != len && (error == ERESTART ||
661 error == EINTR || error == EWOULDBLOCK))
662 error = 0;
663 /* Generation of SIGPIPE can be controlled per socket */
664 if (error == EPIPE && !(so->so_flags & SOF_NOSIGPIPE))
665 psignal(p, SIGPIPE);
666 }
667 if (error == 0)
668 *retsize = len - auio.uio_resid;
669 #if KTRACE
670 if (ktriov != NULL) {
671 if (error == 0) {
672 ktruio.uio_iov = ktriov;
673 ktruio.uio_resid = retsize[0];
674 ktrgenio(p->p_tracep, s, UIO_WRITE, &ktruio, error, -1);
675 }
676 FREE(ktriov, M_TEMP);
677 }
678 #endif
679 bad:
680 if (to)
681 FREE(to, M_SONAME);
682 KERNEL_DEBUG(DBG_FNC_SENDIT | DBG_FUNC_END, error,0,0,0,0);
683 return (error);
684 }
685
686
687 struct sendto_args {
688 int s;
689 caddr_t buf;
690 size_t len;
691 int flags;
692 caddr_t to;
693 int tolen;
694 };
695
696 int
697 sendto(p, uap, retval)
698 struct proc *p;
699 register struct sendto_args /* {
700 int s;
701 caddr_t buf;
702 size_t len;
703 int flags;
704 caddr_t to;
705 int tolen;
706 } */ *uap;
707 register_t *retval;
708
709 {
710 struct msghdr msg;
711 struct iovec aiov;
712 int stat;
713
714 KERNEL_DEBUG(DBG_FNC_SENDTO | DBG_FUNC_START, 0,0,0,0,0);
715 AUDIT_ARG(fd, uap->s);
716
717 msg.msg_name = uap->to;
718 msg.msg_namelen = uap->tolen;
719 msg.msg_iov = &aiov;
720 msg.msg_iovlen = 1;
721 msg.msg_control = 0;
722 #ifdef COMPAT_OLDSOCK
723 msg.msg_flags = 0;
724 #endif
725 aiov.iov_base = uap->buf;
726 aiov.iov_len = uap->len;
727 stat = sendit(p, uap->s, &msg, uap->flags, retval);
728 KERNEL_DEBUG(DBG_FNC_SENDTO | DBG_FUNC_END, stat, *retval,0,0,0);
729 return(stat);
730 }
731
732 #ifdef COMPAT_OLDSOCK
733 struct osend_args {
734 int s;
735 caddr_t buf;
736 int len;
737 int flags;
738 };
739
740 int
741 osend(p, uap, retval)
742 struct proc *p;
743 register struct osend_args /* {
744 int s;
745 caddr_t buf;
746 int len;
747 int flags;
748 } */ *uap;
749 register_t *retval;
750
751 {
752 struct msghdr msg;
753 struct iovec aiov;
754
755 msg.msg_name = 0;
756 msg.msg_namelen = 0;
757 msg.msg_iov = &aiov;
758 msg.msg_iovlen = 1;
759 aiov.iov_base = uap->buf;
760 aiov.iov_len = uap->len;
761 msg.msg_control = 0;
762 msg.msg_flags = 0;
763 return (sendit(p, uap->s, &msg, uap->flags, retval));
764 }
765 struct osendmsg_args {
766 int s;
767 caddr_t msg;
768 int flags;
769 };
770
771 int
772 osendmsg(p, uap, retval)
773 struct proc *p;
774 register struct osendmsg_args /* {
775 int s;
776 caddr_t msg;
777 int flags;
778 } */ *uap;
779 register_t *retval;
780
781 {
782 struct msghdr msg;
783 struct iovec aiov[UIO_SMALLIOV], *iov;
784 int error;
785
786 error = copyin(uap->msg, (caddr_t)&msg, sizeof (struct omsghdr));
787 if (error)
788 return (error);
789 if ((u_int)msg.msg_iovlen >= UIO_SMALLIOV) {
790 if ((u_int)msg.msg_iovlen >= UIO_MAXIOV)
791 return (EMSGSIZE);
792 MALLOC(iov, struct iovec *,
793 sizeof(struct iovec) * (u_int)msg.msg_iovlen, M_IOV,
794 M_WAITOK);
795 } else
796 iov = aiov;
797 error = copyin((caddr_t)msg.msg_iov, (caddr_t)iov,
798 (unsigned)(msg.msg_iovlen * sizeof (struct iovec)));
799 if (error)
800 goto done;
801 msg.msg_flags = MSG_COMPAT;
802 msg.msg_iov = iov;
803 error = sendit(p, uap->s, &msg, uap->flags, retval);
804 done:
805 if (iov != aiov)
806 FREE(iov, M_IOV);
807 return (error);
808 }
809 #endif
810
811 struct sendmsg_args {
812 int s;
813 caddr_t msg;
814 int flags;
815 };
816
817 int
818 sendmsg(p, uap, retval)
819 struct proc *p;
820 register struct sendmsg_args *uap;
821 register_t *retval;
822 {
823 struct msghdr msg;
824 struct iovec aiov[UIO_SMALLIOV], *iov;
825 int error;
826
827 KERNEL_DEBUG(DBG_FNC_SENDMSG | DBG_FUNC_START, 0,0,0,0,0);
828 AUDIT_ARG(fd, uap->s);
829 if (error = copyin(uap->msg, (caddr_t)&msg, sizeof (msg)))
830 {
831 KERNEL_DEBUG(DBG_FNC_SENDMSG | DBG_FUNC_END, error,0,0,0,0);
832 return (error);
833 }
834
835 if ((u_int)msg.msg_iovlen >= UIO_SMALLIOV) {
836 if ((u_int)msg.msg_iovlen >= UIO_MAXIOV) {
837 KERNEL_DEBUG(DBG_FNC_SENDMSG | DBG_FUNC_END, EMSGSIZE,0,0,0,0);
838 return (EMSGSIZE);
839 }
840 MALLOC(iov, struct iovec *,
841 sizeof(struct iovec) * (u_int)msg.msg_iovlen, M_IOV,
842 M_WAITOK);
843 } else
844 iov = aiov;
845 if (msg.msg_iovlen &&
846 (error = copyin((caddr_t)msg.msg_iov, (caddr_t)iov,
847 (unsigned)(msg.msg_iovlen * sizeof (struct iovec)))))
848 goto done;
849 msg.msg_iov = iov;
850 #ifdef COMPAT_OLDSOCK
851 msg.msg_flags = 0;
852 #endif
853 error = sendit(p, uap->s, &msg, uap->flags, retval);
854 done:
855 if (iov != aiov)
856 FREE(iov, M_IOV);
857 KERNEL_DEBUG(DBG_FNC_SENDMSG | DBG_FUNC_END, error,0,0,0,0);
858 return (error);
859 }
860
861 static int
862 recvit(p, s, mp, namelenp, retval)
863 register struct proc *p;
864 int s;
865 register struct msghdr *mp;
866 caddr_t namelenp;
867 register_t *retval;
868 {
869 struct file *fp;
870 struct uio auio;
871 register struct iovec *iov;
872 register int i;
873 int len, error;
874 struct mbuf *m, *control = 0;
875 caddr_t ctlbuf;
876 struct socket *so;
877 struct sockaddr *fromsa = 0;
878 #if KTRACE
879 struct iovec *ktriov = NULL;
880 struct uio ktruio;
881 #endif
882
883 KERNEL_DEBUG(DBG_FNC_RECVIT | DBG_FUNC_START, 0,0,0,0,0);
884 if (error = getsock(p->p_fd, s, &fp))
885 {
886 KERNEL_DEBUG(DBG_FNC_RECVIT | DBG_FUNC_END, error,0,0,0,0);
887 return (error);
888 }
889
890 auio.uio_iov = mp->msg_iov;
891 auio.uio_iovcnt = mp->msg_iovlen;
892 auio.uio_segflg = UIO_USERSPACE;
893 auio.uio_rw = UIO_READ;
894 auio.uio_procp = p;
895 auio.uio_offset = 0; /* XXX */
896 auio.uio_resid = 0;
897 iov = mp->msg_iov;
898 for (i = 0; i < mp->msg_iovlen; i++, iov++) {
899 if ((auio.uio_resid += iov->iov_len) < 0) {
900 KERNEL_DEBUG(DBG_FNC_RECVIT | DBG_FUNC_END, EINVAL,0,0,0,0);
901 return (EINVAL);
902 }
903 }
904 #if KTRACE
905 if (KTRPOINT(p, KTR_GENIO)) {
906 int iovlen = auio.uio_iovcnt * sizeof (struct iovec);
907
908 MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
909 bcopy((caddr_t)auio.uio_iov, (caddr_t)ktriov, iovlen);
910 ktruio = auio;
911 }
912 #endif
913 len = auio.uio_resid;
914 so = (struct socket *)fp->f_data;
915 if (so == NULL)
916 error = EBADF;
917 else
918 error = so->so_proto->pr_usrreqs->pru_soreceive(so, &fromsa, &auio,
919 (struct mbuf **)0, mp->msg_control ? &control : (struct mbuf **)0,
920 &mp->msg_flags);
921 AUDIT_ARG(sockaddr, p, fromsa);
922 if (error) {
923 if (auio.uio_resid != len && (error == ERESTART ||
924 error == EINTR || error == EWOULDBLOCK))
925 error = 0;
926 }
927 #if KTRACE
928 if (ktriov != NULL) {
929 if (error == 0) {
930 ktruio.uio_iov = ktriov;
931 ktruio.uio_resid = len - auio.uio_resid;
932 ktrgenio(p->p_tracep, s, UIO_WRITE, &ktruio, error, -1);
933 }
934 FREE(ktriov, M_TEMP);
935 }
936 #endif
937 if (error)
938 goto out;
939 *retval = len - auio.uio_resid;
940 if (mp->msg_name) {
941 len = mp->msg_namelen;
942 if (len <= 0 || fromsa == 0)
943 len = 0;
944 else {
945 #ifndef MIN
946 #define MIN(a,b) ((a)>(b)?(b):(a))
947 #endif
948 /* save sa_len before it is destroyed by MSG_COMPAT */
949 len = MIN(len, fromsa->sa_len);
950 #ifdef COMPAT_OLDSOCK
951 if (mp->msg_flags & MSG_COMPAT)
952 ((struct osockaddr *)fromsa)->sa_family =
953 fromsa->sa_family;
954 #endif
955 error = copyout(fromsa,
956 (caddr_t)mp->msg_name, (unsigned)len);
957 if (error)
958 goto out;
959 }
960 mp->msg_namelen = len;
961 if (namelenp &&
962 (error = copyout((caddr_t)&len, namelenp, sizeof (int)))) {
963 #ifdef COMPAT_OLDSOCK
964 if (mp->msg_flags & MSG_COMPAT)
965 error = 0; /* old recvfrom didn't check */
966 else
967 #endif
968 goto out;
969 }
970 }
971 if (mp->msg_control) {
972 #ifdef COMPAT_OLDSOCK
973 /*
974 * We assume that old recvmsg calls won't receive access
975 * rights and other control info, esp. as control info
976 * is always optional and those options didn't exist in 4.3.
977 * If we receive rights, trim the cmsghdr; anything else
978 * is tossed.
979 */
980 if (control && mp->msg_flags & MSG_COMPAT) {
981 if (mtod(control, struct cmsghdr *)->cmsg_level !=
982 SOL_SOCKET ||
983 mtod(control, struct cmsghdr *)->cmsg_type !=
984 SCM_RIGHTS) {
985 mp->msg_controllen = 0;
986 goto out;
987 }
988 control->m_len -= sizeof (struct cmsghdr);
989 control->m_data += sizeof (struct cmsghdr);
990 }
991 #endif
992 len = mp->msg_controllen;
993 m = control;
994 mp->msg_controllen = 0;
995 ctlbuf = (caddr_t) mp->msg_control;
996
997 while (m && len > 0) {
998 unsigned int tocopy;
999
1000 if (len >= m->m_len)
1001 tocopy = m->m_len;
1002 else {
1003 mp->msg_flags |= MSG_CTRUNC;
1004 tocopy = len;
1005 }
1006
1007 if (error = copyout((caddr_t)mtod(m, caddr_t),
1008 ctlbuf, tocopy))
1009 goto out;
1010
1011 ctlbuf += tocopy;
1012 len -= tocopy;
1013 m = m->m_next;
1014 }
1015 mp->msg_controllen = ctlbuf - mp->msg_control;
1016 }
1017 out:
1018 if (fromsa)
1019 FREE(fromsa, M_SONAME);
1020 if (control)
1021 m_freem(control);
1022 KERNEL_DEBUG(DBG_FNC_RECVIT | DBG_FUNC_END, error,0,0,0,0);
1023 return (error);
1024 }
1025
1026
1027 struct recvfrom_args {
1028 int s;
1029 caddr_t buf;
1030 size_t len;
1031 int flags;
1032 caddr_t from;
1033 int *fromlenaddr;
1034 };
1035
1036 int
1037 recvfrom(p, uap, retval)
1038 struct proc *p;
1039 register struct recvfrom_args /* {
1040 int s;
1041 caddr_t buf;
1042 size_t len;
1043 int flags;
1044 caddr_t from;
1045 int *fromlenaddr;
1046 } */ *uap;
1047 register_t *retval;
1048 {
1049 struct msghdr msg;
1050 struct iovec aiov;
1051 int error;
1052
1053 KERNEL_DEBUG(DBG_FNC_RECVFROM | DBG_FUNC_START, 0,0,0,0,0);
1054 AUDIT_ARG(fd, uap->s);
1055
1056 if (uap->fromlenaddr) {
1057 error = copyin((caddr_t)uap->fromlenaddr,
1058 (caddr_t)&msg.msg_namelen, sizeof (msg.msg_namelen));
1059 if (error)
1060 return (error);
1061 } else
1062 msg.msg_namelen = 0;
1063 msg.msg_name = uap->from;
1064 msg.msg_iov = &aiov;
1065 msg.msg_iovlen = 1;
1066 aiov.iov_base = uap->buf;
1067 aiov.iov_len = uap->len;
1068 msg.msg_control = 0;
1069 msg.msg_flags = uap->flags;
1070 KERNEL_DEBUG(DBG_FNC_RECVFROM | DBG_FUNC_END, error,0,0,0,0);
1071 return (recvit(p, uap->s, &msg, (caddr_t)uap->fromlenaddr, retval));
1072 }
1073
1074 #ifdef COMPAT_OLDSOCK
1075 int
1076 orecvfrom(p, uap, retval)
1077 struct proc *p;
1078 struct recvfrom_args *uap;
1079 register_t *retval;
1080 {
1081
1082 uap->flags |= MSG_COMPAT;
1083 return (recvfrom(p, uap, retval));
1084 }
1085 #endif
1086
1087
1088 #ifdef COMPAT_OLDSOCK
1089 struct orecv_args {
1090 int s;
1091 caddr_t buf;
1092 int len;
1093 int flags;
1094 };
1095
1096 int
1097 orecv(p, uap, retval)
1098 struct proc *p;
1099 struct orecv_args *uap;
1100 register_t *retval;
1101 {
1102 struct msghdr msg;
1103 struct iovec aiov;
1104
1105 msg.msg_name = 0;
1106 msg.msg_namelen = 0;
1107 msg.msg_iov = &aiov;
1108 msg.msg_iovlen = 1;
1109 aiov.iov_base = uap->buf;
1110 aiov.iov_len = uap->len;
1111 msg.msg_control = 0;
1112 msg.msg_flags = uap->flags;
1113 return (recvit(p, uap->s, &msg, (caddr_t)0, retval));
1114 }
1115
1116 /*
1117 * Old recvmsg. This code takes advantage of the fact that the old msghdr
1118 * overlays the new one, missing only the flags, and with the (old) access
1119 * rights where the control fields are now.
1120 */
1121 struct orecvmsg_args {
1122 int s;
1123 struct omsghdr *msg;
1124 int flags;
1125 };
1126
1127 int
1128 orecvmsg(p, uap, retval)
1129 struct proc *p;
1130 struct orecvmsg_args *uap;
1131 register_t *retval;
1132 {
1133 struct msghdr msg;
1134 struct iovec aiov[UIO_SMALLIOV], *iov;
1135 int error;
1136
1137 error = copyin((caddr_t)uap->msg, (caddr_t)&msg,
1138 sizeof (struct omsghdr));
1139 if (error)
1140 return (error);
1141 if ((u_int)msg.msg_iovlen >= UIO_SMALLIOV) {
1142 if ((u_int)msg.msg_iovlen >= UIO_MAXIOV)
1143 return (EMSGSIZE);
1144 MALLOC(iov, struct iovec *,
1145 sizeof(struct iovec) * (u_int)msg.msg_iovlen, M_IOV,
1146 M_WAITOK);
1147 } else
1148 iov = aiov;
1149 msg.msg_flags = uap->flags | MSG_COMPAT;
1150 error = copyin((caddr_t)msg.msg_iov, (caddr_t)iov,
1151 (unsigned)(msg.msg_iovlen * sizeof (struct iovec)));
1152 if (error)
1153 goto done;
1154 msg.msg_iov = iov;
1155 error = recvit(p, uap->s, &msg, (caddr_t)&uap->msg->msg_namelen, retval);
1156
1157 if (msg.msg_controllen && error == 0)
1158 error = copyout((caddr_t)&msg.msg_controllen,
1159 (caddr_t)&uap->msg->msg_accrightslen, sizeof (int));
1160 done:
1161 if (iov != aiov)
1162 FREE(iov, M_IOV);
1163 return (error);
1164 }
1165 #endif
1166
1167 struct recvmsg_args {
1168 int s;
1169 struct msghdr *msg;
1170 int flags;
1171 };
1172
1173 int
1174 recvmsg(p, uap, retval)
1175 struct proc *p;
1176 struct recvmsg_args *uap;
1177 register_t *retval;
1178 {
1179 struct msghdr msg;
1180 struct iovec aiov[UIO_SMALLIOV], *uiov, *iov;
1181 register int error;
1182
1183 KERNEL_DEBUG(DBG_FNC_RECVMSG | DBG_FUNC_START, 0,0,0,0,0);
1184 AUDIT_ARG(fd, uap->s);
1185 if (error = copyin((caddr_t)uap->msg, (caddr_t)&msg,
1186 sizeof (msg)))
1187 {
1188 KERNEL_DEBUG(DBG_FNC_RECVMSG | DBG_FUNC_END, error,0,0,0,0);
1189 return (error);
1190 }
1191
1192 if ((u_int)msg.msg_iovlen >= UIO_SMALLIOV) {
1193 if ((u_int)msg.msg_iovlen >= UIO_MAXIOV) {
1194 KERNEL_DEBUG(DBG_FNC_RECVMSG | DBG_FUNC_END, EMSGSIZE,0,0,0,0);
1195 return (EMSGSIZE);
1196 }
1197 MALLOC(iov, struct iovec *,
1198 sizeof(struct iovec) * (u_int)msg.msg_iovlen, M_IOV,
1199 M_WAITOK);
1200 } else
1201 iov = aiov;
1202 #ifdef COMPAT_OLDSOCK
1203 msg.msg_flags = uap->flags &~ MSG_COMPAT;
1204 #else
1205 msg.msg_flags = uap->flags;
1206 #endif
1207 uiov = msg.msg_iov;
1208 msg.msg_iov = iov;
1209 error = copyin((caddr_t)uiov, (caddr_t)iov,
1210 (unsigned)(msg.msg_iovlen * sizeof (struct iovec)));
1211 if (error)
1212 goto done;
1213 error = recvit(p, uap->s, &msg, (caddr_t)0, retval);
1214 if (!error) {
1215 msg.msg_iov = uiov;
1216 error = copyout((caddr_t)&msg, (caddr_t)uap->msg, sizeof(msg));
1217 }
1218 done:
1219 if (iov != aiov)
1220 FREE(iov, M_IOV);
1221 KERNEL_DEBUG(DBG_FNC_RECVMSG | DBG_FUNC_END, error,0,0,0,0);
1222 return (error);
1223 }
1224
1225 /* ARGSUSED */
1226 struct shutdown_args {
1227 int s;
1228 int how;
1229 };
1230
1231 int
1232 shutdown(p, uap, retval)
1233 struct proc *p;
1234 struct shutdown_args *uap;
1235 register_t *retval;
1236 {
1237 struct file *fp;
1238 int error;
1239
1240 AUDIT_ARG(fd, uap->s);
1241 error = getsock(p->p_fd, uap->s, &fp);
1242 if (error)
1243 return (error);
1244 if (fp->f_data == NULL)
1245 return (EBADF);
1246 return (soshutdown((struct socket *)fp->f_data, uap->how));
1247 }
1248
1249
1250
1251
1252
1253 /* ARGSUSED */
1254 struct setsockopt_args {
1255 int s;
1256 int level;
1257 int name;
1258 caddr_t val;
1259 socklen_t valsize;
1260 };
1261
1262 int
1263 setsockopt(p, uap, retval)
1264 struct proc *p;
1265 struct setsockopt_args *uap;
1266 register_t *retval;
1267 {
1268 struct file *fp;
1269 struct sockopt sopt;
1270 int error;
1271
1272 AUDIT_ARG(fd, uap->s);
1273 if (uap->val == 0 && uap->valsize != 0)
1274 return (EFAULT);
1275 if (uap->valsize < 0)
1276 return (EINVAL);
1277
1278 error = getsock(p->p_fd, uap->s, &fp);
1279 if (error)
1280 return (error);
1281
1282 sopt.sopt_dir = SOPT_SET;
1283 sopt.sopt_level = uap->level;
1284 sopt.sopt_name = uap->name;
1285 sopt.sopt_val = uap->val;
1286 sopt.sopt_valsize = uap->valsize;
1287 sopt.sopt_p = p;
1288
1289 if (fp->f_data == NULL)
1290 return (EBADF);
1291 return (sosetopt((struct socket *)fp->f_data, &sopt));
1292 }
1293
1294
1295
1296 int
1297 getsockopt(p, uap, retval)
1298 struct proc *p;
1299 struct getsockopt_args *uap;
1300 register_t *retval;
1301 {
1302 int valsize, error;
1303 struct file *fp;
1304 struct sockopt sopt;
1305
1306 error = getsock(p->p_fd, uap->s, &fp);
1307 if (error)
1308 return (error);
1309 if (uap->val) {
1310 error = copyin((caddr_t)uap->avalsize, (caddr_t)&valsize,
1311 sizeof (valsize));
1312 if (error)
1313 return (error);
1314 if (valsize < 0)
1315 return (EINVAL);
1316 } else
1317 valsize = 0;
1318
1319 sopt.sopt_dir = SOPT_GET;
1320 sopt.sopt_level = uap->level;
1321 sopt.sopt_name = uap->name;
1322 sopt.sopt_val = uap->val;
1323 sopt.sopt_valsize = (size_t)valsize; /* checked non-negative above */
1324 sopt.sopt_p = p;
1325
1326 if (fp->f_data == NULL)
1327 return (EBADF);
1328 error = sogetopt((struct socket *)fp->f_data, &sopt);
1329 if (error == 0) {
1330 valsize = sopt.sopt_valsize;
1331 error = copyout((caddr_t)&valsize,
1332 (caddr_t)uap->avalsize, sizeof (valsize));
1333 }
1334 return (error);
1335 }
1336
1337
1338
1339 struct pipe_args {
1340 int dummy;
1341 };
1342 /* ARGSUSED */
1343 int
1344 pipe(p, uap, retval)
1345 struct proc *p;
1346 struct pipe_args *uap;
1347 register_t *retval;
1348 {
1349 struct file *rf, *wf;
1350 struct socket *rso, *wso;
1351 int fd, error;
1352
1353 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
1354 if (error = socreate(AF_UNIX, &rso, SOCK_STREAM, 0)) {
1355 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
1356 return (error);
1357 }
1358 if (error = socreate(AF_UNIX, &wso, SOCK_STREAM, 0)) {
1359 goto free1;
1360 }
1361 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
1362 error = falloc(p, &rf, &fd);
1363 if (error)
1364 goto free2;
1365 retval[0] = fd;
1366 rf->f_flag = FREAD;
1367 rf->f_type = DTYPE_SOCKET;
1368 rf->f_ops = &socketops;
1369 rf->f_data = (caddr_t)rso;
1370 if (error = falloc(p, &wf, &fd))
1371 goto free3;
1372 wf->f_flag = FWRITE;
1373 wf->f_type = DTYPE_SOCKET;
1374 wf->f_ops = &socketops;
1375 wf->f_data = (caddr_t)wso;
1376 retval[1] = fd;
1377
1378 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
1379 error = unp_connect2(wso, rso);
1380 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
1381 if (error)
1382 goto free4;
1383 *fdflags(p, retval[0]) &= ~UF_RESERVED;
1384 *fdflags(p, retval[1]) &= ~UF_RESERVED;
1385 return (0);
1386 free4:
1387 fdrelse(p, retval[1]);
1388 ffree(wf);
1389 free3:
1390 fdrelse(p, retval[0]);
1391 ffree(rf);
1392 free2:
1393 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
1394 (void)soclose(wso);
1395 free1:
1396 (void)soclose(rso);
1397
1398 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
1399 return (error);
1400 }
1401
1402
1403 /*
1404 * Get socket name.
1405 */
1406 /* ARGSUSED */
1407 static int
1408 getsockname1(p, uap, retval, compat)
1409 struct proc *p;
1410 register struct getsockname_args *uap;
1411 register_t *retval;
1412 int compat;
1413 {
1414 struct file *fp;
1415 register struct socket *so;
1416 struct sockaddr *sa;
1417 u_int len;
1418 int error;
1419
1420 error = getsock(p->p_fd, uap->fdes, &fp);
1421 if (error)
1422 return (error);
1423 error = copyin((caddr_t)uap->alen, (caddr_t)&len, sizeof (len));
1424 if (error)
1425 return (error);
1426 so = (struct socket *)fp->f_data;
1427 if (so == NULL)
1428 return (EBADF);
1429 sa = 0;
1430 error = (*so->so_proto->pr_usrreqs->pru_sockaddr)(so, &sa);
1431 if (error)
1432 goto bad;
1433 if (sa == 0) {
1434 len = 0;
1435 goto gotnothing;
1436 }
1437
1438 len = MIN(len, sa->sa_len);
1439 #ifdef COMPAT_OLDSOCK
1440 if (compat)
1441 ((struct osockaddr *)sa)->sa_family = sa->sa_family;
1442 #endif
1443 error = copyout(sa, (caddr_t)uap->asa, (u_int)len);
1444 if (error == 0)
1445 gotnothing:
1446 error = copyout((caddr_t)&len, (caddr_t)uap->alen,
1447 sizeof (len));
1448 bad:
1449 if (sa)
1450 FREE(sa, M_SONAME);
1451 return (error);
1452 }
1453
1454 int
1455 getsockname(p, uap, retval)
1456 struct proc *p;
1457 struct getsockname_args *uap;
1458 register_t *retval;
1459 {
1460
1461 return (getsockname1(p, uap, retval, 0));
1462 }
1463
1464 #ifdef COMPAT_OLDSOCK
1465 int
1466 ogetsockname(p, uap, retval)
1467 struct proc *p;
1468 struct getsockname_args *uap;
1469 register_t *retval;
1470 {
1471
1472 return (getsockname1(p, uap, retval, 1));
1473 }
1474 #endif /* COMPAT_OLDSOCK */
1475
1476 /*
1477 * Get name of peer for connected socket.
1478 */
1479 /* ARGSUSED */
1480 int
1481 getpeername1(p, uap, retval, compat)
1482 struct proc *p;
1483 register struct getpeername_args *uap;
1484 register_t *retval;
1485 int compat;
1486 {
1487 struct file *fp;
1488 register struct socket *so;
1489 struct sockaddr *sa;
1490 u_int len;
1491 int error;
1492
1493 error = getsock(p->p_fd, uap->fdes, &fp);
1494 if (error)
1495 return (error);
1496 so = (struct socket *)fp->f_data;
1497 if (so == NULL)
1498 return (EBADF);
1499 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONFIRMING)) == 0)
1500 return (ENOTCONN);
1501 error = copyin((caddr_t)uap->alen, (caddr_t)&len, sizeof (len));
1502 if (error)
1503 return (error);
1504 sa = 0;
1505 error = (*so->so_proto->pr_usrreqs->pru_peeraddr)(so, &sa);
1506 if (error)
1507 goto bad;
1508 if (sa == 0) {
1509 len = 0;
1510 goto gotnothing;
1511 }
1512 len = MIN(len, sa->sa_len);
1513 #ifdef COMPAT_OLDSOCK
1514 if (compat)
1515 ((struct osockaddr *)sa)->sa_family =
1516 sa->sa_family;
1517 #endif
1518 error = copyout(sa, (caddr_t)uap->asa, (u_int)len);
1519 if (error)
1520 goto bad;
1521 gotnothing:
1522 error = copyout((caddr_t)&len, (caddr_t)uap->alen, sizeof (len));
1523 bad:
1524 if (sa) FREE(sa, M_SONAME);
1525 return (error);
1526 }
1527
1528 int
1529 getpeername(p, uap, retval)
1530 struct proc *p;
1531 struct getpeername_args *uap;
1532 register_t *retval;
1533 {
1534
1535 return (getpeername1(p, uap, retval, 0));
1536 }
1537
1538 #ifdef COMPAT_OLDSOCK
1539 int
1540 ogetpeername(p, uap, retval)
1541 struct proc *p;
1542 struct ogetpeername_args *uap;
1543 register_t *retval;
1544 {
1545
1546 /* XXX uap should have type `getpeername_args *' to begin with. */
1547 return (getpeername1(p, (struct getpeername_args *)uap, retval, 1));
1548 }
1549 #endif /* COMPAT_OLDSOCK */
1550
1551 int
1552 sockargs(mp, buf, buflen, type)
1553 struct mbuf **mp;
1554 caddr_t buf;
1555 int buflen, type;
1556 {
1557 register struct sockaddr *sa;
1558 register struct mbuf *m;
1559 int error;
1560
1561 if ((u_int)buflen > MLEN) {
1562 #ifdef COMPAT_OLDSOCK
1563 if (type == MT_SONAME && (u_int)buflen <= 112)
1564 buflen = MLEN; /* unix domain compat. hack */
1565 else
1566 #endif
1567 return (EINVAL);
1568 }
1569 m = m_get(M_WAIT, type);
1570 if (m == NULL)
1571 return (ENOBUFS);
1572 m->m_len = buflen;
1573 error = copyin(buf, mtod(m, caddr_t), (u_int)buflen);
1574 if (error)
1575 (void) m_free(m);
1576 else {
1577 *mp = m;
1578 if (type == MT_SONAME) {
1579 sa = mtod(m, struct sockaddr *);
1580
1581 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1582 if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
1583 sa->sa_family = sa->sa_len;
1584 #endif
1585 sa->sa_len = buflen;
1586 }
1587 }
1588 return (error);
1589 }
1590
1591 int
1592 getsockaddr(namp, uaddr, len)
1593 struct sockaddr **namp;
1594 caddr_t uaddr;
1595 size_t len;
1596 {
1597 struct sockaddr *sa;
1598 int error;
1599
1600 if (len > SOCK_MAXADDRLEN)
1601 return ENAMETOOLONG;
1602
1603 if (len == 0)
1604 return EINVAL;
1605
1606 MALLOC(sa, struct sockaddr *, len, M_SONAME, M_WAITOK);
1607 error = copyin(uaddr, sa, len);
1608 if (error) {
1609 FREE(sa, M_SONAME);
1610 } else {
1611 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1612 if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
1613 sa->sa_family = sa->sa_len;
1614 #endif
1615 sa->sa_len = len;
1616 *namp = sa;
1617 }
1618 return error;
1619 }
1620
1621 int
1622 getsock(fdp, fdes, fpp)
1623 struct filedesc *fdp;
1624 int fdes;
1625 struct file **fpp;
1626 {
1627 register struct file *fp;
1628
1629 if ((unsigned)fdes >= fdp->fd_nfiles ||
1630 (fp = fdp->fd_ofiles[fdes]) == NULL ||
1631 (fdp->fd_ofileflags[fdes] & UF_RESERVED))
1632 return (EBADF);
1633 if (fp->f_type != DTYPE_SOCKET)
1634 return (ENOTSOCK);
1635 *fpp = fp;
1636 return (0);
1637 }
1638
1639 #if SENDFILE
1640 /*
1641 * Allocate a pool of sf_bufs (sendfile(2) or "super-fast" if you prefer. :-))
1642 * XXX - The sf_buf functions are currently private to sendfile(2), so have
1643 * been made static, but may be useful in the future for doing zero-copy in
1644 * other parts of the networking code.
1645 */
1646 static void
1647 sf_buf_init(void *arg)
1648 {
1649 int i;
1650
1651 SLIST_INIT(&sf_freelist);
1652 sf_base = kmem_alloc_pageable(kernel_map, nsfbufs * PAGE_SIZE);
1653 sf_bufs = _MALLOC(nsfbufs * sizeof(struct sf_buf), M_TEMP, M_NOWAIT);
1654 bzero(sf_bufs, nsfbufs * sizeof(struct sf_buf));
1655 for (i = 0; i < nsfbufs; i++) {
1656 sf_bufs[i].kva = sf_base + i * PAGE_SIZE;
1657 SLIST_INSERT_HEAD(&sf_freelist, &sf_bufs[i], free_list);
1658 }
1659 }
1660
1661 /*
1662 * Get an sf_buf from the freelist. Will block if none are available.
1663 */
1664 static struct sf_buf *
1665 sf_buf_alloc()
1666 {
1667 struct sf_buf *sf;
1668 int s;
1669
1670 s = splimp();
1671 while ((sf = SLIST_FIRST(&sf_freelist)) == NULL) {
1672 sf_buf_alloc_want = 1;
1673 tsleep(&sf_freelist, PVM, "sfbufa", 0);
1674 }
1675 SLIST_REMOVE_HEAD(&sf_freelist, free_list);
1676 splx(s);
1677 sf->refcnt = 1;
1678 return (sf);
1679 }
1680
1681 #define dtosf(x) (&sf_bufs[((uintptr_t)(x) - (uintptr_t)sf_base) >> PAGE_SHIFT])
1682 static void
1683 sf_buf_ref(caddr_t addr, u_int size)
1684 {
1685 struct sf_buf *sf;
1686
1687 sf = dtosf(addr);
1688 if (sf->refcnt == 0)
1689 panic("sf_buf_ref: referencing a free sf_buf");
1690 sf->refcnt++;
1691 }
1692
1693 /*
1694 * Lose a reference to an sf_buf. When none left, detach mapped page
1695 * and release resources back to the system.
1696 *
1697 * Must be called at splimp.
1698 */
1699 static void
1700 sf_buf_free(caddr_t addr, u_int size)
1701 {
1702 struct sf_buf *sf;
1703 struct vm_page *m;
1704 int s;
1705
1706 sf = dtosf(addr);
1707 if (sf->refcnt == 0)
1708 panic("sf_buf_free: freeing free sf_buf");
1709 sf->refcnt--;
1710 if (sf->refcnt == 0) {
1711 pmap_qremove((vm_offset_t)addr, 1);
1712 m = sf->m;
1713 s = splvm();
1714 vm_page_unwire(m, 0);
1715 /*
1716 * Check for the object going away on us. This can
1717 * happen since we don't hold a reference to it.
1718 * If so, we're responsible for freeing the page.
1719 */
1720 if (m->wire_count == 0 && m->object == NULL)
1721 vm_page_lock_queues();
1722 vm_page_free(m);
1723 vm_page_unlock_queues();
1724 splx(s);
1725 sf->m = NULL;
1726 SLIST_INSERT_HEAD(&sf_freelist, sf, free_list);
1727 if (sf_buf_alloc_want) {
1728 sf_buf_alloc_want = 0;
1729 wakeup(&sf_freelist);
1730 }
1731 }
1732 }
1733
1734 /*
1735 * sendfile(2).
1736 * int sendfile(int fd, int s, off_t offset, size_t nbytes,
1737 * struct sf_hdtr *hdtr, off_t *sbytes, int flags)
1738 *
1739 * Send a file specified by 'fd' and starting at 'offset' to a socket
1740 * specified by 's'. Send only 'nbytes' of the file or until EOF if
1741 * nbytes == 0. Optionally add a header and/or trailer to the socket
1742 * output. If specified, write the total number of bytes sent into *sbytes.
1743 */
1744 int
1745 sendfile(struct proc *p, struct sendfile_args *uap)
1746 {
1747 struct file *fp;
1748 struct filedesc *fdp = p->p_fd;
1749 struct vnode *vp;
1750 struct vm_object *obj;
1751 struct socket *so;
1752 struct mbuf *m;
1753 struct sf_buf *sf;
1754 struct vm_page *pg;
1755 struct writev_args nuap;
1756 struct sf_hdtr hdtr;
1757 off_t off, xfsize, sbytes = 0;
1758 int error = 0, s;
1759
1760 /*
1761 * Do argument checking. Must be a regular file in, stream
1762 * type and connected socket out, positive offset.
1763 */
1764 if (((u_int)uap->fd) >= fdp->fd_nfiles ||
1765 (fp = fdp->fd_ofiles[uap->fd]) == NULL ||
1766 (fp->f_flag & FREAD) == 0) {
1767 error = EBADF;
1768 goto done;
1769 }
1770 if (fp->f_type != DTYPE_VNODE) {
1771 error = EINVAL;
1772 goto done;
1773 }
1774 vp = (struct vnode *)fp->f_data;
1775 obj = vp->v_object;
1776 if (vp->v_type != VREG || obj == NULL) {
1777 error = EINVAL;
1778 goto done;
1779 }
1780 error = getsock(p->p_fd, uap->s, &fp);
1781 if (error)
1782 goto done;
1783 so = (struct socket *)fp->f_data;
1784 if (so == NULL) {
1785 error = EBADF;
1786 goto done;
1787 }
1788 if (so->so_type != SOCK_STREAM) {
1789 error = EINVAL;
1790 goto done;
1791 }
1792 if ((so->so_state & SS_ISCONNECTED) == 0) {
1793 error = ENOTCONN;
1794 goto done;
1795 }
1796 if (uap->offset < 0) {
1797 error = EINVAL;
1798 goto done;
1799 }
1800
1801 /*
1802 * If specified, get the pointer to the sf_hdtr struct for
1803 * any headers/trailers.
1804 */
1805 if (uap->hdtr != NULL) {
1806 error = copyin(uap->hdtr, &hdtr, sizeof(hdtr));
1807 if (error)
1808 goto done;
1809 /*
1810 * Send any headers. Wimp out and use writev(2).
1811 */
1812 if (hdtr.headers != NULL) {
1813 nuap.fd = uap->s;
1814 nuap.iovp = hdtr.headers;
1815 nuap.iovcnt = hdtr.hdr_cnt;
1816 error = writev(p, &nuap);
1817 if (error)
1818 goto done;
1819 sbytes += p->p_retval[0];
1820 }
1821 }
1822
1823 /*
1824 * Protect against multiple writers to the socket.
1825 */
1826 (void) sblock(&so->so_snd, M_WAIT);
1827
1828 /*
1829 * Loop through the pages in the file, starting with the requested
1830 * offset. Get a file page (do I/O if necessary), map the file page
1831 * into an sf_buf, attach an mbuf header to the sf_buf, and queue
1832 * it on the socket.
1833 */
1834 for (off = uap->offset; ; off += xfsize, sbytes += xfsize) {
1835 vm_object_offset_t pindex;
1836 vm_object_offset_t pgoff;
1837
1838 pindex = OFF_TO_IDX(off);
1839 retry_lookup:
1840 /*
1841 * Calculate the amount to transfer. Not to exceed a page,
1842 * the EOF, or the passed in nbytes.
1843 */
1844 xfsize = obj->un_pager.vnp.vnp_size - off;
1845 if (xfsize > PAGE_SIZE_64)
1846 xfsize = PAGE_SIZE;
1847 pgoff = (vm_object_offset_t)(off & PAGE_MASK_64);
1848 if (PAGE_SIZE - pgoff < xfsize)
1849 xfsize = PAGE_SIZE_64 - pgoff;
1850 if (uap->nbytes && xfsize > (uap->nbytes - sbytes))
1851 xfsize = uap->nbytes - sbytes;
1852 if (xfsize <= 0)
1853 break;
1854 /*
1855 * Optimize the non-blocking case by looking at the socket space
1856 * before going to the extra work of constituting the sf_buf.
1857 */
1858 if ((so->so_state & SS_NBIO) && sbspace(&so->so_snd) <= 0) {
1859 if (so->so_state & SS_CANTSENDMORE)
1860 error = EPIPE;
1861 else
1862 error = EAGAIN;
1863 sbunlock(&so->so_snd);
1864 goto done;
1865 }
1866 /*
1867 * Attempt to look up the page. If the page doesn't exist or the
1868 * part we're interested in isn't valid, then read it from disk.
1869 * If some other part of the kernel has this page (i.e. it's busy),
1870 * then disk I/O may be occuring on it, so wait and retry.
1871 */
1872 pg = vm_page_lookup(obj, pindex);
1873 if (pg == NULL || (!(pg->flags & PG_BUSY) && !pg->busy &&
1874 !vm_page_is_valid(pg, pgoff, xfsize))) {
1875 struct uio auio;
1876 struct iovec aiov;
1877 int bsize;
1878
1879 if (pg == NULL) {
1880 pg = vm_page_alloc(obj, pindex, VM_ALLOC_NORMAL);
1881 if (pg == NULL) {
1882 VM_WAIT;
1883 goto retry_lookup;
1884 }
1885 /*
1886 * don't just clear PG_BUSY manually -
1887 * vm_page_alloc() should be considered opaque,
1888 * use the VM routine provided to clear
1889 * PG_BUSY.
1890 */
1891 vm_page_wakeup(pg);
1892
1893 }
1894 /*
1895 * Ensure that our page is still around when the I/O completes.
1896 */
1897 vm_page_io_start(pg);
1898 vm_page_wire(pg);
1899 /*
1900 * Get the page from backing store.
1901 */
1902 bsize = vp->v_mount->mnt_stat.f_iosize;
1903 auio.uio_iov = &aiov;
1904 auio.uio_iovcnt = 1;
1905 aiov.iov_base = 0;
1906 aiov.iov_len = MAXBSIZE;
1907 auio.uio_resid = MAXBSIZE;
1908 auio.uio_offset = trunc_page(off);
1909 auio.uio_segflg = UIO_NOCOPY;
1910 auio.uio_rw = UIO_READ;
1911 auio.uio_procp = p;
1912 vn_lock(vp, LK_SHARED | LK_NOPAUSE | LK_RETRY, p);
1913 error = VOP_READ(vp, &auio, IO_VMIO | ((MAXBSIZE / bsize) << 16),
1914 p->p_ucred);
1915 VOP_UNLOCK(vp, 0, p);
1916 vm_page_flag_clear(pg, PG_ZERO);
1917 vm_page_io_finish(pg);
1918 if (error) {
1919 vm_page_unwire(pg, 0);
1920 /*
1921 * See if anyone else might know about this page.
1922 * If not and it is not valid, then free it.
1923 */
1924 if (pg->wire_count == 0 && pg->valid == 0 &&
1925 pg->busy == 0 && !(pg->flags & PG_BUSY) &&
1926 pg->hold_count == 0)
1927 vm_page_lock_queues();
1928 vm_page_free(pg);
1929 vm_page_unlock_queues();
1930 sbunlock(&so->so_snd);
1931 goto done;
1932 }
1933 } else {
1934 if ((pg->flags & PG_BUSY) || pg->busy) {
1935 s = splvm();
1936 if ((pg->flags & PG_BUSY) || pg->busy) {
1937 /*
1938 * Page is busy. Wait and retry.
1939 */
1940 vm_page_flag_set(pg, PG_WANTED);
1941 tsleep(pg, PVM, "sfpbsy", 0);
1942 splx(s);
1943 goto retry_lookup;
1944 }
1945 splx(s);
1946 }
1947 /*
1948 * Protect from having the page ripped out from beneath us.
1949 */
1950 vm_page_wire(pg);
1951 }
1952 /*
1953 * Allocate a kernel virtual page and insert the physical page
1954 * into it.
1955 */
1956 sf = sf_buf_alloc();
1957 sf->m = pg;
1958 pmap_qenter(sf->kva, &pg, 1);
1959 /*
1960 * Get an mbuf header and set it up as having external storage.
1961 */
1962 MGETHDR(m, M_WAIT, MT_DATA);
1963 m->m_ext.ext_free = sf_buf_free;
1964 m->m_ext.ext_ref = sf_buf_ref;
1965 m->m_ext.ext_buf = (void *)sf->kva;
1966 m->m_ext.ext_size = PAGE_SIZE;
1967 m->m_data = (char *) sf->kva + pgoff;
1968 m->m_flags |= M_EXT;
1969 m->m_pkthdr.len = m->m_len = xfsize;
1970 /*
1971 * Add the buffer to the socket buffer chain.
1972 */
1973 s = splnet();
1974 retry_space:
1975 /*
1976 * Make sure that the socket is still able to take more data.
1977 * CANTSENDMORE being true usually means that the connection
1978 * was closed. so_error is true when an error was sensed after
1979 * a previous send.
1980 * The state is checked after the page mapping and buffer
1981 * allocation above since those operations may block and make
1982 * any socket checks stale. From this point forward, nothing
1983 * blocks before the pru_send (or more accurately, any blocking
1984 * results in a loop back to here to re-check).
1985 */
1986 if ((so->so_state & SS_CANTSENDMORE) || so->so_error) {
1987 if (so->so_state & SS_CANTSENDMORE) {
1988 error = EPIPE;
1989 } else {
1990 error = so->so_error;
1991 so->so_error = 0;
1992 }
1993 m_freem(m);
1994 sbunlock(&so->so_snd);
1995 splx(s);
1996 goto done;
1997 }
1998 /*
1999 * Wait for socket space to become available. We do this just
2000 * after checking the connection state above in order to avoid
2001 * a race condition with sbwait().
2002 */
2003 if (sbspace(&so->so_snd) < so->so_snd.sb_lowat) {
2004 if (so->so_state & SS_NBIO) {
2005 m_freem(m);
2006 sbunlock(&so->so_snd);
2007 splx(s);
2008 error = EAGAIN;
2009 goto done;
2010 }
2011 error = sbwait(&so->so_snd);
2012 /*
2013 * An error from sbwait usually indicates that we've
2014 * been interrupted by a signal. If we've sent anything
2015 * then return bytes sent, otherwise return the error.
2016 */
2017 if (error) {
2018 m_freem(m);
2019 sbunlock(&so->so_snd);
2020 splx(s);
2021 goto done;
2022 }
2023 goto retry_space;
2024 }
2025 error = (*so->so_proto->pr_usrreqs->pru_send)(so, 0, m, 0, 0, p);
2026 splx(s);
2027 if (error) {
2028 sbunlock(&so->so_snd);
2029 goto done;
2030 }
2031 }
2032 sbunlock(&so->so_snd);
2033
2034 /*
2035 * Send trailers. Wimp out and use writev(2).
2036 */
2037 if (uap->hdtr != NULL && hdtr.trailers != NULL) {
2038 nuap.fd = uap->s;
2039 nuap.iovp = hdtr.trailers;
2040 nuap.iovcnt = hdtr.trl_cnt;
2041 error = writev(p, &nuap);
2042 if (error)
2043 goto done;
2044 sbytes += p->p_retval[0];
2045 }
2046
2047 done:
2048 if (uap->sbytes != NULL) {
2049 copyout(&sbytes, uap->sbytes, sizeof(off_t));
2050 }
2051 return (error);
2052 }
2053
2054 #endif