1*13119Ssam /* uipc_usrreq.c 1.14 83/06/14 */ 28925Sroot 38925Sroot #include "../h/param.h" 48925Sroot #include "../h/dir.h" 58925Sroot #include "../h/user.h" 68925Sroot #include "../h/mbuf.h" 78925Sroot #include "../h/protosw.h" 88925Sroot #include "../h/socket.h" 98925Sroot #include "../h/socketvar.h" 108925Sroot #include "../h/unpcb.h" 118925Sroot #include "../h/un.h" 128925Sroot #include "../h/inode.h" 139169Ssam #include "../h/nami.h" 1412760Ssam #include "../h/file.h" 158925Sroot 168925Sroot /* 178925Sroot * Unix communications domain. 1812760Ssam * 1912760Ssam * TODO: 2012760Ssam * SEQPACKET, RDM 21*13119Ssam * rethink name space problems 2212760Ssam * need a proper out-of-band 238925Sroot */ 24*13119Ssam struct sockaddr sun_noname = { AF_UNIX }; 258925Sroot 268925Sroot /*ARGSUSED*/ 2712760Ssam uipc_usrreq(so, req, m, nam, rights) 288925Sroot struct socket *so; 298925Sroot int req; 3012760Ssam struct mbuf *m, *nam, *rights; 318925Sroot { 328925Sroot struct unpcb *unp = sotounpcb(so); 338925Sroot register struct socket *so2; 348925Sroot int error = 0; 358925Sroot 3612760Ssam if (req != PRU_SEND && rights && rights->m_len) { 3712760Ssam error = EOPNOTSUPP; 3812760Ssam goto release; 3912760Ssam } 4012760Ssam if (unp == 0 && req != PRU_ATTACH) { 4112760Ssam error = EINVAL; 4212760Ssam goto release; 4312760Ssam } 448925Sroot switch (req) { 458925Sroot 468925Sroot case PRU_ATTACH: 478925Sroot if (unp) { 489169Ssam error = EISCONN; 498925Sroot break; 508925Sroot } 519028Sroot error = unp_attach(so); 528925Sroot break; 538925Sroot 548925Sroot case PRU_DETACH: 558925Sroot unp_detach(unp); 568925Sroot break; 578925Sroot 589169Ssam case PRU_BIND: 599169Ssam error = unp_bind(unp, nam); 609169Ssam break; 619169Ssam 629169Ssam case PRU_LISTEN: 639169Ssam if (unp->unp_inode == 0) 649169Ssam error = EINVAL; 659169Ssam break; 669169Ssam 678925Sroot case PRU_CONNECT: 689028Sroot error = unp_connect(so, nam); 698925Sroot break; 708925Sroot 7112760Ssam case PRU_CONNECT2: 7213115Ssam error = unp_connect2(so, (struct mbuf *)0, 7313115Ssam (struct socket *)nam); 7412760Ssam break; 7512760Ssam 768925Sroot case PRU_DISCONNECT: 778925Sroot unp_disconnect(unp); 788925Sroot break; 798925Sroot 809169Ssam case PRU_ACCEPT: 819169Ssam nam->m_len = unp->unp_remaddr->m_len; 829169Ssam bcopy(mtod(unp->unp_remaddr, caddr_t), 839169Ssam mtod(nam, caddr_t), (unsigned)nam->m_len); 848925Sroot break; 858925Sroot 868925Sroot case PRU_SHUTDOWN: 878925Sroot socantsendmore(so); 888925Sroot unp_usrclosed(unp); 898925Sroot break; 908925Sroot 918925Sroot case PRU_RCVD: 928925Sroot switch (so->so_type) { 938925Sroot 948925Sroot case SOCK_DGRAM: 958925Sroot panic("uipc 1"); 9610139Ssam /*NOTREACHED*/ 978925Sroot 9810139Ssam case SOCK_STREAM: 998925Sroot #define rcv (&so->so_rcv) 1008925Sroot #define snd (&so2->so_snd) 1018925Sroot if (unp->unp_conn == 0) 1028925Sroot break; 1038925Sroot so2 = unp->unp_conn->unp_socket; 1048925Sroot /* 1058925Sroot * Transfer resources back to send port 1068925Sroot * and wakeup any waiting to write. 1078925Sroot */ 1088925Sroot snd->sb_mbmax += rcv->sb_mbmax - rcv->sb_mbcnt; 1098925Sroot rcv->sb_mbmax = rcv->sb_mbcnt; 1108925Sroot snd->sb_hiwat += rcv->sb_hiwat - rcv->sb_cc; 1118925Sroot rcv->sb_hiwat = rcv->sb_cc; 1128925Sroot sbwakeup(snd); 1138925Sroot #undef snd 1148925Sroot #undef rcv 1158925Sroot break; 1168925Sroot 1178925Sroot default: 1188925Sroot panic("uipc 2"); 1198925Sroot } 1208925Sroot break; 1218925Sroot 1228925Sroot case PRU_SEND: 1238925Sroot switch (so->so_type) { 1248925Sroot 1258925Sroot case SOCK_DGRAM: 1269028Sroot if (nam) { 1278925Sroot if (unp->unp_conn) { 1288925Sroot error = EISCONN; 1298925Sroot break; 1308925Sroot } 1319028Sroot error = unp_connect(so, nam); 1328925Sroot if (error) 1338925Sroot break; 1348925Sroot } else { 1358925Sroot if (unp->unp_conn == 0) { 1368925Sroot error = ENOTCONN; 1378925Sroot break; 1388925Sroot } 1398925Sroot } 1408925Sroot so2 = unp->unp_conn->unp_socket; 1419169Ssam /* BEGIN XXX */ 14212760Ssam if (rights) { 14312760Ssam error = unp_internalize(rights); 14412760Ssam if (error) 14512760Ssam break; 14612760Ssam } 14712760Ssam if (sbspace(&so2->so_rcv) > 0) { 148*13119Ssam /* 149*13119Ssam * There's no record of source socket's 150*13119Ssam * name, so send null name for the moment. 151*13119Ssam */ 1529169Ssam (void) sbappendaddr(&so2->so_rcv, 153*13119Ssam &sun_noname, m, rights); 15412760Ssam sbwakeup(&so2->so_rcv); 15512760Ssam m = 0; 15612760Ssam } 1579169Ssam /* END XXX */ 1589028Sroot if (nam) 1599169Ssam unp_disconnect(unp); 1608925Sroot break; 1618925Sroot 1628925Sroot case SOCK_STREAM: 1638925Sroot #define rcv (&so2->so_rcv) 1648925Sroot #define snd (&so->so_snd) 16512760Ssam if (rights && rights->m_len) { 16612760Ssam error = EOPNOTSUPP; 16712760Ssam break; 16812760Ssam } 1698925Sroot if (unp->unp_conn == 0) 1708925Sroot panic("uipc 3"); 1718925Sroot so2 = unp->unp_conn->unp_socket; 1728925Sroot /* 1738925Sroot * Send to paired receive port, and then 1748925Sroot * give it enough resources to hold what it already has. 1758925Sroot * Wake up readers. 1768925Sroot */ 1778925Sroot sbappend(rcv, m); 1788925Sroot snd->sb_mbmax -= rcv->sb_mbcnt - rcv->sb_mbmax; 1798925Sroot rcv->sb_mbmax = rcv->sb_mbcnt; 1808925Sroot snd->sb_hiwat -= rcv->sb_cc - rcv->sb_hiwat; 1818925Sroot rcv->sb_hiwat = rcv->sb_cc; 1828925Sroot sbwakeup(rcv); 1838925Sroot #undef snd 1848925Sroot #undef rcv 1858925Sroot break; 1868925Sroot 1878925Sroot default: 1888925Sroot panic("uipc 4"); 1898925Sroot } 19012760Ssam m = 0; 1918925Sroot break; 1928925Sroot 1938925Sroot case PRU_ABORT: 1948925Sroot unp_drop(unp, ECONNABORTED); 1958925Sroot break; 1968925Sroot 1978925Sroot /* SOME AS YET UNIMPLEMENTED HOOKS */ 1988925Sroot case PRU_CONTROL: 19913050Ssam return (EOPNOTSUPP); 2008925Sroot 2018925Sroot case PRU_SENSE: 2028925Sroot error = EOPNOTSUPP; 2038925Sroot break; 2048925Sroot /* END UNIMPLEMENTED HOOKS */ 2058925Sroot 2068925Sroot case PRU_RCVOOB: 2078925Sroot break; 2088925Sroot 2098925Sroot case PRU_SENDOOB: 2108925Sroot break; 2118925Sroot 2128925Sroot case PRU_SOCKADDR: 2138925Sroot break; 2148925Sroot 2158925Sroot case PRU_SLOWTIMO: 2168925Sroot break; 2178925Sroot 2188925Sroot default: 2198925Sroot panic("piusrreq"); 2208925Sroot } 22112760Ssam release: 22212760Ssam if (m) 22312760Ssam m_freem(m); 22411709Ssam return (error); 2258925Sroot } 2268925Sroot 22712760Ssam /* SHOULD BE PIPSIZ and 0 */ 2288925Sroot int unp_sendspace = 1024*2; 2298925Sroot int unp_recvspace = 1024*2; 2308925Sroot 2319169Ssam unp_attach(so) 2328925Sroot struct socket *so; 2338925Sroot { 2349169Ssam register struct mbuf *m; 2358925Sroot register struct unpcb *unp; 2368925Sroot int error; 2378925Sroot 2388925Sroot error = soreserve(so, unp_sendspace, unp_recvspace); 2398925Sroot if (error) 24010139Ssam return (error); 2419637Ssam m = m_getclr(M_DONTWAIT, MT_PCB); 24210139Ssam if (m == NULL) 24310139Ssam return (ENOBUFS); 2448925Sroot unp = mtod(m, struct unpcb *); 2458925Sroot so->so_pcb = (caddr_t)unp; 2468925Sroot unp->unp_socket = so; 2478925Sroot return (0); 2488925Sroot } 2498925Sroot 2508925Sroot unp_detach(unp) 2519169Ssam register struct unpcb *unp; 2528925Sroot { 2538925Sroot 2548925Sroot if (unp->unp_inode) { 2558925Sroot irele(unp->unp_inode); 2568925Sroot unp->unp_inode = 0; 2578925Sroot } 2588925Sroot if (unp->unp_conn) 2598925Sroot unp_disconnect(unp); 2608925Sroot while (unp->unp_refs) 2618925Sroot unp_drop(unp->unp_refs, ECONNRESET); 2628925Sroot soisdisconnected(unp->unp_socket); 2638925Sroot unp->unp_socket->so_pcb = 0; 2649169Ssam m_freem(unp->unp_remaddr); 2659169Ssam (void) m_free(dtom(unp)); 2668925Sroot } 2678925Sroot 2689169Ssam unp_bind(unp, nam) 2698925Sroot struct unpcb *unp; 2709169Ssam struct mbuf *nam; 2718925Sroot { 2729169Ssam struct sockaddr_un *soun = mtod(nam, struct sockaddr_un *); 2738925Sroot register struct inode *ip; 2749169Ssam extern schar(); 2758925Sroot int error; 2768925Sroot 2778925Sroot u.u_dirp = soun->sun_path; 27812760Ssam if (nam->m_len == MLEN) 27912760Ssam return (EINVAL); 28012760Ssam *(mtod(nam, caddr_t) + nam->m_len) = 0; 28112760Ssam /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */ 2829169Ssam ip = namei(schar, CREATE, 1); 2838925Sroot if (ip) { 2848925Sroot iput(ip); 28510139Ssam return (EADDRINUSE); 2868925Sroot } 28711828Ssam if (error = u.u_error) { 28811828Ssam u.u_error = 0; /* XXX */ 28911828Ssam return (error); 29011828Ssam } 2918925Sroot ip = maknode(IFSOCK | 0777); 2928925Sroot if (ip == NULL) { 2938925Sroot error = u.u_error; /* XXX */ 2948925Sroot u.u_error = 0; /* XXX */ 2958925Sroot return (error); 2968925Sroot } 2978925Sroot ip->i_socket = unp->unp_socket; 2988925Sroot unp->unp_inode = ip; 2998925Sroot iunlock(ip); /* but keep reference */ 3008925Sroot return (0); 3018925Sroot } 3028925Sroot 3039169Ssam unp_connect(so, nam) 3048925Sroot struct socket *so; 3059169Ssam struct mbuf *nam; 3068925Sroot { 3079169Ssam register struct sockaddr_un *soun = mtod(nam, struct sockaddr_un *); 3089169Ssam register struct inode *ip; 3098925Sroot int error; 31012760Ssam register struct socket *so2; 3118925Sroot 3128925Sroot u.u_dirp = soun->sun_path; 31312760Ssam if (nam->m_len + (nam->m_off - MMINOFF) == MLEN) 31412760Ssam return (EMSGSIZE); 31512760Ssam *(mtod(nam, caddr_t) + nam->m_len) = 0; 3169169Ssam ip = namei(schar, LOOKUP, 1); 3178925Sroot if (ip == 0) { 3188925Sroot error = u.u_error; 3198925Sroot u.u_error = 0; 32010139Ssam return (error); /* XXX */ 3218925Sroot } 3228925Sroot if ((ip->i_mode&IFMT) != IFSOCK) { 3238925Sroot error = ENOTSOCK; 3248925Sroot goto bad; 3258925Sroot } 3268925Sroot so2 = ip->i_socket; 3278925Sroot if (so2 == 0) { 3288925Sroot error = ECONNREFUSED; 3298925Sroot goto bad; 3308925Sroot } 33113115Ssam if (so->so_type != so2->so_type) { 33213115Ssam error = EPROTOTYPE; 33313115Ssam goto bad; 33413115Ssam } 33513115Ssam if (so->so_proto->pr_flags & PR_CONNREQUIRED && 33613115Ssam ((so2->so_options&SO_ACCEPTCONN) == 0 || 33713115Ssam (so2 = sonewconn(so2)) == 0)) { 33813115Ssam error = ECONNREFUSED; 33913115Ssam goto bad; 34013115Ssam } 34112760Ssam error = unp_connect2(so, nam, so2); 34212760Ssam bad: 34312760Ssam iput(ip); 34412760Ssam return (error); 34512760Ssam } 34612760Ssam 34712760Ssam unp_connect2(so, sonam, so2) 34812760Ssam register struct socket *so; 34912760Ssam struct mbuf *sonam; 35012760Ssam register struct socket *so2; 35112760Ssam { 35212760Ssam register struct unpcb *unp = sotounpcb(so); 35312760Ssam register struct unpcb *unp2; 35412760Ssam 35512760Ssam if (so2->so_type != so->so_type) 35612760Ssam return (EPROTOTYPE); 3578925Sroot switch (so->so_type) { 3588925Sroot 3598925Sroot case SOCK_DGRAM: 3608925Sroot unp2 = sotounpcb(so2); 36112760Ssam unp->unp_conn = unp2; 3628925Sroot unp->unp_nextref = unp2->unp_refs; 3638925Sroot unp2->unp_refs = unp; 3648925Sroot break; 3658925Sroot 3668925Sroot case SOCK_STREAM: 3679169Ssam unp2 = sotounpcb(so2); 3689169Ssam unp->unp_conn = unp2; 3699169Ssam unp2->unp_conn = unp; 37012760Ssam if (sonam) 37112760Ssam unp2->unp_remaddr = m_copy(sonam, 0, (int)M_COPYALL); 3728925Sroot break; 3738925Sroot 3748925Sroot default: 37512760Ssam panic("unp_connect2"); 3768925Sroot } 3779169Ssam soisconnected(so2); 3788925Sroot soisconnected(so); 3798925Sroot return (0); 3808925Sroot } 3819169Ssam 3829169Ssam unp_disconnect(unp) 3839169Ssam struct unpcb *unp; 3849169Ssam { 3859169Ssam register struct unpcb *unp2 = unp->unp_conn; 3869169Ssam 3879169Ssam if (unp2 == 0) 3889169Ssam return; 3899169Ssam unp->unp_conn = 0; 3909169Ssam soisdisconnected(unp->unp_socket); 3919169Ssam switch (unp->unp_socket->so_type) { 3929169Ssam 3939169Ssam case SOCK_DGRAM: 3949169Ssam if (unp2->unp_refs == unp) 3959169Ssam unp2->unp_refs = unp->unp_nextref; 3969169Ssam else { 3979169Ssam unp2 = unp2->unp_refs; 3989169Ssam for (;;) { 3999169Ssam if (unp2 == 0) 4009169Ssam panic("unp_disconnect"); 4019169Ssam if (unp2->unp_nextref == unp) 4029169Ssam break; 4039169Ssam unp2 = unp2->unp_nextref; 4049169Ssam } 4059169Ssam unp2->unp_nextref = unp->unp_nextref; 4069169Ssam } 4079169Ssam unp->unp_nextref = 0; 4089169Ssam break; 4099169Ssam 4109169Ssam case SOCK_STREAM: 4119169Ssam unp2->unp_conn = 0; 4129169Ssam soisdisconnected(unp2->unp_socket); 4139169Ssam break; 4149169Ssam } 4159169Ssam } 4169169Ssam 41712760Ssam #ifdef notdef 4189169Ssam unp_abort(unp) 4199169Ssam struct unpcb *unp; 4209169Ssam { 4219169Ssam 4229169Ssam unp_detach(unp); 4239169Ssam } 42412760Ssam #endif 4259169Ssam 4269169Ssam /*ARGSUSED*/ 4279169Ssam unp_usrclosed(unp) 4289169Ssam struct unpcb *unp; 4299169Ssam { 4309169Ssam 4319169Ssam } 4329169Ssam 4339169Ssam unp_drop(unp, errno) 4349169Ssam struct unpcb *unp; 4359169Ssam int errno; 4369169Ssam { 4379169Ssam 4389169Ssam unp->unp_socket->so_error = errno; 4399169Ssam unp_disconnect(unp); 4409169Ssam } 4419169Ssam 44212760Ssam #ifdef notdef 4439169Ssam unp_drain() 4449169Ssam { 4459169Ssam 4469169Ssam } 44712760Ssam #endif 44812760Ssam 44912760Ssam unp_externalize(rights) 45012760Ssam struct mbuf *rights; 45112760Ssam { 45212760Ssam int newfds = rights->m_len / sizeof (int); 45312760Ssam register int i; 45412760Ssam register struct file **rp = mtod(rights, struct file **); 45512760Ssam register struct file *fp; 45612760Ssam int f; 45712760Ssam 45812760Ssam if (newfds > ufavail()) { 45912760Ssam for (i = 0; i < newfds; i++) { 46012760Ssam fp = *rp; 46112760Ssam unp_discard(fp); 46212760Ssam *rp++ = 0; 46312760Ssam } 46412760Ssam return (EMSGSIZE); 46512760Ssam } 46612760Ssam for (i = 0; i < newfds; i++) { 46712760Ssam f = ufalloc(0); 46812760Ssam if (f < 0) 46912760Ssam panic("unp_externalize"); 47012760Ssam fp = *rp; 47112760Ssam u.u_ofile[f] = fp; 47212760Ssam fp->f_msgcount--; 47312760Ssam *(int *)rp = f; 47412760Ssam } 47512760Ssam return (0); 47612760Ssam } 47712760Ssam 47812760Ssam unp_internalize(rights) 47912760Ssam struct mbuf *rights; 48012760Ssam { 48112760Ssam register struct file **rp; 48212760Ssam int oldfds = rights->m_len / sizeof (int); 48312760Ssam register int i; 48412760Ssam register struct file *fp; 48512760Ssam 48612760Ssam rp = mtod(rights, struct file **); 48713084Ssam for (i = 0; i < oldfds; i++) 48812760Ssam if (getf(*(int *)rp++) == 0) 48912760Ssam return (EBADF); 49012760Ssam rp = mtod(rights, struct file **); 49113084Ssam for (i = 0; i < oldfds; i++) { 49212760Ssam fp = getf(*(int *)rp); 49312760Ssam *rp++ = fp; 49412760Ssam fp->f_count++; 49512760Ssam fp->f_msgcount++; 49612760Ssam } 49712760Ssam return (0); 49812760Ssam } 49912760Ssam 50012760Ssam int unp_defer, unp_gcing; 50112760Ssam int unp_mark(); 50212760Ssam 50312760Ssam unp_gc() 50412760Ssam { 50512760Ssam register struct file *fp; 50612760Ssam register struct socket *so; 50712760Ssam 50812760Ssam if (unp_gcing) 50912760Ssam return; 51012760Ssam unp_gcing = 1; 51112760Ssam restart: 51212760Ssam unp_defer = 0; 51312760Ssam for (fp = file; fp < fileNFILE; fp++) 51412760Ssam fp->f_flag &= ~(FMARK|FDEFER); 51512760Ssam do { 51612760Ssam for (fp = file; fp < fileNFILE; fp++) { 51712760Ssam if (fp->f_count == 0) 51812760Ssam continue; 51912760Ssam if (fp->f_flag & FDEFER) { 52012760Ssam fp->f_flag &= ~FDEFER; 52112760Ssam unp_defer--; 52212760Ssam } else { 52312760Ssam if (fp->f_flag & FMARK) 52412760Ssam continue; 52512760Ssam if (fp->f_count == fp->f_msgcount) 52612760Ssam continue; 52712760Ssam fp->f_flag |= FMARK; 52812760Ssam } 52912760Ssam if (fp->f_type != DTYPE_SOCKET) 53012760Ssam continue; 53112760Ssam so = (struct socket *)fp->f_data; 53212760Ssam if (so->so_proto->pr_family != AF_UNIX || 53312760Ssam (so->so_proto->pr_flags&PR_ADDR) == 0) 53412760Ssam continue; 53512760Ssam if (so->so_rcv.sb_flags & SB_LOCK) { 53612760Ssam sbwait(&so->so_rcv); 53712760Ssam goto restart; 53812760Ssam } 53912760Ssam unp_scan(so->so_rcv.sb_mb, unp_mark); 54012760Ssam } 54112760Ssam } while (unp_defer); 54212760Ssam for (fp = file; fp < fileNFILE; fp++) { 54312760Ssam if (fp->f_count == 0) 54412760Ssam continue; 54512760Ssam if (fp->f_count == fp->f_msgcount && (fp->f_flag&FMARK)==0) { 54612760Ssam if (fp->f_type != DTYPE_SOCKET) 54712760Ssam panic("unp_gc"); 54812760Ssam (void) soshutdown((struct socket *)fp->f_data, 0); 54912760Ssam } 55012760Ssam } 55112760Ssam unp_gcing = 0; 55212760Ssam } 55312760Ssam 55412760Ssam unp_scan(m, op) 55512760Ssam register struct mbuf *m; 55612760Ssam int (*op)(); 55712760Ssam { 55812760Ssam register struct file **rp; 55912760Ssam register int i; 56012760Ssam int qfds; 56112760Ssam 56212760Ssam while (m) { 56312760Ssam m = m->m_next; 56412760Ssam if (m == 0) 56512760Ssam goto bad; 56612760Ssam if (m->m_len) { 56712760Ssam qfds = m->m_len / sizeof (struct file *); 56812760Ssam rp = mtod(m, struct file **); 56912760Ssam for (i = 0; i < qfds; i++) 57012760Ssam (*op)(*rp++); 57112760Ssam } 57212760Ssam do { 57312760Ssam m = m->m_next; 57412760Ssam if (m == 0) 57512760Ssam goto bad; 57612760Ssam } while (m->m_act == 0); 57712760Ssam m = m->m_next; 57812760Ssam } 57912760Ssam return; 58012760Ssam bad: 58112760Ssam panic("unp_gcscan"); 58212760Ssam } 58312760Ssam 58412760Ssam unp_mark(fp) 58512760Ssam struct file *fp; 58612760Ssam { 58712760Ssam 58812760Ssam if (fp->f_flag & FMARK) 58912760Ssam return; 59012760Ssam unp_defer++; 59112760Ssam fp->f_flag |= (FMARK|FDEFER); 59212760Ssam } 59312760Ssam 59412760Ssam unp_discard(fp) 59512760Ssam struct file *fp; 59612760Ssam { 59712760Ssam 59812760Ssam fp->f_msgcount--; 59913084Ssam closef(fp); 60012760Ssam } 601