uipc_syscalls.c revision 1.172.2.1 1 /* $NetBSD: uipc_syscalls.c,v 1.172.2.1 2015/08/08 15:45:47 martin Exp $ */
2
3 /*-
4 * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Andrew Doran.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * Copyright (c) 1982, 1986, 1989, 1990, 1993
34 * The Regents of the University of California. All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. 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.6 (Berkeley) 2/14/95
61 */
62
63 #include <sys/cdefs.h>
64 __KERNEL_RCSID(0, "$NetBSD: uipc_syscalls.c,v 1.172.2.1 2015/08/08 15:45:47 martin Exp $");
65
66 #include "opt_pipe.h"
67
68 #include <sys/param.h>
69 #include <sys/systm.h>
70 #include <sys/filedesc.h>
71 #include <sys/proc.h>
72 #include <sys/file.h>
73 #include <sys/buf.h>
74 #define MBUFTYPES
75 #include <sys/mbuf.h>
76 #include <sys/protosw.h>
77 #include <sys/socket.h>
78 #include <sys/socketvar.h>
79 #include <sys/signalvar.h>
80 #include <sys/un.h>
81 #include <sys/ktrace.h>
82 #include <sys/event.h>
83 #include <sys/atomic.h>
84 #include <sys/kauth.h>
85
86 #include <sys/mount.h>
87 #include <sys/syscallargs.h>
88
89 /*
90 * System call interface to the socket abstraction.
91 */
92 extern const struct fileops socketops;
93
94 int
95 sys___socket30(struct lwp *l, const struct sys___socket30_args *uap,
96 register_t *retval)
97 {
98 /* {
99 syscallarg(int) domain;
100 syscallarg(int) type;
101 syscallarg(int) protocol;
102 } */
103 int fd, error;
104
105 error = fsocreate(SCARG(uap, domain), NULL, SCARG(uap, type),
106 SCARG(uap, protocol), &fd);
107 if (error == 0) {
108 *retval = fd;
109 }
110 return error;
111 }
112
113 int
114 sys_bind(struct lwp *l, const struct sys_bind_args *uap, register_t *retval)
115 {
116 /* {
117 syscallarg(int) s;
118 syscallarg(const struct sockaddr *) name;
119 syscallarg(unsigned int) namelen;
120 } */
121 struct mbuf *nam;
122 int error;
123
124 error = sockargs(&nam, SCARG(uap, name), SCARG(uap, namelen),
125 MT_SONAME);
126 if (error)
127 return error;
128
129 return do_sys_bind(l, SCARG(uap, s), nam);
130 }
131
132 int
133 do_sys_bind(struct lwp *l, int fd, struct mbuf *nam)
134 {
135 struct socket *so;
136 int error;
137
138 if ((error = fd_getsock(fd, &so)) != 0) {
139 m_freem(nam);
140 return (error);
141 }
142 MCLAIM(nam, so->so_mowner);
143 error = sobind(so, nam, l);
144 m_freem(nam);
145 fd_putfile(fd);
146 return error;
147 }
148
149 int
150 sys_listen(struct lwp *l, const struct sys_listen_args *uap, register_t *retval)
151 {
152 /* {
153 syscallarg(int) s;
154 syscallarg(int) backlog;
155 } */
156 struct socket *so;
157 int error;
158
159 if ((error = fd_getsock(SCARG(uap, s), &so)) != 0)
160 return (error);
161 error = solisten(so, SCARG(uap, backlog), l);
162 fd_putfile(SCARG(uap, s));
163 return error;
164 }
165
166 int
167 do_sys_accept(struct lwp *l, int sock, struct mbuf **name,
168 register_t *new_sock, const sigset_t *mask, int flags, int clrflags)
169 {
170 file_t *fp, *fp2;
171 struct mbuf *nam;
172 int error, fd;
173 struct socket *so, *so2;
174 short wakeup_state = 0;
175
176 if ((fp = fd_getfile(sock)) == NULL)
177 return EBADF;
178 if (fp->f_type != DTYPE_SOCKET) {
179 fd_putfile(sock);
180 return ENOTSOCK;
181 }
182 if ((error = fd_allocfile(&fp2, &fd)) != 0) {
183 fd_putfile(sock);
184 return error;
185 }
186 nam = m_get(M_WAIT, MT_SONAME);
187 *new_sock = fd;
188 so = fp->f_data;
189 solock(so);
190
191 if (__predict_false(mask))
192 sigsuspendsetup(l, mask);
193
194 if (!(so->so_proto->pr_flags & PR_LISTEN)) {
195 error = EOPNOTSUPP;
196 goto bad;
197 }
198 if ((so->so_options & SO_ACCEPTCONN) == 0) {
199 error = EINVAL;
200 goto bad;
201 }
202 if ((so->so_state & SS_NBIO) && so->so_qlen == 0) {
203 error = EWOULDBLOCK;
204 goto bad;
205 }
206 while (so->so_qlen == 0 && so->so_error == 0) {
207 if (so->so_state & SS_CANTRCVMORE) {
208 so->so_error = ECONNABORTED;
209 break;
210 }
211 if (wakeup_state & SS_RESTARTSYS) {
212 error = ERESTART;
213 goto bad;
214 }
215 error = sowait(so, true, 0);
216 if (error) {
217 goto bad;
218 }
219 wakeup_state = so->so_state;
220 }
221 if (so->so_error) {
222 error = so->so_error;
223 so->so_error = 0;
224 goto bad;
225 }
226 /* connection has been removed from the listen queue */
227 KNOTE(&so->so_rcv.sb_sel.sel_klist, NOTE_SUBMIT);
228 so2 = TAILQ_FIRST(&so->so_q);
229 if (soqremque(so2, 1) == 0)
230 panic("accept");
231 fp2->f_type = DTYPE_SOCKET;
232 fp2->f_flag = (fp->f_flag & ~clrflags) |
233 ((flags & SOCK_NONBLOCK) ? FNONBLOCK : 0)|
234 ((flags & SOCK_NOSIGPIPE) ? FNOSIGPIPE : 0);
235 fp2->f_ops = &socketops;
236 fp2->f_data = so2;
237 if (fp2->f_flag & FNONBLOCK)
238 so2->so_state |= SS_NBIO;
239 else
240 so2->so_state &= ~SS_NBIO;
241 error = soaccept(so2, nam);
242 so2->so_cred = kauth_cred_dup(so->so_cred);
243 sounlock(so);
244 if (error) {
245 /* an error occurred, free the file descriptor and mbuf */
246 m_freem(nam);
247 mutex_enter(&fp2->f_lock);
248 fp2->f_count++;
249 mutex_exit(&fp2->f_lock);
250 closef(fp2);
251 fd_abort(curproc, NULL, fd);
252 } else {
253 fd_set_exclose(l, fd, (flags & SOCK_CLOEXEC) != 0);
254 fd_affix(curproc, fp2, fd);
255 *name = nam;
256 }
257 fd_putfile(sock);
258 if (__predict_false(mask))
259 sigsuspendteardown(l);
260 return error;
261 bad:
262 sounlock(so);
263 m_freem(nam);
264 fd_putfile(sock);
265 fd_abort(curproc, fp2, fd);
266 if (__predict_false(mask))
267 sigsuspendteardown(l);
268 return error;
269 }
270
271 int
272 sys_accept(struct lwp *l, const struct sys_accept_args *uap, register_t *retval)
273 {
274 /* {
275 syscallarg(int) s;
276 syscallarg(struct sockaddr *) name;
277 syscallarg(unsigned int *) anamelen;
278 } */
279 int error, fd;
280 struct mbuf *name;
281
282 error = do_sys_accept(l, SCARG(uap, s), &name, retval, NULL, 0, 0);
283 if (error != 0)
284 return error;
285 error = copyout_sockname(SCARG(uap, name), SCARG(uap, anamelen),
286 MSG_LENUSRSPACE, name);
287 if (name != NULL)
288 m_free(name);
289 if (error != 0) {
290 fd = (int)*retval;
291 if (fd_getfile(fd) != NULL)
292 (void)fd_close(fd);
293 }
294 return error;
295 }
296
297 int
298 sys_paccept(struct lwp *l, const struct sys_paccept_args *uap,
299 register_t *retval)
300 {
301 /* {
302 syscallarg(int) s;
303 syscallarg(struct sockaddr *) name;
304 syscallarg(unsigned int *) anamelen;
305 syscallarg(const sigset_t *) mask;
306 syscallarg(int) flags;
307 } */
308 int error, fd;
309 struct mbuf *name;
310 sigset_t *mask, amask;
311
312 if (SCARG(uap, mask) != NULL) {
313 error = copyin(SCARG(uap, mask), &amask, sizeof(amask));
314 if (error)
315 return error;
316 mask = &amask;
317 } else
318 mask = NULL;
319
320 error = do_sys_accept(l, SCARG(uap, s), &name, retval, mask,
321 SCARG(uap, flags), FNONBLOCK);
322 if (error != 0)
323 return error;
324 error = copyout_sockname(SCARG(uap, name), SCARG(uap, anamelen),
325 MSG_LENUSRSPACE, name);
326 if (name != NULL)
327 m_free(name);
328 if (error != 0) {
329 fd = (int)*retval;
330 if (fd_getfile(fd) != NULL)
331 (void)fd_close(fd);
332 }
333 return error;
334 }
335
336 int
337 sys_connect(struct lwp *l, const struct sys_connect_args *uap,
338 register_t *retval)
339 {
340 /* {
341 syscallarg(int) s;
342 syscallarg(const struct sockaddr *) name;
343 syscallarg(unsigned int) namelen;
344 } */
345 int error;
346 struct mbuf *nam;
347
348 error = sockargs(&nam, SCARG(uap, name), SCARG(uap, namelen),
349 MT_SONAME);
350 if (error)
351 return error;
352 return do_sys_connect(l, SCARG(uap, s), nam);
353 }
354
355 int
356 do_sys_connect(struct lwp *l, int fd, struct mbuf *nam)
357 {
358 struct socket *so;
359 int error;
360 int interrupted = 0;
361
362 if ((error = fd_getsock(fd, &so)) != 0) {
363 m_freem(nam);
364 return (error);
365 }
366 solock(so);
367 MCLAIM(nam, so->so_mowner);
368 if ((so->so_state & SS_ISCONNECTING) != 0) {
369 error = EALREADY;
370 goto out;
371 }
372
373 error = soconnect(so, nam, l);
374 if (error)
375 goto bad;
376 if ((so->so_state & (SS_NBIO|SS_ISCONNECTING)) ==
377 (SS_NBIO|SS_ISCONNECTING)) {
378 error = EINPROGRESS;
379 goto out;
380 }
381 while ((so->so_state & SS_ISCONNECTING) != 0 && so->so_error == 0) {
382 error = sowait(so, true, 0);
383 if (__predict_false((so->so_state & SS_ISABORTING) != 0)) {
384 error = EPIPE;
385 interrupted = 1;
386 break;
387 }
388 if (error) {
389 if (error == EINTR || error == ERESTART)
390 interrupted = 1;
391 break;
392 }
393 }
394 if (error == 0) {
395 error = so->so_error;
396 so->so_error = 0;
397 }
398 bad:
399 if (!interrupted)
400 so->so_state &= ~SS_ISCONNECTING;
401 if (error == ERESTART)
402 error = EINTR;
403 out:
404 sounlock(so);
405 fd_putfile(fd);
406 m_freem(nam);
407 return error;
408 }
409
410 static int
411 makesocket(struct lwp *l, file_t **fp, int *fd, int flags, int type,
412 int domain, int proto, struct socket *soo)
413 {
414 struct socket *so;
415 int error;
416
417 if ((error = socreate(domain, &so, type, proto, l, soo)) != 0) {
418 return error;
419 }
420 if (flags & SOCK_NONBLOCK) {
421 so->so_state |= SS_NBIO;
422 }
423
424 if ((error = fd_allocfile(fp, fd)) != 0) {
425 soclose(so);
426 return error;
427 }
428 fd_set_exclose(l, *fd, (flags & SOCK_CLOEXEC) != 0);
429 (*fp)->f_flag = FREAD|FWRITE|
430 ((flags & SOCK_NONBLOCK) ? FNONBLOCK : 0)|
431 ((flags & SOCK_NOSIGPIPE) ? FNOSIGPIPE : 0);
432 (*fp)->f_type = DTYPE_SOCKET;
433 (*fp)->f_ops = &socketops;
434 (*fp)->f_data = so;
435 return 0;
436 }
437
438 int
439 sys_socketpair(struct lwp *l, const struct sys_socketpair_args *uap,
440 register_t *retval)
441 {
442 /* {
443 syscallarg(int) domain;
444 syscallarg(int) type;
445 syscallarg(int) protocol;
446 syscallarg(int *) rsv;
447 } */
448 file_t *fp1, *fp2;
449 struct socket *so1, *so2;
450 int fd, error, sv[2];
451 proc_t *p = curproc;
452 int flags = SCARG(uap, type) & SOCK_FLAGS_MASK;
453 int type = SCARG(uap, type) & ~SOCK_FLAGS_MASK;
454 int domain = SCARG(uap, domain);
455 int proto = SCARG(uap, protocol);
456
457 error = makesocket(l, &fp1, &fd, flags, type, domain, proto, NULL);
458 if (error)
459 return error;
460 so1 = fp1->f_data;
461 sv[0] = fd;
462
463 error = makesocket(l, &fp2, &fd, flags, type, domain, proto, so1);
464 if (error)
465 goto out;
466 so2 = fp2->f_data;
467 sv[1] = fd;
468
469 solock(so1);
470 error = soconnect2(so1, so2);
471 if (error == 0 && type == SOCK_DGRAM) {
472 /*
473 * Datagram socket connection is asymmetric.
474 */
475 error = soconnect2(so2, so1);
476 }
477 sounlock(so1);
478
479 if (error == 0)
480 error = copyout(sv, SCARG(uap, rsv), sizeof(sv));
481 if (error == 0) {
482 fd_affix(p, fp2, sv[1]);
483 fd_affix(p, fp1, sv[0]);
484 return 0;
485 }
486 fd_abort(p, fp2, sv[1]);
487 (void)soclose(so2);
488 out:
489 fd_abort(p, fp1, sv[0]);
490 (void)soclose(so1);
491 return error;
492 }
493
494 int
495 sys_sendto(struct lwp *l, const struct sys_sendto_args *uap,
496 register_t *retval)
497 {
498 /* {
499 syscallarg(int) s;
500 syscallarg(const void *) buf;
501 syscallarg(size_t) len;
502 syscallarg(int) flags;
503 syscallarg(const struct sockaddr *) to;
504 syscallarg(unsigned int) tolen;
505 } */
506 struct msghdr msg;
507 struct iovec aiov;
508
509 msg.msg_name = __UNCONST(SCARG(uap, to)); /* XXXUNCONST kills const */
510 msg.msg_namelen = SCARG(uap, tolen);
511 msg.msg_iov = &aiov;
512 msg.msg_iovlen = 1;
513 msg.msg_control = NULL;
514 msg.msg_flags = 0;
515 aiov.iov_base = __UNCONST(SCARG(uap, buf)); /* XXXUNCONST kills const */
516 aiov.iov_len = SCARG(uap, len);
517 return do_sys_sendmsg(l, SCARG(uap, s), &msg, SCARG(uap, flags), retval);
518 }
519
520 int
521 sys_sendmsg(struct lwp *l, const struct sys_sendmsg_args *uap,
522 register_t *retval)
523 {
524 /* {
525 syscallarg(int) s;
526 syscallarg(const struct msghdr *) msg;
527 syscallarg(int) flags;
528 } */
529 struct msghdr msg;
530 int error;
531
532 error = copyin(SCARG(uap, msg), &msg, sizeof(msg));
533 if (error)
534 return (error);
535
536 msg.msg_flags = MSG_IOVUSRSPACE;
537 return do_sys_sendmsg(l, SCARG(uap, s), &msg, SCARG(uap, flags), retval);
538 }
539
540 static int
541 do_sys_sendmsg_so(struct lwp *l, int s, struct socket *so, file_t *fp,
542 struct msghdr *mp, int flags, register_t *retsize)
543 {
544
545 struct iovec aiov[UIO_SMALLIOV], *iov = aiov, *tiov, *ktriov = NULL;
546 struct mbuf *to, *control;
547 struct uio auio;
548 size_t len, iovsz;
549 int i, error;
550
551 ktrkuser("msghdr", mp, sizeof *mp);
552
553 /* If the caller passed us stuff in mbufs, we must free them. */
554 to = (mp->msg_flags & MSG_NAMEMBUF) ? mp->msg_name : NULL;
555 control = (mp->msg_flags & MSG_CONTROLMBUF) ? mp->msg_control : NULL;
556 iovsz = mp->msg_iovlen * sizeof(struct iovec);
557
558 if (mp->msg_flags & MSG_IOVUSRSPACE) {
559 if ((unsigned int)mp->msg_iovlen > UIO_SMALLIOV) {
560 if ((unsigned int)mp->msg_iovlen > IOV_MAX) {
561 error = EMSGSIZE;
562 goto bad;
563 }
564 iov = kmem_alloc(iovsz, KM_SLEEP);
565 }
566 if (mp->msg_iovlen != 0) {
567 error = copyin(mp->msg_iov, iov, iovsz);
568 if (error)
569 goto bad;
570 }
571 mp->msg_iov = iov;
572 }
573
574 auio.uio_iov = mp->msg_iov;
575 auio.uio_iovcnt = mp->msg_iovlen;
576 auio.uio_rw = UIO_WRITE;
577 auio.uio_offset = 0; /* XXX */
578 auio.uio_resid = 0;
579 KASSERT(l == curlwp);
580 auio.uio_vmspace = l->l_proc->p_vmspace;
581
582 for (i = 0, tiov = mp->msg_iov; i < mp->msg_iovlen; i++, tiov++) {
583 /*
584 * Writes return ssize_t because -1 is returned on error.
585 * Therefore, we must restrict the length to SSIZE_MAX to
586 * avoid garbage return values.
587 */
588 auio.uio_resid += tiov->iov_len;
589 if (tiov->iov_len > SSIZE_MAX || auio.uio_resid > SSIZE_MAX) {
590 error = EINVAL;
591 goto bad;
592 }
593 }
594
595 if (mp->msg_name && to == NULL) {
596 error = sockargs(&to, mp->msg_name, mp->msg_namelen,
597 MT_SONAME);
598 if (error)
599 goto bad;
600 }
601
602 if (mp->msg_control) {
603 if (mp->msg_controllen < CMSG_ALIGN(sizeof(struct cmsghdr))) {
604 error = EINVAL;
605 goto bad;
606 }
607 if (control == NULL) {
608 error = sockargs(&control, mp->msg_control,
609 mp->msg_controllen, MT_CONTROL);
610 if (error)
611 goto bad;
612 }
613 }
614
615 if (ktrpoint(KTR_GENIO) && iovsz > 0) {
616 ktriov = kmem_alloc(iovsz, KM_SLEEP);
617 memcpy(ktriov, auio.uio_iov, iovsz);
618 }
619
620 if (mp->msg_name)
621 MCLAIM(to, so->so_mowner);
622 if (mp->msg_control)
623 MCLAIM(control, so->so_mowner);
624
625 len = auio.uio_resid;
626 error = (*so->so_send)(so, to, &auio, NULL, control, flags, l);
627 /* Protocol is responsible for freeing 'control' */
628 control = NULL;
629
630 if (error) {
631 if (auio.uio_resid != len && (error == ERESTART ||
632 error == EINTR || error == EWOULDBLOCK))
633 error = 0;
634 if (error == EPIPE && (fp->f_flag & FNOSIGPIPE) == 0 &&
635 (flags & MSG_NOSIGNAL) == 0) {
636 mutex_enter(proc_lock);
637 psignal(l->l_proc, SIGPIPE);
638 mutex_exit(proc_lock);
639 }
640 }
641 if (error == 0)
642 *retsize = len - auio.uio_resid;
643
644 bad:
645 if (ktriov != NULL) {
646 ktrgeniov(s, UIO_WRITE, ktriov, *retsize, error);
647 kmem_free(ktriov, iovsz);
648 }
649
650 if (iov != aiov)
651 kmem_free(iov, iovsz);
652 if (to)
653 m_freem(to);
654 if (control)
655 m_freem(control);
656
657 return error;
658 }
659
660 int
661 do_sys_sendmsg(struct lwp *l, int s, struct msghdr *mp, int flags,
662 register_t *retsize)
663 {
664 int error;
665 struct socket *so;
666 file_t *fp;
667
668 if ((error = fd_getsock1(s, &so, &fp)) != 0) {
669 /* We have to free msg_name and msg_control ourselves */
670 if (mp->msg_flags & MSG_NAMEMBUF)
671 m_freem(mp->msg_name);
672 if (mp->msg_flags & MSG_CONTROLMBUF)
673 m_freem(mp->msg_control);
674 return error;
675 }
676 error = do_sys_sendmsg_so(l, s, so, fp, mp, flags, retsize);
677 /* msg_name and msg_control freed */
678 fd_putfile(s);
679 return error;
680 }
681
682 int
683 sys_recvfrom(struct lwp *l, const struct sys_recvfrom_args *uap,
684 register_t *retval)
685 {
686 /* {
687 syscallarg(int) s;
688 syscallarg(void *) buf;
689 syscallarg(size_t) len;
690 syscallarg(int) flags;
691 syscallarg(struct sockaddr *) from;
692 syscallarg(unsigned int *) fromlenaddr;
693 } */
694 struct msghdr msg;
695 struct iovec aiov;
696 int error;
697 struct mbuf *from;
698
699 msg.msg_name = NULL;
700 msg.msg_iov = &aiov;
701 msg.msg_iovlen = 1;
702 aiov.iov_base = SCARG(uap, buf);
703 aiov.iov_len = SCARG(uap, len);
704 msg.msg_control = NULL;
705 msg.msg_flags = SCARG(uap, flags) & MSG_USERFLAGS;
706
707 error = do_sys_recvmsg(l, SCARG(uap, s), &msg, &from, NULL, retval);
708 if (error != 0)
709 return error;
710
711 error = copyout_sockname(SCARG(uap, from), SCARG(uap, fromlenaddr),
712 MSG_LENUSRSPACE, from);
713 if (from != NULL)
714 m_free(from);
715 return error;
716 }
717
718 int
719 sys_recvmsg(struct lwp *l, const struct sys_recvmsg_args *uap,
720 register_t *retval)
721 {
722 /* {
723 syscallarg(int) s;
724 syscallarg(struct msghdr *) msg;
725 syscallarg(int) flags;
726 } */
727 struct msghdr msg;
728 int error;
729 struct mbuf *from, *control;
730
731 error = copyin(SCARG(uap, msg), &msg, sizeof(msg));
732 if (error)
733 return error;
734
735 msg.msg_flags = (SCARG(uap, flags) & MSG_USERFLAGS) | MSG_IOVUSRSPACE;
736
737 error = do_sys_recvmsg(l, SCARG(uap, s), &msg, &from,
738 msg.msg_control != NULL ? &control : NULL, retval);
739 if (error != 0)
740 return error;
741
742 if (msg.msg_control != NULL)
743 error = copyout_msg_control(l, &msg, control);
744
745 if (error == 0)
746 error = copyout_sockname(msg.msg_name, &msg.msg_namelen, 0,
747 from);
748 if (from != NULL)
749 m_free(from);
750 if (error == 0) {
751 ktrkuser("msghdr", &msg, sizeof msg);
752 error = copyout(&msg, SCARG(uap, msg), sizeof(msg));
753 }
754
755 return error;
756 }
757
758 int
759 sys_sendmmsg(struct lwp *l, const struct sys_sendmmsg_args *uap,
760 register_t *retval)
761 {
762 /* {
763 syscallarg(int) s;
764 syscallarg(struct mmsghdr *) mmsg;
765 syscallarg(unsigned int) vlen;
766 syscallarg(unsigned int) flags;
767 } */
768 struct mmsghdr mmsg;
769 struct socket *so;
770 file_t *fp;
771 struct msghdr *msg = &mmsg.msg_hdr;
772 int error, s;
773 unsigned int vlen, flags, dg;
774
775 s = SCARG(uap, s);
776 if ((error = fd_getsock1(s, &so, &fp)) != 0)
777 return error;
778
779 vlen = SCARG(uap, vlen);
780 if (vlen > 1024)
781 vlen = 1024;
782
783 flags = (SCARG(uap, flags) & MSG_USERFLAGS) | MSG_IOVUSRSPACE;
784
785 for (dg = 0; dg < vlen;) {
786 error = copyin(SCARG(uap, mmsg) + dg, &mmsg, sizeof(mmsg));
787 if (error)
788 break;
789
790 msg->msg_flags = flags;
791
792 error = do_sys_sendmsg_so(l, s, so, fp, msg, flags, retval);
793 if (error)
794 break;
795
796 ktrkuser("msghdr", msg, sizeof *msg);
797 mmsg.msg_len = *retval;
798 error = copyout(&mmsg, SCARG(uap, mmsg) + dg, sizeof(mmsg));
799 if (error)
800 break;
801 dg++;
802
803 }
804
805 *retval = dg;
806 if (error)
807 so->so_error = error;
808
809 fd_putfile(s);
810
811 /*
812 * If we succeeded at least once, return 0, hopefully so->so_error
813 * will catch it next time.
814 */
815 if (dg)
816 return 0;
817 return error;
818 }
819
820 /*
821 * Adjust for a truncated SCM_RIGHTS control message.
822 * This means closing any file descriptors that aren't present
823 * in the returned buffer.
824 * m is the mbuf holding the (already externalized) SCM_RIGHTS message.
825 */
826 static void
827 free_rights(struct mbuf *m)
828 {
829 struct cmsghdr *cm;
830 int *fdv;
831 unsigned int nfds, i;
832
833 KASSERT(sizeof(*cm) <= m->m_len);
834 cm = mtod(m, struct cmsghdr *);
835
836 KASSERT(CMSG_ALIGN(sizeof(*cm)) <= cm->cmsg_len);
837 KASSERT(cm->cmsg_len <= m->m_len);
838 nfds = (cm->cmsg_len - CMSG_ALIGN(sizeof(*cm))) / sizeof(int);
839 fdv = (int *)CMSG_DATA(cm);
840
841 for (i = 0; i < nfds; i++)
842 if (fd_getfile(fdv[i]) != NULL)
843 (void)fd_close(fdv[i]);
844 }
845
846 void
847 free_control_mbuf(struct lwp *l, struct mbuf *control, struct mbuf *uncopied)
848 {
849 struct mbuf *next;
850 struct cmsghdr *cmsg;
851 bool do_free_rights = false;
852
853 while (control != NULL) {
854 cmsg = mtod(control, struct cmsghdr *);
855 if (control == uncopied)
856 do_free_rights = true;
857 if (do_free_rights && cmsg->cmsg_level == SOL_SOCKET
858 && cmsg->cmsg_type == SCM_RIGHTS)
859 free_rights(control);
860 next = control->m_next;
861 m_free(control);
862 control = next;
863 }
864 }
865
866 /* Copy socket control/CMSG data to user buffer, frees the mbuf */
867 int
868 copyout_msg_control(struct lwp *l, struct msghdr *mp, struct mbuf *control)
869 {
870 int i, len, error = 0;
871 struct cmsghdr *cmsg;
872 struct mbuf *m;
873 char *q;
874
875 len = mp->msg_controllen;
876 if (len <= 0 || control == 0) {
877 mp->msg_controllen = 0;
878 free_control_mbuf(l, control, control);
879 return 0;
880 }
881
882 q = (char *)mp->msg_control;
883
884 for (m = control; m != NULL; ) {
885 cmsg = mtod(m, struct cmsghdr *);
886 i = m->m_len;
887 if (len < i) {
888 mp->msg_flags |= MSG_CTRUNC;
889 if (cmsg->cmsg_level == SOL_SOCKET
890 && cmsg->cmsg_type == SCM_RIGHTS)
891 /* Do not truncate me ... */
892 break;
893 i = len;
894 }
895 error = copyout(mtod(m, void *), q, i);
896 ktrkuser("msgcontrol", mtod(m, void *), i);
897 if (error != 0) {
898 /* We must free all the SCM_RIGHTS */
899 m = control;
900 break;
901 }
902 m = m->m_next;
903 if (m)
904 i = ALIGN(i);
905 q += i;
906 len -= i;
907 if (len <= 0)
908 break;
909 }
910
911 free_control_mbuf(l, control, m);
912
913 mp->msg_controllen = q - (char *)mp->msg_control;
914 return error;
915 }
916
917 static int
918 do_sys_recvmsg_so(struct lwp *l, int s, struct socket *so, struct msghdr *mp,
919 struct mbuf **from, struct mbuf **control, register_t *retsize)
920 {
921 struct iovec aiov[UIO_SMALLIOV], *iov = aiov, *tiov, *ktriov = NULL;
922 struct uio auio;
923 size_t len, iovsz;
924 int i, error;
925
926 ktrkuser("msghdr", mp, sizeof *mp);
927
928 *from = NULL;
929 if (control != NULL)
930 *control = NULL;
931
932 iovsz = mp->msg_iovlen * sizeof(struct iovec);
933
934 if (mp->msg_flags & MSG_IOVUSRSPACE) {
935 if ((unsigned int)mp->msg_iovlen > UIO_SMALLIOV) {
936 if ((unsigned int)mp->msg_iovlen > IOV_MAX) {
937 error = EMSGSIZE;
938 goto out;
939 }
940 iov = kmem_alloc(iovsz, KM_SLEEP);
941 }
942 if (mp->msg_iovlen != 0) {
943 error = copyin(mp->msg_iov, iov, iovsz);
944 if (error)
945 goto out;
946 }
947 auio.uio_iov = iov;
948 } else
949 auio.uio_iov = mp->msg_iov;
950 auio.uio_iovcnt = mp->msg_iovlen;
951 auio.uio_rw = UIO_READ;
952 auio.uio_offset = 0; /* XXX */
953 auio.uio_resid = 0;
954 KASSERT(l == curlwp);
955 auio.uio_vmspace = l->l_proc->p_vmspace;
956
957 tiov = auio.uio_iov;
958 for (i = 0; i < mp->msg_iovlen; i++, tiov++) {
959 /*
960 * Reads return ssize_t because -1 is returned on error.
961 * Therefore we must restrict the length to SSIZE_MAX to
962 * avoid garbage return values.
963 */
964 auio.uio_resid += tiov->iov_len;
965 if (tiov->iov_len > SSIZE_MAX || auio.uio_resid > SSIZE_MAX) {
966 error = EINVAL;
967 goto out;
968 }
969 }
970
971 if (ktrpoint(KTR_GENIO) && iovsz > 0) {
972 ktriov = kmem_alloc(iovsz, KM_SLEEP);
973 memcpy(ktriov, auio.uio_iov, iovsz);
974 }
975
976 len = auio.uio_resid;
977 mp->msg_flags &= MSG_USERFLAGS;
978 error = (*so->so_receive)(so, from, &auio, NULL, control,
979 &mp->msg_flags);
980 len -= auio.uio_resid;
981 *retsize = len;
982 if (error != 0 && len != 0
983 && (error == ERESTART || error == EINTR || error == EWOULDBLOCK))
984 /* Some data transferred */
985 error = 0;
986
987 if (ktriov != NULL) {
988 ktrgeniov(s, UIO_READ, ktriov, len, error);
989 kmem_free(ktriov, iovsz);
990 }
991
992 if (error != 0) {
993 m_freem(*from);
994 *from = NULL;
995 if (control != NULL) {
996 free_control_mbuf(l, *control, *control);
997 *control = NULL;
998 }
999 }
1000 out:
1001 if (iov != aiov)
1002 kmem_free(iov, iovsz);
1003 return error;
1004 }
1005
1006
1007 int
1008 do_sys_recvmsg(struct lwp *l, int s, struct msghdr *mp, struct mbuf **from,
1009 struct mbuf **control, register_t *retsize)
1010 {
1011 int error;
1012 struct socket *so;
1013
1014 if ((error = fd_getsock(s, &so)) != 0)
1015 return error;
1016 error = do_sys_recvmsg_so(l, s, so, mp, from, control, retsize);
1017 fd_putfile(s);
1018 return error;
1019 }
1020
1021 int
1022 sys_recvmmsg(struct lwp *l, const struct sys_recvmmsg_args *uap,
1023 register_t *retval)
1024 {
1025 /* {
1026 syscallarg(int) s;
1027 syscallarg(struct mmsghdr *) mmsg;
1028 syscallarg(unsigned int) vlen;
1029 syscallarg(unsigned int) flags;
1030 syscallarg(struct timespec *) timeout;
1031 } */
1032 struct mmsghdr mmsg;
1033 struct socket *so;
1034 struct msghdr *msg = &mmsg.msg_hdr;
1035 int error, s;
1036 struct mbuf *from, *control;
1037 struct timespec ts, now;
1038 unsigned int vlen, flags, dg;
1039
1040 if (SCARG(uap, timeout)) {
1041 if ((error = copyin(SCARG(uap, timeout), &ts, sizeof(ts))) != 0)
1042 return error;
1043 getnanotime(&now);
1044 timespecadd(&now, &ts, &ts);
1045 }
1046
1047 s = SCARG(uap, s);
1048 if ((error = fd_getsock(s, &so)) != 0)
1049 return error;
1050
1051 vlen = SCARG(uap, vlen);
1052 if (vlen > 1024)
1053 vlen = 1024;
1054
1055 from = NULL;
1056 flags = (SCARG(uap, flags) & MSG_USERFLAGS) | MSG_IOVUSRSPACE;
1057
1058 for (dg = 0; dg < vlen;) {
1059 error = copyin(SCARG(uap, mmsg) + dg, &mmsg, sizeof(mmsg));
1060 if (error)
1061 break;
1062
1063 msg->msg_flags = flags & ~MSG_WAITFORONE;
1064
1065 if (from != NULL) {
1066 m_free(from);
1067 from = NULL;
1068 }
1069
1070 error = do_sys_recvmsg_so(l, s, so, msg, &from,
1071 msg->msg_control != NULL ? &control : NULL, retval);
1072 if (error) {
1073 if (error == EAGAIN && dg > 0)
1074 error = 0;
1075 break;
1076 }
1077
1078 if (msg->msg_control != NULL)
1079 error = copyout_msg_control(l, msg, control);
1080 if (error)
1081 break;
1082
1083 error = copyout_sockname(msg->msg_name, &msg->msg_namelen, 0,
1084 from);
1085 if (error)
1086 break;
1087
1088 ktrkuser("msghdr", msg, sizeof *msg);
1089 mmsg.msg_len = *retval;
1090
1091 error = copyout(&mmsg, SCARG(uap, mmsg) + dg, sizeof(mmsg));
1092 if (error)
1093 break;
1094
1095 dg++;
1096 if (msg->msg_flags & MSG_OOB)
1097 break;
1098
1099 if (SCARG(uap, timeout)) {
1100 getnanotime(&now);
1101 timespecsub(&now, &ts, &now);
1102 if (now.tv_sec > 0)
1103 break;
1104 }
1105
1106 if (flags & MSG_WAITFORONE)
1107 flags |= MSG_DONTWAIT;
1108
1109 }
1110
1111 if (from != NULL)
1112 m_free(from);
1113
1114 *retval = dg;
1115 if (error)
1116 so->so_error = error;
1117
1118 fd_putfile(s);
1119
1120 /*
1121 * If we succeeded at least once, return 0, hopefully so->so_error
1122 * will catch it next time.
1123 */
1124 if (dg)
1125 return 0;
1126
1127 return error;
1128 }
1129
1130 int
1131 sys_shutdown(struct lwp *l, const struct sys_shutdown_args *uap,
1132 register_t *retval)
1133 {
1134 /* {
1135 syscallarg(int) s;
1136 syscallarg(int) how;
1137 } */
1138 struct socket *so;
1139 int error;
1140
1141 if ((error = fd_getsock(SCARG(uap, s), &so)) != 0)
1142 return error;
1143 solock(so);
1144 error = soshutdown(so, SCARG(uap, how));
1145 sounlock(so);
1146 fd_putfile(SCARG(uap, s));
1147 return error;
1148 }
1149
1150 int
1151 sys_setsockopt(struct lwp *l, const struct sys_setsockopt_args *uap,
1152 register_t *retval)
1153 {
1154 /* {
1155 syscallarg(int) s;
1156 syscallarg(int) level;
1157 syscallarg(int) name;
1158 syscallarg(const void *) val;
1159 syscallarg(unsigned int) valsize;
1160 } */
1161 struct sockopt sopt;
1162 struct socket *so;
1163 file_t *fp;
1164 int error;
1165 unsigned int len;
1166
1167 len = SCARG(uap, valsize);
1168 if (len > 0 && SCARG(uap, val) == NULL)
1169 return EINVAL;
1170
1171 if (len > MCLBYTES)
1172 return EINVAL;
1173
1174 if ((error = fd_getsock1(SCARG(uap, s), &so, &fp)) != 0)
1175 return (error);
1176
1177 sockopt_init(&sopt, SCARG(uap, level), SCARG(uap, name), len);
1178
1179 if (len > 0) {
1180 error = copyin(SCARG(uap, val), sopt.sopt_data, len);
1181 if (error)
1182 goto out;
1183 }
1184
1185 error = sosetopt(so, &sopt);
1186 if (so->so_options & SO_NOSIGPIPE)
1187 atomic_or_uint(&fp->f_flag, FNOSIGPIPE);
1188 else
1189 atomic_and_uint(&fp->f_flag, ~FNOSIGPIPE);
1190
1191 out:
1192 sockopt_destroy(&sopt);
1193 fd_putfile(SCARG(uap, s));
1194 return error;
1195 }
1196
1197 int
1198 sys_getsockopt(struct lwp *l, const struct sys_getsockopt_args *uap,
1199 register_t *retval)
1200 {
1201 /* {
1202 syscallarg(int) s;
1203 syscallarg(int) level;
1204 syscallarg(int) name;
1205 syscallarg(void *) val;
1206 syscallarg(unsigned int *) avalsize;
1207 } */
1208 struct sockopt sopt;
1209 struct socket *so;
1210 file_t *fp;
1211 unsigned int valsize, len;
1212 int error;
1213
1214 if (SCARG(uap, val) != NULL) {
1215 error = copyin(SCARG(uap, avalsize), &valsize, sizeof(valsize));
1216 if (error)
1217 return error;
1218 } else
1219 valsize = 0;
1220
1221 if ((error = fd_getsock1(SCARG(uap, s), &so, &fp)) != 0)
1222 return (error);
1223
1224 sockopt_init(&sopt, SCARG(uap, level), SCARG(uap, name), 0);
1225
1226 if (fp->f_flag & FNOSIGPIPE)
1227 so->so_options |= SO_NOSIGPIPE;
1228 else
1229 so->so_options &= ~SO_NOSIGPIPE;
1230 error = sogetopt(so, &sopt);
1231 if (error)
1232 goto out;
1233
1234 if (valsize > 0) {
1235 len = min(valsize, sopt.sopt_size);
1236 error = copyout(sopt.sopt_data, SCARG(uap, val), len);
1237 if (error)
1238 goto out;
1239
1240 error = copyout(&len, SCARG(uap, avalsize), sizeof(len));
1241 if (error)
1242 goto out;
1243 }
1244
1245 out:
1246 sockopt_destroy(&sopt);
1247 fd_putfile(SCARG(uap, s));
1248 return error;
1249 }
1250
1251 #ifdef PIPE_SOCKETPAIR
1252
1253 int
1254 pipe1(struct lwp *l, register_t *retval, int flags)
1255 {
1256 file_t *rf, *wf;
1257 struct socket *rso, *wso;
1258 int fd, error;
1259 proc_t *p;
1260
1261 if (flags & ~(O_CLOEXEC|O_NONBLOCK|O_NOSIGPIPE))
1262 return EINVAL;
1263 p = curproc;
1264 if ((error = socreate(AF_LOCAL, &rso, SOCK_STREAM, 0, l, NULL)) != 0)
1265 return error;
1266 if ((error = socreate(AF_LOCAL, &wso, SOCK_STREAM, 0, l, rso)) != 0)
1267 goto free1;
1268 /* remember this socket pair implements a pipe */
1269 wso->so_state |= SS_ISAPIPE;
1270 rso->so_state |= SS_ISAPIPE;
1271 if ((error = fd_allocfile(&rf, &fd)) != 0)
1272 goto free2;
1273 retval[0] = fd;
1274 rf->f_flag = FREAD | flags;
1275 rf->f_type = DTYPE_SOCKET;
1276 rf->f_ops = &socketops;
1277 rf->f_data = rso;
1278 if ((error = fd_allocfile(&wf, &fd)) != 0)
1279 goto free3;
1280 wf->f_flag = FWRITE | flags;
1281 wf->f_type = DTYPE_SOCKET;
1282 wf->f_ops = &socketops;
1283 wf->f_data = wso;
1284 retval[1] = fd;
1285 solock(wso);
1286 error = unp_connect2(wso, rso);
1287 sounlock(wso);
1288 if (error != 0)
1289 goto free4;
1290 fd_affix(p, wf, (int)retval[1]);
1291 fd_affix(p, rf, (int)retval[0]);
1292 return (0);
1293 free4:
1294 fd_abort(p, wf, (int)retval[1]);
1295 free3:
1296 fd_abort(p, rf, (int)retval[0]);
1297 free2:
1298 (void)soclose(wso);
1299 free1:
1300 (void)soclose(rso);
1301 return error;
1302 }
1303 #endif /* PIPE_SOCKETPAIR */
1304
1305 /*
1306 * Get peer socket name.
1307 */
1308 int
1309 do_sys_getpeername(int fd, struct mbuf **nam)
1310 {
1311 struct socket *so;
1312 struct mbuf *m;
1313 int error;
1314
1315 if ((error = fd_getsock(fd, &so)) != 0)
1316 return error;
1317
1318 m = m_getclr(M_WAIT, MT_SONAME);
1319 MCLAIM(m, so->so_mowner);
1320
1321 solock(so);
1322 if ((so->so_state & SS_ISCONNECTED) == 0)
1323 error = ENOTCONN;
1324 else {
1325 *nam = m;
1326 error = (*so->so_proto->pr_usrreqs->pr_peeraddr)(so, m);
1327 }
1328 sounlock(so);
1329 if (error != 0)
1330 m_free(m);
1331 fd_putfile(fd);
1332 return error;
1333 }
1334
1335 /*
1336 * Get local socket name.
1337 */
1338 int
1339 do_sys_getsockname(int fd, struct mbuf **nam)
1340 {
1341 struct socket *so;
1342 struct mbuf *m;
1343 int error;
1344
1345 if ((error = fd_getsock(fd, &so)) != 0)
1346 return error;
1347
1348 m = m_getclr(M_WAIT, MT_SONAME);
1349 MCLAIM(m, so->so_mowner);
1350
1351 *nam = m;
1352 solock(so);
1353 error = (*so->so_proto->pr_usrreqs->pr_sockaddr)(so, m);
1354 sounlock(so);
1355 if (error != 0)
1356 m_free(m);
1357 fd_putfile(fd);
1358 return error;
1359 }
1360
1361 int
1362 copyout_sockname(struct sockaddr *asa, unsigned int *alen, int flags,
1363 struct mbuf *addr)
1364 {
1365 int len;
1366 int error;
1367
1368 if (asa == NULL)
1369 /* Assume application not interested */
1370 return 0;
1371
1372 if (flags & MSG_LENUSRSPACE) {
1373 error = copyin(alen, &len, sizeof(len));
1374 if (error)
1375 return error;
1376 } else
1377 len = *alen;
1378 if (len < 0)
1379 return EINVAL;
1380
1381 if (addr == NULL) {
1382 len = 0;
1383 error = 0;
1384 } else {
1385 if (len > addr->m_len)
1386 len = addr->m_len;
1387 /* Maybe this ought to copy a chain ? */
1388 ktrkuser(mbuftypes[MT_SONAME], mtod(addr, void *), len);
1389 error = copyout(mtod(addr, void *), asa, len);
1390 }
1391
1392 if (error == 0) {
1393 if (flags & MSG_LENUSRSPACE)
1394 error = copyout(&len, alen, sizeof(len));
1395 else
1396 *alen = len;
1397 }
1398
1399 return error;
1400 }
1401
1402 /*
1403 * Get socket name.
1404 */
1405 int
1406 sys_getsockname(struct lwp *l, const struct sys_getsockname_args *uap,
1407 register_t *retval)
1408 {
1409 /* {
1410 syscallarg(int) fdes;
1411 syscallarg(struct sockaddr *) asa;
1412 syscallarg(unsigned int *) alen;
1413 } */
1414 struct mbuf *m;
1415 int error;
1416
1417 error = do_sys_getsockname(SCARG(uap, fdes), &m);
1418 if (error != 0)
1419 return error;
1420
1421 error = copyout_sockname(SCARG(uap, asa), SCARG(uap, alen),
1422 MSG_LENUSRSPACE, m);
1423 if (m != NULL)
1424 m_free(m);
1425 return error;
1426 }
1427
1428 /*
1429 * Get name of peer for connected socket.
1430 */
1431 int
1432 sys_getpeername(struct lwp *l, const struct sys_getpeername_args *uap,
1433 register_t *retval)
1434 {
1435 /* {
1436 syscallarg(int) fdes;
1437 syscallarg(struct sockaddr *) asa;
1438 syscallarg(unsigned int *) alen;
1439 } */
1440 struct mbuf *m;
1441 int error;
1442
1443 error = do_sys_getpeername(SCARG(uap, fdes), &m);
1444 if (error != 0)
1445 return error;
1446
1447 error = copyout_sockname(SCARG(uap, asa), SCARG(uap, alen),
1448 MSG_LENUSRSPACE, m);
1449 if (m != NULL)
1450 m_free(m);
1451 return error;
1452 }
1453
1454 /*
1455 * XXX In a perfect world, we wouldn't pass around socket control
1456 * XXX arguments in mbufs, and this could go away.
1457 */
1458 int
1459 sockargs(struct mbuf **mp, const void *bf, size_t buflen, int type)
1460 {
1461 struct sockaddr *sa;
1462 struct mbuf *m;
1463 int error;
1464
1465 /*
1466 * We can't allow socket names > UCHAR_MAX in length, since that
1467 * will overflow sa_len. Control data more than a page size in
1468 * length is just too much.
1469 */
1470 if (buflen > (type == MT_SONAME ? UCHAR_MAX : PAGE_SIZE))
1471 return EINVAL;
1472
1473 /* Allocate an mbuf to hold the arguments. */
1474 m = m_get(M_WAIT, type);
1475 /* can't claim. don't who to assign it to. */
1476 if (buflen > MLEN) {
1477 /*
1478 * Won't fit into a regular mbuf, so we allocate just
1479 * enough external storage to hold the argument.
1480 */
1481 MEXTMALLOC(m, buflen, M_WAITOK);
1482 }
1483 m->m_len = buflen;
1484 error = copyin(bf, mtod(m, void *), buflen);
1485 if (error) {
1486 (void)m_free(m);
1487 return error;
1488 }
1489 ktrkuser(mbuftypes[type], mtod(m, void *), buflen);
1490 *mp = m;
1491 if (type == MT_SONAME) {
1492 sa = mtod(m, struct sockaddr *);
1493 #if BYTE_ORDER != BIG_ENDIAN
1494 /*
1495 * 4.3BSD compat thing - need to stay, since bind(2),
1496 * connect(2), sendto(2) were not versioned for COMPAT_43.
1497 */
1498 if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
1499 sa->sa_family = sa->sa_len;
1500 #endif
1501 sa->sa_len = buflen;
1502 }
1503 return 0;
1504 }
1505