1*16054Skarels /* uipc_usrreq.c 6.3 84/02/15 */ 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 2113119Ssam * rethink name space problems 2212760Ssam * need a proper out-of-band 238925Sroot */ 2413119Ssam 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) { 14813119Ssam /* 14913119Ssam * There's no record of source socket's 15013119Ssam * name, so send null name for the moment. 15113119Ssam */ 1529169Ssam (void) sbappendaddr(&so2->so_rcv, 15313119Ssam &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 21514121Ssam case PRU_PEERADDR: 21614121Ssam break; 21714121Ssam 2188925Sroot case PRU_SLOWTIMO: 2198925Sroot break; 2208925Sroot 2218925Sroot default: 2228925Sroot panic("piusrreq"); 2238925Sroot } 22412760Ssam release: 22512760Ssam if (m) 22612760Ssam m_freem(m); 22711709Ssam return (error); 2288925Sroot } 2298925Sroot 23012760Ssam /* SHOULD BE PIPSIZ and 0 */ 2318925Sroot int unp_sendspace = 1024*2; 232*16054Skarels int unp_recvspace = 1024*2 + sizeof(struct sockaddr); 2338925Sroot 2349169Ssam unp_attach(so) 2358925Sroot struct socket *so; 2368925Sroot { 2379169Ssam register struct mbuf *m; 2388925Sroot register struct unpcb *unp; 2398925Sroot int error; 2408925Sroot 2418925Sroot error = soreserve(so, unp_sendspace, unp_recvspace); 2428925Sroot if (error) 24310139Ssam return (error); 2449637Ssam m = m_getclr(M_DONTWAIT, MT_PCB); 24510139Ssam if (m == NULL) 24610139Ssam return (ENOBUFS); 2478925Sroot unp = mtod(m, struct unpcb *); 2488925Sroot so->so_pcb = (caddr_t)unp; 2498925Sroot unp->unp_socket = so; 2508925Sroot return (0); 2518925Sroot } 2528925Sroot 2538925Sroot unp_detach(unp) 2549169Ssam register struct unpcb *unp; 2558925Sroot { 2568925Sroot 2578925Sroot if (unp->unp_inode) { 2588925Sroot irele(unp->unp_inode); 2598925Sroot unp->unp_inode = 0; 2608925Sroot } 2618925Sroot if (unp->unp_conn) 2628925Sroot unp_disconnect(unp); 2638925Sroot while (unp->unp_refs) 2648925Sroot unp_drop(unp->unp_refs, ECONNRESET); 2658925Sroot soisdisconnected(unp->unp_socket); 2668925Sroot unp->unp_socket->so_pcb = 0; 2679169Ssam m_freem(unp->unp_remaddr); 2689169Ssam (void) m_free(dtom(unp)); 2698925Sroot } 2708925Sroot 2719169Ssam unp_bind(unp, nam) 2728925Sroot struct unpcb *unp; 2739169Ssam struct mbuf *nam; 2748925Sroot { 2759169Ssam struct sockaddr_un *soun = mtod(nam, struct sockaddr_un *); 2768925Sroot register struct inode *ip; 2779169Ssam extern schar(); 2788925Sroot int error; 2798925Sroot 2808925Sroot u.u_dirp = soun->sun_path; 28112760Ssam if (nam->m_len == MLEN) 28212760Ssam return (EINVAL); 28312760Ssam *(mtod(nam, caddr_t) + nam->m_len) = 0; 28412760Ssam /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */ 2859169Ssam ip = namei(schar, CREATE, 1); 2868925Sroot if (ip) { 2878925Sroot iput(ip); 28810139Ssam return (EADDRINUSE); 2898925Sroot } 29011828Ssam if (error = u.u_error) { 29111828Ssam u.u_error = 0; /* XXX */ 29211828Ssam return (error); 29311828Ssam } 2948925Sroot ip = maknode(IFSOCK | 0777); 2958925Sroot if (ip == NULL) { 2968925Sroot error = u.u_error; /* XXX */ 2978925Sroot u.u_error = 0; /* XXX */ 2988925Sroot return (error); 2998925Sroot } 3008925Sroot ip->i_socket = unp->unp_socket; 3018925Sroot unp->unp_inode = ip; 3028925Sroot iunlock(ip); /* but keep reference */ 3038925Sroot return (0); 3048925Sroot } 3058925Sroot 3069169Ssam unp_connect(so, nam) 3078925Sroot struct socket *so; 3089169Ssam struct mbuf *nam; 3098925Sroot { 3109169Ssam register struct sockaddr_un *soun = mtod(nam, struct sockaddr_un *); 3119169Ssam register struct inode *ip; 3128925Sroot int error; 31312760Ssam register struct socket *so2; 3148925Sroot 3158925Sroot u.u_dirp = soun->sun_path; 31612760Ssam if (nam->m_len + (nam->m_off - MMINOFF) == MLEN) 31712760Ssam return (EMSGSIZE); 31812760Ssam *(mtod(nam, caddr_t) + nam->m_len) = 0; 3199169Ssam ip = namei(schar, LOOKUP, 1); 3208925Sroot if (ip == 0) { 3218925Sroot error = u.u_error; 3228925Sroot u.u_error = 0; 32310139Ssam return (error); /* XXX */ 3248925Sroot } 3258925Sroot if ((ip->i_mode&IFMT) != IFSOCK) { 3268925Sroot error = ENOTSOCK; 3278925Sroot goto bad; 3288925Sroot } 3298925Sroot so2 = ip->i_socket; 3308925Sroot if (so2 == 0) { 3318925Sroot error = ECONNREFUSED; 3328925Sroot goto bad; 3338925Sroot } 33413115Ssam if (so->so_type != so2->so_type) { 33513115Ssam error = EPROTOTYPE; 33613115Ssam goto bad; 33713115Ssam } 33813115Ssam if (so->so_proto->pr_flags & PR_CONNREQUIRED && 33913115Ssam ((so2->so_options&SO_ACCEPTCONN) == 0 || 34013115Ssam (so2 = sonewconn(so2)) == 0)) { 34113115Ssam error = ECONNREFUSED; 34213115Ssam goto bad; 34313115Ssam } 34412760Ssam error = unp_connect2(so, nam, so2); 34512760Ssam bad: 34612760Ssam iput(ip); 34712760Ssam return (error); 34812760Ssam } 34912760Ssam 35012760Ssam unp_connect2(so, sonam, so2) 35112760Ssam register struct socket *so; 35212760Ssam struct mbuf *sonam; 35312760Ssam register struct socket *so2; 35412760Ssam { 35512760Ssam register struct unpcb *unp = sotounpcb(so); 35612760Ssam register struct unpcb *unp2; 35712760Ssam 35812760Ssam if (so2->so_type != so->so_type) 35912760Ssam return (EPROTOTYPE); 36014049Ssam unp2 = sotounpcb(so2); 36114049Ssam unp->unp_conn = unp2; 3628925Sroot switch (so->so_type) { 3638925Sroot 3648925Sroot case SOCK_DGRAM: 3658925Sroot unp->unp_nextref = unp2->unp_refs; 3668925Sroot unp2->unp_refs = unp; 3678925Sroot break; 3688925Sroot 3698925Sroot case SOCK_STREAM: 3709169Ssam unp2->unp_conn = unp; 37112760Ssam if (sonam) 37212760Ssam unp2->unp_remaddr = m_copy(sonam, 0, (int)M_COPYALL); 37314049Ssam soisconnected(so2); 37414049Ssam soisconnected(so); 3758925Sroot break; 3768925Sroot 3778925Sroot default: 37812760Ssam panic("unp_connect2"); 3798925Sroot } 3808925Sroot return (0); 3818925Sroot } 3829169Ssam 3839169Ssam unp_disconnect(unp) 3849169Ssam struct unpcb *unp; 3859169Ssam { 3869169Ssam register struct unpcb *unp2 = unp->unp_conn; 3879169Ssam 3889169Ssam if (unp2 == 0) 3899169Ssam return; 3909169Ssam unp->unp_conn = 0; 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: 41114049Ssam soisdisconnected(unp->unp_socket); 4129169Ssam unp2->unp_conn = 0; 4139169Ssam soisdisconnected(unp2->unp_socket); 4149169Ssam break; 4159169Ssam } 4169169Ssam } 4179169Ssam 41812760Ssam #ifdef notdef 4199169Ssam unp_abort(unp) 4209169Ssam struct unpcb *unp; 4219169Ssam { 4229169Ssam 4239169Ssam unp_detach(unp); 4249169Ssam } 42512760Ssam #endif 4269169Ssam 4279169Ssam /*ARGSUSED*/ 4289169Ssam unp_usrclosed(unp) 4299169Ssam struct unpcb *unp; 4309169Ssam { 4319169Ssam 4329169Ssam } 4339169Ssam 4349169Ssam unp_drop(unp, errno) 4359169Ssam struct unpcb *unp; 4369169Ssam int errno; 4379169Ssam { 438*16054Skarels struct socket *so = unp->unp_socket; 4399169Ssam 440*16054Skarels so->so_error = errno; 4419169Ssam unp_disconnect(unp); 442*16054Skarels if (so->so_head) { 443*16054Skarels so->so_pcb = (caddr_t) 0; 444*16054Skarels (void) m_free(dtom(unp)); 445*16054Skarels sofree(so); 446*16054Skarels } 4479169Ssam } 4489169Ssam 44912760Ssam #ifdef notdef 4509169Ssam unp_drain() 4519169Ssam { 4529169Ssam 4539169Ssam } 45412760Ssam #endif 45512760Ssam 45612760Ssam unp_externalize(rights) 45712760Ssam struct mbuf *rights; 45812760Ssam { 45912760Ssam int newfds = rights->m_len / sizeof (int); 46012760Ssam register int i; 46112760Ssam register struct file **rp = mtod(rights, struct file **); 46212760Ssam register struct file *fp; 46312760Ssam int f; 46412760Ssam 46512760Ssam if (newfds > ufavail()) { 46612760Ssam for (i = 0; i < newfds; i++) { 46712760Ssam fp = *rp; 46812760Ssam unp_discard(fp); 46912760Ssam *rp++ = 0; 47012760Ssam } 47112760Ssam return (EMSGSIZE); 47212760Ssam } 47312760Ssam for (i = 0; i < newfds; i++) { 47412760Ssam f = ufalloc(0); 47512760Ssam if (f < 0) 47612760Ssam panic("unp_externalize"); 47712760Ssam fp = *rp; 47812760Ssam u.u_ofile[f] = fp; 47912760Ssam fp->f_msgcount--; 48014927Smckusick *(int *)rp++ = f; 48112760Ssam } 48212760Ssam return (0); 48312760Ssam } 48412760Ssam 48512760Ssam unp_internalize(rights) 48612760Ssam struct mbuf *rights; 48712760Ssam { 48812760Ssam register struct file **rp; 48912760Ssam int oldfds = rights->m_len / sizeof (int); 49012760Ssam register int i; 49112760Ssam register struct file *fp; 49212760Ssam 49312760Ssam rp = mtod(rights, struct file **); 49413084Ssam for (i = 0; i < oldfds; i++) 49512760Ssam if (getf(*(int *)rp++) == 0) 49612760Ssam return (EBADF); 49712760Ssam rp = mtod(rights, struct file **); 49813084Ssam for (i = 0; i < oldfds; i++) { 49912760Ssam fp = getf(*(int *)rp); 50012760Ssam *rp++ = fp; 50112760Ssam fp->f_count++; 50212760Ssam fp->f_msgcount++; 50312760Ssam } 50412760Ssam return (0); 50512760Ssam } 50612760Ssam 50712760Ssam int unp_defer, unp_gcing; 50812760Ssam int unp_mark(); 50912760Ssam 51012760Ssam unp_gc() 51112760Ssam { 51212760Ssam register struct file *fp; 51312760Ssam register struct socket *so; 51412760Ssam 51512760Ssam if (unp_gcing) 51612760Ssam return; 51712760Ssam unp_gcing = 1; 51812760Ssam restart: 51912760Ssam unp_defer = 0; 52012760Ssam for (fp = file; fp < fileNFILE; fp++) 52112760Ssam fp->f_flag &= ~(FMARK|FDEFER); 52212760Ssam do { 52312760Ssam for (fp = file; fp < fileNFILE; fp++) { 52412760Ssam if (fp->f_count == 0) 52512760Ssam continue; 52612760Ssam if (fp->f_flag & FDEFER) { 52712760Ssam fp->f_flag &= ~FDEFER; 52812760Ssam unp_defer--; 52912760Ssam } else { 53012760Ssam if (fp->f_flag & FMARK) 53112760Ssam continue; 53212760Ssam if (fp->f_count == fp->f_msgcount) 53312760Ssam continue; 53412760Ssam fp->f_flag |= FMARK; 53512760Ssam } 53612760Ssam if (fp->f_type != DTYPE_SOCKET) 53712760Ssam continue; 53812760Ssam so = (struct socket *)fp->f_data; 53912760Ssam if (so->so_proto->pr_family != AF_UNIX || 54012760Ssam (so->so_proto->pr_flags&PR_ADDR) == 0) 54112760Ssam continue; 54212760Ssam if (so->so_rcv.sb_flags & SB_LOCK) { 54312760Ssam sbwait(&so->so_rcv); 54412760Ssam goto restart; 54512760Ssam } 54612760Ssam unp_scan(so->so_rcv.sb_mb, unp_mark); 54712760Ssam } 54812760Ssam } while (unp_defer); 54912760Ssam for (fp = file; fp < fileNFILE; fp++) { 55012760Ssam if (fp->f_count == 0) 55112760Ssam continue; 55212760Ssam if (fp->f_count == fp->f_msgcount && (fp->f_flag&FMARK)==0) { 55312760Ssam if (fp->f_type != DTYPE_SOCKET) 55412760Ssam panic("unp_gc"); 55512760Ssam (void) soshutdown((struct socket *)fp->f_data, 0); 55612760Ssam } 55712760Ssam } 55812760Ssam unp_gcing = 0; 55912760Ssam } 56012760Ssam 56112760Ssam unp_scan(m, op) 56212760Ssam register struct mbuf *m; 56312760Ssam int (*op)(); 56412760Ssam { 56512760Ssam register struct file **rp; 56612760Ssam register int i; 56712760Ssam int qfds; 56812760Ssam 56912760Ssam while (m) { 57012760Ssam m = m->m_next; 57112760Ssam if (m == 0) 57212760Ssam goto bad; 57312760Ssam if (m->m_len) { 57412760Ssam qfds = m->m_len / sizeof (struct file *); 57512760Ssam rp = mtod(m, struct file **); 57612760Ssam for (i = 0; i < qfds; i++) 57712760Ssam (*op)(*rp++); 57812760Ssam } 57912760Ssam do { 58012760Ssam m = m->m_next; 58112760Ssam if (m == 0) 58212760Ssam goto bad; 58312760Ssam } while (m->m_act == 0); 58412760Ssam m = m->m_next; 58512760Ssam } 58612760Ssam return; 58712760Ssam bad: 58812760Ssam panic("unp_gcscan"); 58912760Ssam } 59012760Ssam 59112760Ssam unp_mark(fp) 59212760Ssam struct file *fp; 59312760Ssam { 59412760Ssam 59512760Ssam if (fp->f_flag & FMARK) 59612760Ssam return; 59712760Ssam unp_defer++; 59812760Ssam fp->f_flag |= (FMARK|FDEFER); 59912760Ssam } 60012760Ssam 60112760Ssam unp_discard(fp) 60212760Ssam struct file *fp; 60312760Ssam { 60412760Ssam 60512760Ssam fp->f_msgcount--; 60613084Ssam closef(fp); 60712760Ssam } 608