1*16431Skarels /* uipc_usrreq.c 6.4 84/05/02 */ 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; 23216054Skarels 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 { 43816054Skarels struct socket *so = unp->unp_socket; 4399169Ssam 44016054Skarels so->so_error = errno; 4419169Ssam unp_disconnect(unp); 44216054Skarels if (so->so_head) { 44316054Skarels so->so_pcb = (caddr_t) 0; 444*16431Skarels m_freem(unp->unp_remaddr); 44516054Skarels (void) m_free(dtom(unp)); 44616054Skarels sofree(so); 44716054Skarels } 4489169Ssam } 4499169Ssam 45012760Ssam #ifdef notdef 4519169Ssam unp_drain() 4529169Ssam { 4539169Ssam 4549169Ssam } 45512760Ssam #endif 45612760Ssam 45712760Ssam unp_externalize(rights) 45812760Ssam struct mbuf *rights; 45912760Ssam { 46012760Ssam int newfds = rights->m_len / sizeof (int); 46112760Ssam register int i; 46212760Ssam register struct file **rp = mtod(rights, struct file **); 46312760Ssam register struct file *fp; 46412760Ssam int f; 46512760Ssam 46612760Ssam if (newfds > ufavail()) { 46712760Ssam for (i = 0; i < newfds; i++) { 46812760Ssam fp = *rp; 46912760Ssam unp_discard(fp); 47012760Ssam *rp++ = 0; 47112760Ssam } 47212760Ssam return (EMSGSIZE); 47312760Ssam } 47412760Ssam for (i = 0; i < newfds; i++) { 47512760Ssam f = ufalloc(0); 47612760Ssam if (f < 0) 47712760Ssam panic("unp_externalize"); 47812760Ssam fp = *rp; 47912760Ssam u.u_ofile[f] = fp; 48012760Ssam fp->f_msgcount--; 48114927Smckusick *(int *)rp++ = f; 48212760Ssam } 48312760Ssam return (0); 48412760Ssam } 48512760Ssam 48612760Ssam unp_internalize(rights) 48712760Ssam struct mbuf *rights; 48812760Ssam { 48912760Ssam register struct file **rp; 49012760Ssam int oldfds = rights->m_len / sizeof (int); 49112760Ssam register int i; 49212760Ssam register struct file *fp; 49312760Ssam 49412760Ssam rp = mtod(rights, struct file **); 49513084Ssam for (i = 0; i < oldfds; i++) 49612760Ssam if (getf(*(int *)rp++) == 0) 49712760Ssam return (EBADF); 49812760Ssam rp = mtod(rights, struct file **); 49913084Ssam for (i = 0; i < oldfds; i++) { 50012760Ssam fp = getf(*(int *)rp); 50112760Ssam *rp++ = fp; 50212760Ssam fp->f_count++; 50312760Ssam fp->f_msgcount++; 50412760Ssam } 50512760Ssam return (0); 50612760Ssam } 50712760Ssam 50812760Ssam int unp_defer, unp_gcing; 50912760Ssam int unp_mark(); 51012760Ssam 51112760Ssam unp_gc() 51212760Ssam { 51312760Ssam register struct file *fp; 51412760Ssam register struct socket *so; 51512760Ssam 51612760Ssam if (unp_gcing) 51712760Ssam return; 51812760Ssam unp_gcing = 1; 51912760Ssam restart: 52012760Ssam unp_defer = 0; 52112760Ssam for (fp = file; fp < fileNFILE; fp++) 52212760Ssam fp->f_flag &= ~(FMARK|FDEFER); 52312760Ssam do { 52412760Ssam for (fp = file; fp < fileNFILE; fp++) { 52512760Ssam if (fp->f_count == 0) 52612760Ssam continue; 52712760Ssam if (fp->f_flag & FDEFER) { 52812760Ssam fp->f_flag &= ~FDEFER; 52912760Ssam unp_defer--; 53012760Ssam } else { 53112760Ssam if (fp->f_flag & FMARK) 53212760Ssam continue; 53312760Ssam if (fp->f_count == fp->f_msgcount) 53412760Ssam continue; 53512760Ssam fp->f_flag |= FMARK; 53612760Ssam } 53712760Ssam if (fp->f_type != DTYPE_SOCKET) 53812760Ssam continue; 53912760Ssam so = (struct socket *)fp->f_data; 54012760Ssam if (so->so_proto->pr_family != AF_UNIX || 54112760Ssam (so->so_proto->pr_flags&PR_ADDR) == 0) 54212760Ssam continue; 54312760Ssam if (so->so_rcv.sb_flags & SB_LOCK) { 54412760Ssam sbwait(&so->so_rcv); 54512760Ssam goto restart; 54612760Ssam } 54712760Ssam unp_scan(so->so_rcv.sb_mb, unp_mark); 54812760Ssam } 54912760Ssam } while (unp_defer); 55012760Ssam for (fp = file; fp < fileNFILE; fp++) { 55112760Ssam if (fp->f_count == 0) 55212760Ssam continue; 55312760Ssam if (fp->f_count == fp->f_msgcount && (fp->f_flag&FMARK)==0) { 55412760Ssam if (fp->f_type != DTYPE_SOCKET) 55512760Ssam panic("unp_gc"); 55612760Ssam (void) soshutdown((struct socket *)fp->f_data, 0); 55712760Ssam } 55812760Ssam } 55912760Ssam unp_gcing = 0; 56012760Ssam } 56112760Ssam 56212760Ssam unp_scan(m, op) 56312760Ssam register struct mbuf *m; 56412760Ssam int (*op)(); 56512760Ssam { 56612760Ssam register struct file **rp; 56712760Ssam register int i; 56812760Ssam int qfds; 56912760Ssam 57012760Ssam while (m) { 57112760Ssam m = m->m_next; 57212760Ssam if (m == 0) 57312760Ssam goto bad; 57412760Ssam if (m->m_len) { 57512760Ssam qfds = m->m_len / sizeof (struct file *); 57612760Ssam rp = mtod(m, struct file **); 57712760Ssam for (i = 0; i < qfds; i++) 57812760Ssam (*op)(*rp++); 57912760Ssam } 58012760Ssam do { 58112760Ssam m = m->m_next; 58212760Ssam if (m == 0) 58312760Ssam goto bad; 58412760Ssam } while (m->m_act == 0); 58512760Ssam m = m->m_next; 58612760Ssam } 58712760Ssam return; 58812760Ssam bad: 58912760Ssam panic("unp_gcscan"); 59012760Ssam } 59112760Ssam 59212760Ssam unp_mark(fp) 59312760Ssam struct file *fp; 59412760Ssam { 59512760Ssam 59612760Ssam if (fp->f_flag & FMARK) 59712760Ssam return; 59812760Ssam unp_defer++; 59912760Ssam fp->f_flag |= (FMARK|FDEFER); 60012760Ssam } 60112760Ssam 60212760Ssam unp_discard(fp) 60312760Ssam struct file *fp; 60412760Ssam { 60512760Ssam 60612760Ssam fp->f_msgcount--; 60713084Ssam closef(fp); 60812760Ssam } 609