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