59 #include <sys/cdefs.h>
64 #include <sys/param.h>
65 #include <sys/domain.h>
66 #include <sys/fcntl.h>
67 #include <sys/malloc.h>
68 #include <sys/eventhandler.h>
70 #include <sys/filedesc.h>
71 #include <sys/kernel.h>
74 #include <sys/mount.h>
75 #include <sys/mutex.h>
76 #include <sys/namei.h>
78 #include <sys/protosw.h>
79 #include <sys/queue.h>
80 #include <sys/resourcevar.h>
81 #include <sys/rwlock.h>
82 #include <sys/socket.h>
83 #include <sys/socketvar.h>
84 #include <sys/signalvar.h>
87 #include <sys/sysctl.h>
88 #include <sys/systm.h>
89 #include <sys/taskqueue.h>
91 #include <sys/unpcb.h>
92 #include <sys/vnode.h>
100 #include <security/mac/mac_framework.h>
124 static
int unp_defers_count;
126 static const struct sockaddr sun_noname = {
sizeof(sun_noname), AF_LOCAL };
162 static SYSCTL_NODE(_net, PF_LOCAL, local, CTLFLAG_RW, 0,
"Local domain");
163 static SYSCTL_NODE(_net_local, SOCK_STREAM, stream, CTLFLAG_RW, 0,
165 static SYSCTL_NODE(_net_local, SOCK_DGRAM, dgram, CTLFLAG_RW, 0,
"SOCK_DGRAM");
166 static SYSCTL_NODE(_net_local, SOCK_SEQPACKET, seqpacket, CTLFLAG_RW, 0,
169 SYSCTL_ULONG(_net_local_stream, OID_AUTO, sendspace, CTLFLAG_RW,
170 &unpst_sendspace, 0,
"Default stream send space.");
171 SYSCTL_ULONG(_net_local_stream, OID_AUTO, recvspace, CTLFLAG_RW,
172 &unpst_recvspace, 0,
"Default stream receive space.");
173 SYSCTL_ULONG(_net_local_dgram, OID_AUTO, maxdgram, CTLFLAG_RW,
174 &unpdg_sendspace, 0,
"Default datagram send space.");
175 SYSCTL_ULONG(_net_local_dgram, OID_AUTO, recvspace, CTLFLAG_RW,
176 &unpdg_recvspace, 0,
"Default datagram receive space.");
177 SYSCTL_ULONG(_net_local_seqpacket, OID_AUTO, maxseqpacket, CTLFLAG_RW,
178 &unpsp_sendspace, 0,
"Default seqpacket send space.");
179 SYSCTL_ULONG(_net_local_seqpacket, OID_AUTO, recvspace, CTLFLAG_RW,
180 &unpsp_recvspace, 0,
"Default seqpacket receive space.");
182 "File descriptors in flight.");
183 SYSCTL_INT(_net_local, OID_AUTO, deferred, CTLFLAG_RD,
184 &unp_defers_count, 0,
185 "File descriptors deferred to taskqueue for close.");
237 #define UNP_LINK_LOCK_INIT() rw_init(&unp_link_rwlock, \
240 #define UNP_LINK_LOCK_ASSERT() rw_assert(&unp_link_rwlock, \
242 #define UNP_LINK_UNLOCK_ASSERT() rw_assert(&unp_link_rwlock, \
245 #define UNP_LINK_RLOCK() rw_rlock(&unp_link_rwlock)
246 #define UNP_LINK_RUNLOCK() rw_runlock(&unp_link_rwlock)
247 #define UNP_LINK_WLOCK() rw_wlock(&unp_link_rwlock)
248 #define UNP_LINK_WUNLOCK() rw_wunlock(&unp_link_rwlock)
249 #define UNP_LINK_WLOCK_ASSERT() rw_assert(&unp_link_rwlock, \
252 #define UNP_LIST_LOCK_INIT() mtx_init(&unp_list_lock, \
253 "unp_list_lock", NULL, MTX_DEF)
254 #define UNP_LIST_LOCK() mtx_lock(&unp_list_lock)
255 #define UNP_LIST_UNLOCK() mtx_unlock(&unp_list_lock)
257 #define UNP_DEFERRED_LOCK_INIT() mtx_init(&unp_defers_lock, \
258 "unp_defer", NULL, MTX_DEF)
259 #define UNP_DEFERRED_LOCK() mtx_lock(&unp_defers_lock)
260 #define UNP_DEFERRED_UNLOCK() mtx_unlock(&unp_defers_lock)
262 #define UNP_PCB_LOCK_INIT(unp) mtx_init(&(unp)->unp_mtx, \
263 "unp_mtx", "unp_mtx", \
264 MTX_DUPOK|MTX_DEF|MTX_RECURSE)
265 #define UNP_PCB_LOCK_DESTROY(unp) mtx_destroy(&(unp)->unp_mtx)
266 #define UNP_PCB_LOCK(unp) mtx_lock(&(unp)->unp_mtx)
267 #define UNP_PCB_UNLOCK(unp) mtx_unlock(&(unp)->unp_mtx)
268 #define UNP_PCB_LOCK_ASSERT(unp) mtx_assert(&(unp)->unp_mtx, MA_OWNED)
272 static int unp_connect(
struct socket *,
struct sockaddr *,
274 static int unp_connect2(
struct socket *so,
struct socket *so2,
int);
275 static void unp_disconnect(
struct unpcb *unp,
struct unpcb *unp2);
278 static void unp_drop(
struct unpcb *,
int);
279 static void unp_gc(__unused
void *,
int);
280 static void unp_scan(
struct mbuf *,
void (*)(
struct file *));
299 .pr_type = SOCK_STREAM,
301 .pr_flags = PR_CONNREQUIRED|PR_WANTRCVD|PR_RIGHTS,
306 .pr_type = SOCK_DGRAM,
308 .pr_flags = PR_ATOMIC|PR_ADDR|PR_RIGHTS,
313 .pr_type = SOCK_SEQPACKET,
321 .pr_flags = PR_ADDR|PR_ATOMIC|PR_CONNREQUIRED|PR_WANTRCVD|
329 .dom_family = AF_LOCAL,
335 .dom_protoswNPROTOSW = &localsw[
sizeof(
localsw)/
sizeof(localsw[0])]
342 struct unpcb *unp, *unp2;
345 KASSERT(unp != NULL, (
"uipc_abort: unp == NULL"));
349 unp2 = unp->unp_conn;
362 struct unpcb *unp, *unp2;
363 const struct sockaddr *sa;
370 KASSERT(unp != NULL, (
"uipc_accept: unp == NULL"));
372 *nam =
malloc(
sizeof(
struct sockaddr_un), M_SONAME, M_WAITOK);
374 unp2 = unp->unp_conn;
375 if (unp2 != NULL && unp2->unp_addr != NULL) {
377 sa = (
struct sockaddr *) unp2->unp_addr;
378 bcopy(sa, *nam, sa->sa_len);
382 bcopy(sa, *nam, sa->sa_len);
391 u_long sendspace, recvspace;
395 KASSERT(so->so_pcb == NULL, (
"uipc_attach: so_pcb != NULL"));
396 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
397 switch (so->so_type) {
414 panic(
"uipc_attach");
416 error =
soreserve(so, sendspace, recvspace);
420 unp = uma_zalloc(
unp_zone, M_NOWAIT | M_ZERO);
423 LIST_INIT(&unp->unp_refs);
425 unp->unp_socket = so;
427 unp->unp_refcount = 1;
432 switch (so->so_type) {
434 LIST_INSERT_HEAD(&
unp_shead, unp, unp_link);
438 LIST_INSERT_HEAD(&
unp_dhead, unp, unp_link);
446 panic(
"uipc_attach");
454 uipc_bind(
struct socket *so,
struct sockaddr *nam,
struct thread *td)
456 struct sockaddr_un *soun = (
struct sockaddr_un *)nam;
458 int error, namelen, vfslocked;
466 KASSERT(unp != NULL, (
"uipc_bind: unp == NULL"));
468 if (soun->sun_len >
sizeof(
struct sockaddr_un))
470 namelen = soun->sun_len - offsetof(
struct sockaddr_un, sun_path);
484 if (unp->unp_vnode != NULL) {
488 if (unp->unp_flags & UNP_BINDING) {
492 unp->unp_flags |= UNP_BINDING;
495 buf =
malloc(namelen + 1, M_TEMP, M_WAITOK);
496 bcopy(soun->sun_path, buf, namelen);
501 NDINIT(&nd, CREATE, MPSAFE | NOFOLLOW | LOCKPARENT | SAVENAME,
502 UIO_SYSSPACE, buf, td);
508 vfslocked = NDHASGIANT(&nd);
509 if (vp != NULL ||
vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
510 NDFREE(&nd, NDF_ONLY_PNBUF);
523 VFS_UNLOCK_GIANT(vfslocked);
527 vattr.va_type = VSOCK;
528 vattr.va_mode = (ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask);
530 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
534 error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
535 NDFREE(&nd, NDF_ONLY_PNBUF);
542 ASSERT_VOP_ELOCKED(vp,
"uipc_bind");
547 VOP_UNP_BIND(vp, unp->unp_socket);
549 unp->unp_addr = soun;
550 unp->unp_flags &= ~UNP_BINDING;
555 VFS_UNLOCK_GIANT(vfslocked);
560 VFS_UNLOCK_GIANT(vfslocked);
562 unp->unp_flags &= ~UNP_BINDING;
569 uipc_connect(
struct socket *so,
struct sockaddr *nam,
struct thread *td)
573 KASSERT(td == curthread, (
"uipc_connect: td != curthread"));
583 struct unpcb *unp, *unp2;
586 KASSERT(unp != NULL, (
"uipc_close: unp == NULL"));
590 unp2 = unp->unp_conn;
603 struct unpcb *unp, *unp2;
608 KASSERT(unp != NULL, (
"uipc_connect2: unp == NULL"));
611 KASSERT(unp2 != NULL, (
"uipc_connect2: unp2 == NULL"));
623 struct unpcb *unp, *unp2;
624 struct sockaddr_un *saved_unp_addr;
626 int freeunp, local_unp_rights;
629 KASSERT(unp != NULL, (
"uipc_detach: unp == NULL"));
634 LIST_REMOVE(unp, unp_link);
642 if ((vp = unp->unp_vnode) != NULL) {
644 unp->unp_vnode = NULL;
646 unp2 = unp->unp_conn;
657 while (!LIST_EMPTY(&unp->unp_refs)) {
658 struct unpcb *ref = LIST_FIRST(&unp->unp_refs);
666 unp->unp_socket->so_pcb = NULL;
667 saved_unp_addr = unp->unp_addr;
668 unp->unp_addr = NULL;
670 freeunp = (unp->unp_refcount == 0);
671 if (saved_unp_addr != NULL)
672 free(saved_unp_addr, M_SONAME);
681 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
683 VFS_UNLOCK_GIANT(vfslocked);
685 if (local_unp_rights)
692 struct unpcb *unp, *unp2;
695 KASSERT(unp != NULL, (
"uipc_disconnect: unp == NULL"));
699 unp2 = unp->unp_conn;
717 KASSERT(unp != NULL, (
"uipc_listen: unp == NULL"));
720 if (unp->unp_vnode == NULL) {
728 cru2x(td->td_ucred, &unp->unp_peercred);
729 unp->unp_flags |= UNP_HAVEPCCACHED;
740 struct unpcb *unp, *unp2;
741 const struct sockaddr *sa;
744 KASSERT(unp != NULL, (
"uipc_peeraddr: unp == NULL"));
746 *nam =
malloc(
sizeof(
struct sockaddr_un), M_SONAME, M_WAITOK);
753 unp2 = unp->unp_conn;
756 if (unp2->unp_addr != NULL)
757 sa = (
struct sockaddr *) unp2->unp_addr;
760 bcopy(sa, *nam, sa->sa_len);
764 bcopy(sa, *nam, sa->sa_len);
773 struct unpcb *unp, *unp2;
779 KASSERT(unp != NULL, (
"uipc_rcvd: unp == NULL"));
781 if (so->so_type != SOCK_STREAM && so->so_type != SOCK_SEQPACKET)
782 panic(
"uipc_rcvd socktype %d", so->so_type);
794 SOCKBUF_LOCK(&so->so_rcv);
795 mbcnt = so->so_rcv.sb_mbcnt;
796 sbcc = so->so_rcv.sb_cc;
797 SOCKBUF_UNLOCK(&so->so_rcv);
799 unp2 = unp->unp_conn;
804 so2 = unp2->unp_socket;
805 SOCKBUF_LOCK(&so2->so_snd);
806 so2->so_snd.sb_mbmax += unp->unp_mbcnt - mbcnt;
807 newhiwat = so2->so_snd.sb_hiwat + unp->unp_cc - sbcc;
808 (void)
chgsbsize(so2->so_cred->cr_uidinfo, &so2->so_snd.sb_hiwat,
809 newhiwat, RLIM_INFINITY);
810 sowwakeup_locked(so2);
811 unp->unp_mbcnt = mbcnt;
818 uipc_send(
struct socket *so,
int flags,
struct mbuf *m,
struct sockaddr *nam,
819 struct mbuf *control,
struct thread *td)
821 struct unpcb *unp, *unp2;
823 u_int mbcnt_delta, sbcc;
828 KASSERT(unp != NULL, (
"uipc_send: unp == NULL"));
830 if (flags & PRUS_OOB) {
836 if ((nam != NULL) || (flags & PRUS_EOF))
840 switch (so->so_type) {
843 const struct sockaddr *from;
845 unp2 = unp->unp_conn;
855 unp2 = unp->unp_conn;
870 if (unp2->unp_flags & UNP_WANTCRED)
873 if (unp->unp_addr != NULL)
874 from = (
struct sockaddr *)unp->unp_addr;
877 so2 = unp2->unp_socket;
878 SOCKBUF_LOCK(&so2->so_rcv);
881 sorwakeup_locked(so2);
885 SOCKBUF_UNLOCK(&so2->so_rcv);
900 if ((so->so_state & SS_ISCONNECTED) == 0) {
913 if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
931 unp2 = unp->unp_conn;
936 so2 = unp2->unp_socket;
938 SOCKBUF_LOCK(&so2->so_rcv);
939 if (unp2->unp_flags & UNP_WANTCRED) {
944 unp2->unp_flags &= ~UNP_WANTCRED;
951 switch (so->so_type) {
953 if (control != NULL) {
961 case SOCK_SEQPACKET: {
962 const struct sockaddr *from;
984 mbcnt_delta = so2->so_rcv.sb_mbcnt - unp2->unp_mbcnt;
985 unp2->unp_mbcnt = so2->so_rcv.sb_mbcnt;
986 sbcc = so2->so_rcv.sb_cc;
987 sorwakeup_locked(so2);
989 SOCKBUF_LOCK(&so->so_snd);
990 if ((
int)so->so_snd.sb_hiwat >= (
int)(sbcc - unp2->unp_cc))
991 newhiwat = so->so_snd.sb_hiwat - (sbcc - unp2->unp_cc);
994 (void)
chgsbsize(so->so_cred->cr_uidinfo, &so->so_snd.sb_hiwat,
995 newhiwat, RLIM_INFINITY);
996 so->so_snd.sb_mbmax -= mbcnt_delta;
997 SOCKBUF_UNLOCK(&so->so_snd);
1004 panic(
"uipc_send unknown socktype");
1010 if (flags & PRUS_EOF) {
1017 if ((nam != NULL) || (flags & PRUS_EOF))
1022 if (control != NULL && error != 0)
1026 if (control != NULL)
1036 struct unpcb *unp, *unp2;
1039 unp = sotounpcb(so);
1040 KASSERT(unp != NULL, (
"uipc_sense: unp == NULL"));
1042 sb->st_blksize = so->so_snd.sb_hiwat;
1045 unp2 = unp->unp_conn;
1046 if ((so->so_type == SOCK_STREAM || so->so_type == SOCK_SEQPACKET) &&
1048 so2 = unp2->unp_socket;
1049 sb->st_blksize += so2->so_rcv.sb_cc;
1052 if (unp->unp_ino == 0)
1054 sb->st_ino = unp->unp_ino;
1065 unp = sotounpcb(so);
1066 KASSERT(unp != NULL, (
"uipc_shutdown: unp == NULL"));
1081 const struct sockaddr *sa;
1083 unp = sotounpcb(so);
1084 KASSERT(unp != NULL, (
"uipc_sockaddr: unp == NULL"));
1086 *nam =
malloc(
sizeof(
struct sockaddr_un), M_SONAME, M_WAITOK);
1088 if (unp->unp_addr != NULL)
1089 sa = (
struct sockaddr *) unp->unp_addr;
1092 bcopy(sa, *nam, sa->sa_len);
1164 if (sopt->sopt_level != 0)
1167 unp = sotounpcb(so);
1168 KASSERT(unp != NULL, (
"uipc_ctloutput: unp == NULL"));
1170 switch (sopt->sopt_dir) {
1172 switch (sopt->sopt_name) {
1173 case LOCAL_PEERCRED:
1175 if (unp->unp_flags & UNP_HAVEPC)
1176 xu = unp->unp_peercred;
1178 if (so->so_type == SOCK_STREAM)
1190 optval = unp->unp_flags & UNP_WANTCRED ? 1 : 0;
1194 case LOCAL_CONNWAIT:
1196 optval = unp->unp_flags & UNP_CONNWAIT ? 1 : 0;
1207 switch (sopt->sopt_name) {
1209 case LOCAL_CONNWAIT:
1210 error =
sooptcopyin(sopt, &optval,
sizeof(optval),
1215 #define OPTSET(bit) do { \
1216 UNP_PCB_LOCK(unp); \
1218 unp->unp_flags |= bit; \
1220 unp->unp_flags &= ~bit; \
1221 UNP_PCB_UNLOCK(unp); \
1224 switch (sopt->sopt_name) {
1229 case LOCAL_CONNWAIT:
1239 error = ENOPROTOOPT;
1252 unp_connect(
struct socket *so,
struct sockaddr *nam,
struct thread *td)
1254 struct sockaddr_un *soun = (
struct sockaddr_un *)nam;
1256 struct socket *so2, *so3;
1257 struct unpcb *unp, *unp2, *unp3;
1258 int error, len, vfslocked;
1259 struct nameidata nd;
1260 char buf[SOCK_MAXADDRLEN];
1261 struct sockaddr *sa;
1265 unp = sotounpcb(so);
1266 KASSERT(unp != NULL, (
"unp_connect: unp == NULL"));
1268 if (nam->sa_len >
sizeof(
struct sockaddr_un))
1270 len = nam->sa_len - offsetof(
struct sockaddr_un, sun_path);
1273 bcopy(soun->sun_path, buf, len);
1277 if (unp->unp_flags & UNP_CONNECTING) {
1282 unp->unp_flags |= UNP_CONNECTING;
1285 sa =
malloc(
sizeof(
struct sockaddr_un), M_SONAME, M_WAITOK);
1286 NDINIT(&nd, LOOKUP, MPSAFE | FOLLOW | LOCKSHARED | LOCKLEAF,
1287 UIO_SYSSPACE, buf, td);
1293 ASSERT_VOP_LOCKED(vp,
"unp_connect");
1294 vfslocked = NDHASGIANT(&nd);
1295 NDFREE(&nd, NDF_ONLY_PNBUF);
1299 if (vp->v_type != VSOCK) {
1304 error = mac_vnode_check_open(td->td_ucred, vp, VWRITE | VREAD);
1308 error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td);
1311 VFS_UNLOCK_GIANT(vfslocked);
1313 unp = sotounpcb(so);
1314 KASSERT(unp != NULL, (
"unp_connect: unp == NULL"));
1321 VOP_UNP_CONNECT(vp, &so2);
1323 error = ECONNREFUSED;
1326 if (so->so_type != so2->so_type) {
1330 if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
1331 if (so2->so_options & SO_ACCEPTCONN) {
1332 CURVNET_SET(so2->so_vnet);
1338 error = ECONNREFUSED;
1341 unp = sotounpcb(so);
1342 unp2 = sotounpcb(so2);
1343 unp3 = sotounpcb(so3);
1347 if (unp2->unp_addr != NULL) {
1348 bcopy(unp2->unp_addr, sa, unp2->unp_addr->sun_len);
1349 unp3->unp_addr = (
struct sockaddr_un *) sa;
1357 cru2x(td->td_ucred, &unp3->unp_peercred);
1358 unp3->unp_flags |= UNP_HAVEPC;
1366 KASSERT(unp2->unp_flags & UNP_HAVEPCCACHED,
1367 (
"unp_connect: listener without cached peercred"));
1368 memcpy(&unp->unp_peercred, &unp2->unp_peercred,
1369 sizeof(unp->unp_peercred));
1370 unp->unp_flags |= UNP_HAVEPC;
1371 if (unp2->unp_flags & UNP_WANTCRED)
1372 unp3->unp_flags |= UNP_WANTCRED;
1377 mac_socketpeer_set_from_socket(so, so3);
1378 mac_socketpeer_set_from_socket(so3, so);
1383 unp = sotounpcb(so);
1384 KASSERT(unp != NULL, (
"unp_connect: unp == NULL"));
1385 unp2 = sotounpcb(so2);
1386 KASSERT(unp2 != NULL, (
"unp_connect: unp2 == NULL"));
1403 VFS_UNLOCK_GIANT(vfslocked);
1407 unp->unp_flags &= ~UNP_CONNECTING;
1418 unp = sotounpcb(so);
1419 KASSERT(unp != NULL, (
"unp_connect2: unp == NULL"));
1420 unp2 = sotounpcb(so2);
1421 KASSERT(unp2 != NULL, (
"unp_connect2: unp2 == NULL"));
1427 if (so2->so_type != so->so_type)
1428 return (EPROTOTYPE);
1429 unp->unp_conn = unp2;
1431 switch (so->so_type) {
1433 LIST_INSERT_HEAD(&unp2->unp_refs, unp, unp_reflink);
1438 case SOCK_SEQPACKET:
1439 unp2->unp_conn = unp;
1440 if (req == PRU_CONNECT &&
1441 ((unp->unp_flags | unp2->unp_flags) & UNP_CONNWAIT))
1449 panic(
"unp_connect2");
1459 KASSERT(unp2 != NULL, (
"unp_disconnect: unp2 == NULL"));
1465 unp->unp_conn = NULL;
1466 switch (unp->unp_socket->so_type) {
1468 LIST_REMOVE(unp, unp_reflink);
1469 so = unp->unp_socket;
1471 so->so_state &= ~SS_ISCONNECTED;
1476 case SOCK_SEQPACKET:
1478 unp2->unp_conn = NULL;
1496 struct unpcb *unp, **unp_list;
1498 struct xunpgen *xug;
1499 struct unp_head *head;
1502 switch ((intptr_t)arg1) {
1511 case SOCK_SEQPACKET:
1516 panic(
"unp_pcblist: arg1 %d", (
int)(intptr_t)arg1);
1523 if (req->oldptr == NULL) {
1525 req->oldidx = 2 * (
sizeof *xug)
1526 + (n + n/8) *
sizeof(
struct xunpcb);
1530 if (req->newptr != NULL)
1536 xug =
malloc(
sizeof(*xug), M_TEMP, M_WAITOK);
1542 xug->xug_len =
sizeof *xug;
1544 xug->xug_gen = gencnt;
1546 error = SYSCTL_OUT(req, xug,
sizeof *xug);
1552 unp_list =
malloc(n *
sizeof *unp_list, M_TEMP, M_WAITOK);
1555 for (unp = LIST_FIRST(head), i = 0; unp && i < n;
1556 unp = LIST_NEXT(unp, unp_link)) {
1558 if (unp->unp_gencnt <= gencnt) {
1560 unp->unp_socket->so_cred)) {
1564 unp_list[i++] = unp;
1565 unp->unp_refcount++;
1573 xu =
malloc(
sizeof(*xu), M_TEMP, M_WAITOK | M_ZERO);
1574 for (i = 0; i < n; i++) {
1577 unp->unp_refcount--;
1578 if (unp->unp_refcount != 0 && unp->unp_gencnt <= gencnt) {
1579 xu->xu_len =
sizeof *xu;
1585 if (unp->unp_addr != NULL)
1586 bcopy(unp->unp_addr, &xu->xu_addr,
1587 unp->unp_addr->sun_len);
1588 if (unp->unp_conn != NULL &&
1589 unp->unp_conn->unp_addr != NULL)
1590 bcopy(unp->unp_conn->unp_addr,
1592 unp->unp_conn->unp_addr->sun_len);
1593 bcopy(unp, &xu->xu_unp,
sizeof *unp);
1596 error = SYSCTL_OUT(req, xu,
sizeof *xu);
1598 freeunp = (unp->unp_refcount == 0);
1617 error = SYSCTL_OUT(req, xug,
sizeof *xug);
1619 free(unp_list, M_TEMP);
1624 SYSCTL_PROC(_net_local_dgram, OID_AUTO, pcblist, CTLTYPE_OPAQUE | CTLFLAG_RD,
1625 (
void *)(intptr_t)SOCK_DGRAM, 0,
unp_pcblist,
"S,xunpcb",
1626 "List of active local datagram sockets");
1627 SYSCTL_PROC(_net_local_stream, OID_AUTO, pcblist, CTLTYPE_OPAQUE | CTLFLAG_RD,
1628 (
void *)(intptr_t)SOCK_STREAM, 0,
unp_pcblist,
"S,xunpcb",
1629 "List of active local stream sockets");
1630 SYSCTL_PROC(_net_local_seqpacket, OID_AUTO, pcblist,
1631 CTLTYPE_OPAQUE | CTLFLAG_RD,
1632 (
void *)(intptr_t)SOCK_SEQPACKET, 0,
unp_pcblist,
"S,xunpcb",
1633 "List of active local seqpacket sockets");
1644 unp2 = unp->unp_conn;
1645 if ((unp->unp_socket->so_type == SOCK_STREAM ||
1646 (unp->unp_socket->so_type == SOCK_SEQPACKET)) && unp2 != NULL) {
1647 so = unp2->unp_socket;
1656 struct socket *so = unp->unp_socket;
1662 so->so_error = errno;
1663 unp2 = unp->unp_conn;
1677 for (i = 0; i < fdcount; i++) {
1687 struct thread *td = curthread;
1688 struct cmsghdr *cm = mtod(control,
struct cmsghdr *);
1694 socklen_t clen = control->m_len, datalen;
1701 if (controlp != NULL)
1703 while (cm != NULL) {
1704 if (
sizeof(*cm) > clen || cm->cmsg_len > clen) {
1708 data = CMSG_DATA(cm);
1709 datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
1710 if (cm->cmsg_level == SOL_SOCKET
1711 && cm->cmsg_type == SCM_RIGHTS) {
1712 newfds = datalen /
sizeof(
struct file *);
1716 if (error || controlp == NULL) {
1720 FILEDESC_XLOCK(td->td_proc->p_fd);
1727 newlen = newfds *
sizeof(int);
1729 SCM_RIGHTS, SOL_SOCKET);
1730 if (*controlp == NULL) {
1731 FILEDESC_XUNLOCK(td->td_proc->p_fd);
1738 CMSG_DATA(mtod(*controlp,
struct cmsghdr *));
1739 if (
fdallocn(td, 0, fdp, newfds) != 0) {
1740 FILEDESC_XUNLOCK(td->td_proc->p_fd);
1747 for (i = 0; i < newfds; i++) {
1749 td->td_proc->p_fd->fd_ofiles[fdp[i]] = fp;
1752 FILEDESC_XUNLOCK(td->td_proc->p_fd);
1755 if (error || controlp == NULL)
1758 cm->cmsg_type, cm->cmsg_level);
1759 if (*controlp == NULL) {
1764 CMSG_DATA(mtod(*controlp,
struct cmsghdr *)),
1767 controlp = &(*controlp)->m_next;
1770 if (CMSG_SPACE(datalen) < clen) {
1771 clen -= CMSG_SPACE(datalen);
1772 cm = (
struct cmsghdr *)
1773 ((caddr_t)cm + CMSG_SPACE(datalen));
1796 if (!IS_DEFAULT_VNET(curvnet))
1799 unp_zone = uma_zcreate(
"unpcb",
sizeof(
struct unpcb), NULL, NULL,
1800 NULL, NULL, UMA_ALIGN_PTR, 0);
1805 NULL, EVENTHANDLER_PRI_ANY);
1809 SLIST_INIT(&unp_defers);
1820 struct mbuf *control = *controlp;
1821 struct proc *p = td->td_proc;
1822 struct filedesc *fdescp = p->p_fd;
1824 struct cmsghdr *cm = mtod(control,
struct cmsghdr *);
1825 struct cmsgcred *cmcred;
1831 socklen_t clen = control->m_len, datalen;
1839 while (cm != NULL) {
1840 if (
sizeof(*cm) > clen || cm->cmsg_level != SOL_SOCKET
1841 || cm->cmsg_len > clen) {
1845 data = CMSG_DATA(cm);
1846 datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
1848 switch (cm->cmsg_type) {
1854 SCM_CREDS, SOL_SOCKET);
1855 if (*controlp == NULL) {
1859 cmcred = (
struct cmsgcred *)
1860 CMSG_DATA(mtod(*controlp,
struct cmsghdr *));
1861 cmcred->cmcred_pid = p->p_pid;
1862 cmcred->cmcred_uid = td->td_ucred->cr_ruid;
1863 cmcred->cmcred_gid = td->td_ucred->cr_rgid;
1864 cmcred->cmcred_euid = td->td_ucred->cr_uid;
1865 cmcred->cmcred_ngroups = MIN(td->td_ucred->cr_ngroups,
1867 for (i = 0; i < cmcred->cmcred_ngroups; i++)
1868 cmcred->cmcred_groups[i] =
1869 td->td_ucred->cr_groups[i];
1873 oldfds = datalen / sizeof (
int);
1879 FILEDESC_SLOCK(fdescp);
1880 for (i = 0; i < oldfds; i++) {
1882 if ((
unsigned)fd >= fdescp->fd_nfiles ||
1883 fdescp->fd_ofiles[fd] == NULL) {
1884 FILEDESC_SUNLOCK(fdescp);
1888 fp = fdescp->fd_ofiles[
fd];
1889 if (!(fp->f_ops->fo_flags & DFLAG_PASSABLE)) {
1890 FILEDESC_SUNLOCK(fdescp);
1901 newlen = oldfds *
sizeof(
struct file *);
1903 SCM_RIGHTS, SOL_SOCKET);
1904 if (*controlp == NULL) {
1905 FILEDESC_SUNLOCK(fdescp);
1910 rp = (
struct file **)
1911 CMSG_DATA(mtod(*controlp,
struct cmsghdr *));
1912 for (i = 0; i < oldfds; i++) {
1913 fp = fdescp->fd_ofiles[*fdp++];
1917 FILEDESC_SUNLOCK(fdescp);
1922 SCM_TIMESTAMP, SOL_SOCKET);
1923 if (*controlp == NULL) {
1927 tv = (
struct timeval *)
1928 CMSG_DATA(mtod(*controlp,
struct cmsghdr *));
1934 SCM_BINTIME, SOL_SOCKET);
1935 if (*controlp == NULL) {
1940 CMSG_DATA(mtod(*controlp,
struct cmsghdr *));
1949 controlp = &(*controlp)->m_next;
1950 if (CMSG_SPACE(datalen) < clen) {
1951 clen -= CMSG_SPACE(datalen);
1952 cm = (
struct cmsghdr *)
1953 ((caddr_t)cm + CMSG_SPACE(datalen));
1965 static struct mbuf *
1968 struct mbuf *m, *n, *n_prev;
1969 struct sockcred *sc;
1970 const struct cmsghdr *cm;
1974 ngroups = MIN(td->td_ucred->cr_ngroups, CMGROUP_MAX);
1975 m =
sbcreatecontrol(NULL, SOCKCREDSIZE(ngroups), SCM_CREDS, SOL_SOCKET);
1979 sc = (
struct sockcred *) CMSG_DATA(mtod(m,
struct cmsghdr *));
1980 sc->sc_uid = td->td_ucred->cr_ruid;
1981 sc->sc_euid = td->td_ucred->cr_uid;
1982 sc->sc_gid = td->td_ucred->cr_rgid;
1983 sc->sc_egid = td->td_ucred->cr_gid;
1984 sc->sc_ngroups = ngroups;
1985 for (i = 0; i < sc->sc_ngroups; i++)
1986 sc->sc_groups[i] = td->td_ucred->cr_groups[i];
1993 if (control != NULL)
1994 for (n = control, n_prev = NULL; n != NULL;) {
1995 cm = mtod(n,
struct cmsghdr *);
1996 if (cm->cmsg_level == SOL_SOCKET &&
1997 cm->cmsg_type == SCM_CREDS) {
1999 control = n->m_next;
2001 n_prev->m_next = n->m_next;
2010 m->m_next = control;
2014 static struct unpcb *
2019 if (fp->f_type != DTYPE_SOCKET)
2021 if ((so = fp->f_data) == NULL)
2023 if (so->so_proto->pr_domain != &localdomain)
2025 return sotounpcb(so);
2034 dr =
malloc(
sizeof(*dr), M_TEMP, M_WAITOK);
2037 SLIST_INSERT_HEAD(&unp_defers, dr, ud_link);
2039 atomic_add_int(&unp_defers_count, 1);
2042 (
void)
closef(fp, (
struct thread *)NULL);
2055 if (SLIST_FIRST(&unp_defers) == NULL) {
2059 SLIST_SWAP(&unp_defers, &drl,
unp_defer);
2062 while ((dr = SLIST_FIRST(&drl)) != NULL) {
2063 SLIST_REMOVE_HEAD(&drl, ud_link);
2068 atomic_add_int(&unp_defers_count, -count);
2078 if ((unp =
fptounp(fp)) != NULL) {
2080 unp->unp_msgcount++;
2094 if ((unp =
fptounp(fp)) != NULL) {
2095 unp->unp_msgcount--;
2117 if ((unp =
fptounp(fp)) == NULL)
2119 if (unp->unp_gcflag & UNPGC_REF)
2121 unp->unp_gcflag &= ~UNPGC_DEAD;
2122 unp->unp_gcflag |= UNPGC_REF;
2134 if (unp->unp_gcflag & UNPGC_SCANNED)
2143 if ((unp->unp_gcflag & UNPGC_REF) == 0 && fp &&
2144 unp->unp_msgcount != 0 && fp->f_count == unp->unp_msgcount) {
2145 unp->unp_gcflag |= UNPGC_DEAD;
2153 so = unp->unp_socket;
2154 SOCKBUF_LOCK(&so->so_rcv);
2156 SOCKBUF_UNLOCK(&so->so_rcv);
2162 TAILQ_FOREACH(soa, &so->so_comp, so_list) {
2163 SOCKBUF_LOCK(&soa->so_rcv);
2165 SOCKBUF_UNLOCK(&soa->so_rcv);
2168 unp->unp_gcflag |= UNPGC_SCANNED;
2172 SYSCTL_INT(_net_local, OID_AUTO, recycled, CTLFLAG_RD, &unp_recycled, 0,
2173 "Number of unreachable sockets claimed by the garbage collector.");
2176 SYSCTL_INT(_net_local, OID_AUTO, taskcount, CTLFLAG_RD, &unp_taskcount, 0,
2177 "Number of times the garbage collector has run.");
2184 struct unp_head **head;
2185 struct file *f, **unref;
2194 for (head = heads; *head != NULL; head++)
2195 LIST_FOREACH(unp, *head, unp_link)
2196 unp->unp_gcflag = 0;
2205 unp_unreachable = 0;
2207 for (head = heads; *head != NULL; head++)
2208 LIST_FOREACH(unp, *head, unp_link)
2210 }
while (unp_marked);
2212 if (unp_unreachable == 0)
2218 unref =
malloc(unp_unreachable *
sizeof(
struct file *),
2227 for (total = 0, head = heads; *head != NULL; head++)
2228 LIST_FOREACH(unp, *head, unp_link)
2229 if ((unp->unp_gcflag & UNPGC_DEAD) != 0) {
2231 if (unp->unp_msgcount == 0 || f == NULL ||
2232 f->f_count != unp->unp_msgcount)
2236 KASSERT(total <= unp_unreachable,
2237 (
"unp_gc: incorrect unreachable count."));
2247 for (i = 0; i < total; i++) {
2250 so = unref[i]->f_data;
2251 CURVNET_SET(so->so_vnet);
2259 for (i = 0; i < total; i++)
2260 fdrop(unref[i], NULL);
2261 unp_recycled += total;
2262 free(unref, M_TEMP);
2281 socklen_t clen, datalen;
2284 while (m0 != NULL) {
2285 for (m = m0; m; m = m->m_next) {
2286 if (m->m_type != MT_CONTROL)
2289 cm = mtod(m,
struct cmsghdr *);
2292 while (cm != NULL) {
2293 if (
sizeof(*cm) > clen || cm->cmsg_len > clen)
2296 data = CMSG_DATA(cm);
2297 datalen = (caddr_t)cm + cm->cmsg_len
2300 if (cm->cmsg_level == SOL_SOCKET &&
2301 cm->cmsg_type == SCM_RIGHTS) {
2302 qfds = datalen /
sizeof (
struct file *);
2304 for (i = 0; i < qfds; i++)
2308 if (CMSG_SPACE(datalen) < clen) {
2309 clen -= CMSG_SPACE(datalen);
2310 cm = (
struct cmsghdr *)
2311 ((caddr_t)cm + CMSG_SPACE(datalen));
2334 ASSERT_VOP_ELOCKED(vp,
"vfs_unp_reclaim");
2335 KASSERT(vp->v_type == VSOCK,
2336 (
"vfs_unp_reclaim: vp->v_type != VSOCK"));
2340 VOP_UNP_CONNECT(vp, &so);
2343 unp = sotounpcb(so);
2347 if (unp->unp_vnode == vp) {
2349 unp->unp_vnode = NULL;
2361 db_print_indent(
int indent)
2365 for (i = 0; i < indent; i++)
2370 db_print_unpflags(
int unp_flags)
2375 if (unp_flags & UNP_HAVEPC) {
2376 db_printf(
"%sUNP_HAVEPC", comma ?
", " :
"");
2379 if (unp_flags & UNP_HAVEPCCACHED) {
2380 db_printf(
"%sUNP_HAVEPCCACHED", comma ?
", " :
"");
2383 if (unp_flags & UNP_WANTCRED) {
2384 db_printf(
"%sUNP_WANTCRED", comma ?
", " :
"");
2387 if (unp_flags & UNP_CONNWAIT) {
2388 db_printf(
"%sUNP_CONNWAIT", comma ?
", " :
"");
2391 if (unp_flags & UNP_CONNECTING) {
2392 db_printf(
"%sUNP_CONNECTING", comma ?
", " :
"");
2395 if (unp_flags & UNP_BINDING) {
2396 db_printf(
"%sUNP_BINDING", comma ?
", " :
"");
2402 db_print_xucred(
int indent,
struct xucred *xu)
2406 db_print_indent(indent);
2407 db_printf(
"cr_version: %u cr_uid: %u cr_ngroups: %d\n",
2408 xu->cr_version, xu->cr_uid, xu->cr_ngroups);
2409 db_print_indent(indent);
2410 db_printf(
"cr_groups: ");
2412 for (i = 0; i < xu->cr_ngroups; i++) {
2413 db_printf(
"%s%u", comma ?
", " :
"", xu->cr_groups[i]);
2420 db_print_unprefs(
int indent,
struct unp_head *uh)
2426 LIST_FOREACH(unp, uh, unp_reflink) {
2427 if (counter % 4 == 0)
2428 db_print_indent(indent);
2429 db_printf(
"%p ", unp);
2430 if (counter % 4 == 3)
2434 if (counter != 0 && counter % 4 != 0)
2438 DB_SHOW_COMMAND(unpcb, db_show_unpcb)
2443 db_printf(
"usage: show unpcb <addr>\n");
2446 unp = (
struct unpcb *)addr;
2448 db_printf(
"unp_socket: %p unp_vnode: %p\n", unp->unp_socket,
2451 db_printf(
"unp_ino: %d unp_conn: %p\n", unp->unp_ino,
2454 db_printf(
"unp_refs:\n");
2455 db_print_unprefs(2, &unp->unp_refs);
2458 db_printf(
"unp_addr: %p\n", unp->unp_addr);
2460 db_printf(
"unp_cc: %d unp_mbcnt: %d unp_gencnt: %llu\n",
2461 unp->unp_cc, unp->unp_mbcnt,
2462 (
unsigned long long)unp->unp_gencnt);
2464 db_printf(
"unp_flags: %x (", unp->unp_flags);
2465 db_print_unpflags(unp->unp_flags);
2468 db_printf(
"unp_peercred:\n");
2469 db_print_xucred(2, &unp->unp_peercred);
2471 db_printf(
"unp_refcount: %u\n", unp->unp_refcount);
static int uipc_attach(struct socket *so, int proto, struct thread *td)
void soisconnected(struct socket *so)
static int uipc_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
int cr_cansee(struct ucred *u1, struct ucred *u2)
static struct mtx unp_defers_lock
int chgsbsize(struct uidinfo *uip, u_int *hiwat, u_int to, rlim_t max)
#define UNP_PCB_LOCK_DESTROY(unp)
static struct rwlock unp_link_rwlock
static void unp_zone_change(void *tag)
void NDFREE(struct nameidata *ndp, const u_int flags)
void cru2x(struct ucred *cr, struct xucred *xcr)
int sooptcopyout(struct sockopt *sopt, const void *buf, size_t len)
int soreceive_dgram(struct socket *so, struct sockaddr **psa, struct uio *uio, struct mbuf **mp0, struct mbuf **controlp, int *flagsp)
static void unp_shutdown(struct unpcb *)
static int uipc_disconnect(struct socket *so)
static uma_zone_t unp_zone
void * malloc(unsigned long size, struct malloc_type *mtp, int flags)
struct sockaddr * sodupsockaddr(const struct sockaddr *sa, int mflags)
static int uipc_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam, struct mbuf *control, struct thread *td)
static u_long unpdg_recvspace
void sbappend_locked(struct sockbuf *sb, struct mbuf *m)
static unp_gen_t unp_gencnt
int soreceive_generic(struct socket *so, struct sockaddr **psa, struct uio *uio, struct mbuf **mp0, struct mbuf **controlp, int *flagsp)
void panic(const char *fmt,...)
void vn_finished_write(struct mount *mp)
static int uipc_shutdown(struct socket *so)
#define UNP_LINK_WLOCK_ASSERT()
static void unp_internalize_fp(struct file *)
#define UNP_PCB_LOCK_INIT(unp)
static void unp_drop(struct unpcb *, int)
int sooptcopyin(struct sockopt *sopt, void *buf, size_t len, size_t minlen)
void solisten_proto(struct socket *so, int backlog)
void socantrcvmore(struct socket *so)
static int unp_unreachable
void m_freem(struct mbuf *mb)
static int unp_pcblist(SYSCTL_HANDLER_ARGS)
static int unp_internalize(struct mbuf **, struct thread *)
static int uipc_sense(struct socket *so, struct stat *sb)
static int uipc_sockaddr(struct socket *so, struct sockaddr **nam)
static struct unp_head unp_dhead
#define UNP_LIST_UNLOCK()
static void unp_gc_process(struct unpcb *unp)
static void uipc_close(struct socket *so)
static struct protosw localsw[]
static struct unp_head unp_shead
#define UNP_LINK_UNLOCK_ASSERT()
static void unp_init(void)
struct socket * sonewconn(struct socket *head, int connstatus)
#define UNP_LIST_LOCK_INIT()
static struct pr_usrreqs uipc_usrreqs_seqpacket
void vput(struct vnode *vp)
static int uipc_peeraddr(struct socket *so, struct sockaddr **nam)
#define UNP_PCB_LOCK_ASSERT(unp)
#define UNP_PCB_LOCK(unp)
static u_long unpsp_recvspace
static int uipc_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
void socantsendmore(struct socket *so)
static void unp_scan(struct mbuf *, void(*)(struct file *))
static struct unp_head unp_sphead
void sorflush(struct socket *so)
static int unp_connect(struct socket *, struct sockaddr *, struct thread *)
static struct domain localdomain
#define UNP_DEFERRED_LOCK()
void vunref(struct vnode *vp)
static void unp_accessable(struct file *fp)
int closef(struct file *fp, struct thread *td)
int sbappendaddr_nospacecheck_locked(struct sockbuf *sb, const struct sockaddr *asa, struct mbuf *m0, struct mbuf *control)
int namei(struct nameidata *ndp)
#define UNP_DEFERRED_LOCK_INIT()
static void unp_freerights(struct file **, int)
void soisconnecting(struct socket *so)
static struct task unp_defer_task
static struct unpcb * fptounp(struct file *fp)
static u_long unpsp_sendspace
#define UNP_PCB_UNLOCK(unp)
static void unp_discard(struct file *)
static struct mbuf * unp_addsockcred(struct thread *, struct mbuf *)
void vfs_unp_reclaim(struct vnode *vp)
struct mbuf * sbcreatecontrol(caddr_t p, int size, int type, int level)
static void unp_dispose(struct mbuf *)
void free(void *addr, struct malloc_type *mtp)
SYSCTL_PROC(_net_local_dgram, OID_AUTO, pcblist, CTLTYPE_OPAQUE|CTLFLAG_RD,(void *)(intptr_t) SOCK_DGRAM, 0, unp_pcblist,"S,xunpcb","List of active local datagram sockets")
int taskqueue_enqueue(struct taskqueue *queue, struct task *task)
static int unp_externalize_fp(struct file *)
#define UNP_LINK_RUNLOCK()
static int uipc_ctloutput(struct socket *, struct sockopt *)
static void unp_gc(__unused void *, int)
static u_long unpst_sendspace
static void uipc_detach(struct socket *so)
static SYSCTL_NODE(_net, PF_LOCAL, local, CTLFLAG_RW, 0,"Local domain")
int soreserve(struct socket *so, u_long sndcc, u_long rcvcc)
#define UNP_DEFERRED_UNLOCK()
void bintime(struct bintime *bt)
int taskqueue_enqueue_timeout(struct taskqueue *queue, struct timeout_task *timeout_task, int ticks)
void vrele(struct vnode *vp)
int vn_start_write(struct vnode *vp, struct mount **mpp, int flags)
static struct mtx unp_list_lock
static int uipc_listen(struct socket *so, int backlog, struct thread *td)
static struct pr_usrreqs uipc_usrreqs_dgram uipc_usrreqs_stream
static int uipc_connect2(struct socket *, struct socket *)
static int unp_externalize(struct mbuf *, struct mbuf **)
int sbappendaddr_locked(struct sockbuf *sb, const struct sockaddr *asa, struct mbuf *m0, struct mbuf *control)
SYSCTL_INT(_net_local, OID_AUTO, inflight, CTLFLAG_RD,&unp_rights, 0,"File descriptors in flight.")
static SLIST_HEAD(unp_defer)
static u_long unpdg_sendspace
void soisdisconnected(struct socket *so)
SYSCTL_ULONG(_net_local_stream, OID_AUTO, sendspace, CTLFLAG_RW,&unpst_sendspace, 0,"Default stream send space.")
static int uipc_accept(struct socket *so, struct sockaddr **nam)
static void uipc_abort(struct socket *so)
#define UNP_LINK_WUNLOCK()
void microtime(struct timeval *tvp)
int solisten_proto_check(struct socket *so)
static int uipc_rcvd(struct socket *so, int flags)
static u_long unpst_recvspace
#define UNP_LINK_LOCK_INIT()
void sotoxsocket(struct socket *so, struct xsocket *xso)
static struct timeout_task unp_gc_task
static void unp_process_defers(void *__unused, int)
int fdallocn(struct thread *td, int minfd, int *fds, int n)
static void unp_disconnect(struct unpcb *unp, struct unpcb *unp2)
int sbappendcontrol_locked(struct sockbuf *sb, struct mbuf *m0, struct mbuf *control)
static struct pr_usrreqs uipc_usrreqs_dgram
static int unp_connect2(struct socket *so, struct socket *so2, int)