1*5095Swnj /* uipc_socket.c 4.15 81/11/26 */ 24786Swnj 34786Swnj #include "../h/param.h" 44829Swnj #include "../h/systm.h" 54786Swnj #include "../h/dir.h" 64786Swnj #include "../h/user.h" 74829Swnj #include "../h/proc.h" 84829Swnj #include "../h/file.h" 94786Swnj #include "../h/inode.h" 104829Swnj #include "../h/buf.h" 114786Swnj #include "../h/mbuf.h" 124829Swnj #include "../h/protosw.h" 134829Swnj #include "../h/socket.h" 144829Swnj #include "../h/socketvar.h" 154916Swnj #include "../h/stat.h" 16*5095Swnj #include "../net/in.h" 17*5095Swnj #include "../net/in_systm.h" 184786Swnj 194786Swnj /* 204890Swnj * Socket support routines. 214890Swnj * 224890Swnj * DEAL WITH INTERRUPT NOTIFICATION. 234786Swnj */ 244786Swnj 254786Swnj /* 264786Swnj * Create a socket. 274786Swnj */ 284927Swnj socreate(aso, type, asp, asa, options) 294786Swnj struct socket **aso; 304786Swnj int type; 314927Swnj struct sockproto *asp; 324927Swnj struct sockaddr *asa; 334829Swnj int options; 344786Swnj { 354786Swnj register struct protosw *prp; 364786Swnj register struct socket *so; 374786Swnj struct mbuf *m; 384890Swnj int pf, proto, error; 394927Swnj COUNT(SOCREATE); 404786Swnj 414786Swnj /* 424890Swnj * Use process standard protocol/protocol family if none 434890Swnj * specified by address argument. 444786Swnj */ 454927Swnj if (asp == 0) { 464890Swnj pf = PF_INET; /* should be u.u_protof */ 474786Swnj proto = 0; 484786Swnj } else { 494927Swnj pf = asp->sp_family; 504927Swnj proto = asp->sp_protocol; 514786Swnj } 524786Swnj 534786Swnj /* 544890Swnj * If protocol specified, look for it, otherwise 554890Swnj * for a protocol of the correct type in the right family. 564890Swnj */ 574890Swnj if (proto) 584890Swnj prp = pffindproto(pf, proto); 594890Swnj else 604890Swnj prp = pffindtype(pf, type); 614890Swnj if (prp == 0) 624890Swnj return (EPROTONOSUPPORT); 634890Swnj 644890Swnj /* 654786Swnj * Get a socket structure. 664786Swnj */ 674890Swnj m = m_getclr(M_WAIT); 684786Swnj if (m == 0) 694786Swnj return (ENOBUFS); 704786Swnj so = mtod(m, struct socket *); 714829Swnj so->so_options = options; 724786Swnj 734786Swnj /* 744890Swnj * Attach protocol to socket, initializing 754890Swnj * and reserving resources. 764786Swnj */ 774786Swnj so->so_proto = prp; 784979Swnj error = (*prp->pr_usrreq)(so, PRU_ATTACH, 0, asa); 794979Swnj if (error) { 804971Swnj (void) m_free(dtom(so)); 814890Swnj return (error); 824786Swnj } 834786Swnj *aso = so; 844786Swnj return (0); 854786Swnj } 864786Swnj 874916Swnj sofree(so) 884916Swnj struct socket *so; 894916Swnj { 904916Swnj 914927Swnj COUNT(SOFREE); 924950Swnj if (so->so_pcb || (so->so_state & SS_USERGONE) == 0) 934950Swnj return; 944950Swnj sbrelease(&so->so_snd); 954950Swnj sbrelease(&so->so_rcv); 964971Swnj (void) m_free(dtom(so)); 974916Swnj } 984916Swnj 994786Swnj /* 1004890Swnj * Close a socket on last file table reference removal. 1014890Swnj * Initiate disconnect if connected. 1024890Swnj * Free socket when disconnect complete. 1034829Swnj */ 1044890Swnj soclose(so) 1054829Swnj register struct socket *so; 1064829Swnj { 1074890Swnj int s = splnet(); /* conservative */ 1084829Swnj 1094927Swnj COUNT(SOCLOSE); 1104890Swnj if (so->so_pcb == 0) 1114890Swnj goto discard; 1124890Swnj if (so->so_state & SS_ISCONNECTED) { 1134890Swnj if ((so->so_state & SS_ISDISCONNECTING) == 0) { 1144927Swnj u.u_error = sodisconnect(so, (struct sockaddr *)0); 1154890Swnj if (u.u_error) { 1164890Swnj splx(s); 1174890Swnj return; 1184890Swnj } 1194890Swnj } 1204890Swnj if ((so->so_state & SS_ISDISCONNECTING) && 1214890Swnj (so->so_options & SO_NBIO)) { 1224890Swnj u.u_error = EINPROGRESS; 1234890Swnj splx(s); 1244890Swnj return; 1254890Swnj } 1264890Swnj while (so->so_state & SS_ISCONNECTED) 1274890Swnj sleep((caddr_t)&so->so_timeo, PZERO+1); 1284890Swnj } 1294890Swnj u.u_error = (*so->so_proto->pr_usrreq)(so, PRU_DETACH, 0, 0); 1304890Swnj discard: 1314950Swnj so->so_state |= SS_USERGONE; 1324950Swnj sofree(so); 1334890Swnj splx(s); 1344829Swnj } 1354829Swnj 1364927Swnj sosplice(pso, so) 1374927Swnj struct socket *pso, *so; 1384927Swnj { 1394927Swnj 1404927Swnj COUNT(SOSPLICE); 1414927Swnj if (pso->so_proto->pr_family != PF_LOCAL) { 1424927Swnj struct socket *tso; 1434927Swnj tso = pso; pso = so; so = tso; 1444927Swnj } 1454927Swnj if (pso->so_proto->pr_family != PF_LOCAL) 1464927Swnj return (EOPNOTSUPP); 1474927Swnj /* check types and buffer space */ 1484927Swnj /* merge buffers */ 1494927Swnj return (0); 1504927Swnj } 1514927Swnj 1524916Swnj /*ARGSUSED*/ 1534890Swnj sostat(so, sb) 1544829Swnj struct socket *so; 1554890Swnj struct stat *sb; 1564829Swnj { 1574829Swnj 1584927Swnj COUNT(SOSTAT); 1594890Swnj return (EOPNOTSUPP); 1604829Swnj } 1614829Swnj 1624829Swnj /* 1634927Swnj * Accept connection on a socket. 1644927Swnj */ 1654927Swnj soaccept(so, asa) 1664927Swnj struct socket *so; 1674927Swnj struct sockaddr *asa; 1684927Swnj { 1694927Swnj int s = splnet(); 1704927Swnj int error; 1714927Swnj 1724927Swnj COUNT(SOACCEPT); 1734927Swnj if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) { 1744927Swnj error = EISCONN; 1754927Swnj goto bad; 1764927Swnj } 1774927Swnj if ((so->so_options & SO_ACCEPTCONN) == 0) { 1784927Swnj error = EINVAL; /* XXX */ 1794927Swnj goto bad; 1804927Swnj } 1814927Swnj error = (*so->so_proto->pr_usrreq)(so, PRU_ACCEPT, 0, (caddr_t)asa); 1824927Swnj bad: 1834927Swnj splx(s); 1844927Swnj return (error); 1854927Swnj } 1864927Swnj 1874927Swnj /* 1884890Swnj * Connect socket to a specified address. 1894890Swnj * If already connected or connecting, then avoid 1904890Swnj * the protocol entry, to keep its job simpler. 1914786Swnj */ 1924927Swnj soconnect(so, asa) 1934786Swnj struct socket *so; 1944927Swnj struct sockaddr *asa; 1954786Swnj { 1964890Swnj int s = splnet(); 1974890Swnj int error; 1984786Swnj 1994927Swnj COUNT(SOCONNECT); 2004890Swnj if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) { 2014890Swnj error = EISCONN; 2024890Swnj goto bad; 2034890Swnj } 2044927Swnj error = (*so->so_proto->pr_usrreq)(so, PRU_CONNECT, 0, (caddr_t)asa); 2054890Swnj bad: 2064890Swnj splx(s); 2074890Swnj return (error); 2084786Swnj } 2094786Swnj 2104786Swnj /* 2114890Swnj * Disconnect from a socket. 2124890Swnj * Address parameter is from system call for later multicast 2134890Swnj * protocols. Check to make sure that connected and no disconnect 2144890Swnj * in progress (for protocol's sake), and then invoke protocol. 2154786Swnj */ 2164927Swnj sodisconnect(so, asa) 2174786Swnj struct socket *so; 2184927Swnj struct sockaddr *asa; 2194786Swnj { 2204890Swnj int s = splnet(); 2214890Swnj int error; 2224786Swnj 2234927Swnj COUNT(SODISCONNECT); 2244890Swnj if ((so->so_state & SS_ISCONNECTED) == 0) { 2254890Swnj error = ENOTCONN; 2264890Swnj goto bad; 2274890Swnj } 2284890Swnj if (so->so_state & SS_ISDISCONNECTING) { 2294890Swnj error = EALREADY; 2304890Swnj goto bad; 2314890Swnj } 2324927Swnj error = (*so->so_proto->pr_usrreq)(so, PRU_DISCONNECT, 0, asa); 2334890Swnj bad: 2344890Swnj splx(s); 2354890Swnj return (error); 2364786Swnj } 2374786Swnj 2384786Swnj /* 2394890Swnj * Send on a socket. 2404890Swnj * If send must go all at once and message is larger than 2414890Swnj * send buffering, then hard error. 2424890Swnj * Lock against other senders. 2434890Swnj * If must go all at once and not enough room now, then 2444890Swnj * inform user that this would block and do nothing. 2454786Swnj */ 2464927Swnj sosend(so, asa) 2474786Swnj register struct socket *so; 2484927Swnj struct sockaddr *asa; 2494786Swnj { 2504890Swnj struct mbuf *top = 0; 2514890Swnj register struct mbuf *m, **mp = ⊤ 2524916Swnj register u_int len; 2534916Swnj int error = 0, space, s; 2544786Swnj 2554927Swnj COUNT(SOSEND); 2564890Swnj if (so->so_state & SS_CANTSENDMORE) 2574890Swnj return (EPIPE); 2584890Swnj if (sosendallatonce(so) && u.u_count > so->so_snd.sb_hiwat) 2594890Swnj return (EMSGSIZE); 2604890Swnj if ((so->so_snd.sb_flags & SB_LOCK) && (so->so_options & SO_NBIO)) 2614890Swnj return (EWOULDBLOCK); 2624890Swnj sblock(&so->so_snd); 2634890Swnj #define snderr(errno) { error = errno; splx(s); goto release; } 2644890Swnj 2654890Swnj s = splnet(); 2664890Swnj again: 2674890Swnj if ((so->so_state & SS_ISCONNECTED) == 0) { 2684890Swnj if (so->so_proto->pr_flags & PR_CONNREQUIRED) 2694890Swnj snderr(ENOTCONN); 2704927Swnj if (asa == 0) 2714890Swnj snderr(EDESTADDRREQ); 2724890Swnj } 2734890Swnj if (so->so_error) 2744890Swnj snderr(so->so_error); 2754890Swnj if (top) { 2764927Swnj error = (*so->so_proto->pr_usrreq)(so, PRU_SEND, top, asa); 2774890Swnj if (error) { 2784890Swnj splx(s); 2794786Swnj goto release; 2804786Swnj } 2814890Swnj top = 0; 2824890Swnj mp = ⊤ 2834786Swnj } 2844979Swnj if (u.u_count == 0) { 2854979Swnj splx(s); 2864979Swnj goto release; 2874979Swnj } 2885018Swnj space = sbspace(&so->so_snd); 2895018Swnj if (space == 0 || sosendallatonce(so) && space < u.u_count) { 2904890Swnj if (so->so_options & SO_NBIO) 2914890Swnj snderr(EWOULDBLOCK); 2924890Swnj sbunlock(&so->so_snd); 2934890Swnj sbwait(&so->so_snd); 2944890Swnj splx(s); 2954786Swnj goto again; 2964786Swnj } 2974890Swnj splx(s); 2985018Swnj while (u.u_count && space > 0) { 2994890Swnj MGET(m, 1); 3004890Swnj if (m == NULL) { 3014890Swnj error = ENOBUFS; 3024890Swnj m_freem(top); 3034890Swnj goto release; 3044786Swnj } 305*5095Swnj if (u.u_count >= CLBYTES && space >= CLBYTES) { 3064890Swnj register struct mbuf *p; 307*5095Swnj MCLGET(p, 1); 3084890Swnj if (p == 0) 3094890Swnj goto nopages; 3104890Swnj m->m_off = (int)p - (int)m; 311*5095Swnj len = CLBYTES; 3124890Swnj } else { 3134786Swnj nopages: 3144890Swnj m->m_off = MMINOFF; 3154890Swnj len = MIN(MLEN, u.u_count); 3164786Swnj } 3174890Swnj iomove(mtod(m, caddr_t), len, B_WRITE); 3184890Swnj m->m_len = len; 3194890Swnj *mp = m; 3204890Swnj mp = &m->m_next; 3215018Swnj space = sbspace(&so->so_snd); 3224786Swnj } 3234890Swnj s = splnet(); 3244890Swnj goto again; 3254890Swnj 3264786Swnj release: 3274890Swnj sbunlock(&so->so_snd); 3284786Swnj return (error); 3294786Swnj } 3304786Swnj 3314927Swnj soreceive(so, asa) 3324786Swnj register struct socket *so; 3334927Swnj struct sockaddr *asa; 3344786Swnj { 3354786Swnj register struct mbuf *m, *n; 3364916Swnj u_int len; 3374890Swnj int eor, s, error = 0; 3384786Swnj 3394927Swnj COUNT(SORECEIVE); 3404890Swnj restart: 3414890Swnj sblock(&so->so_rcv); 3424890Swnj s = splnet(); 3434890Swnj 3444890Swnj #define rcverr(errno) { error = errno; splx(s); goto release; } 3454786Swnj if (so->so_rcv.sb_cc == 0) { 3464890Swnj if (so->so_state & SS_CANTRCVMORE) { 3474890Swnj splx(s); 3484890Swnj goto release; 3494890Swnj } 3505015Sroot if ((so->so_state & SS_ISCONNECTED) == 0 && 3515015Sroot (so->so_proto->pr_flags & PR_CONNREQUIRED)) 3525015Sroot rcverr(ENOTCONN); 3534890Swnj if (so->so_options & SO_NBIO) 3544916Swnj rcverr (EWOULDBLOCK); 3554890Swnj sbunlock(&so->so_rcv); 3564971Swnj sbwait(&so->so_rcv); 3575012Swnj splx(s); 3584890Swnj goto restart; 3594786Swnj } 3604829Swnj m = so->so_rcv.sb_mb; 3614786Swnj if (m == 0) 3624786Swnj panic("receive"); 3635039Swnj if (so->so_proto->pr_flags & PR_ADDR) { 3645039Swnj if (m->m_len != sizeof (struct sockaddr)) 3655039Swnj panic("soreceive addr"); 3665039Swnj if (asa) 3675039Swnj bcopy(mtod(m, caddr_t), (caddr_t)asa, sizeof (*asa)); 3685039Swnj so->so_rcv.sb_cc -= m->m_len; 3695039Swnj so->so_rcv.sb_mbcnt -= MSIZE; 3705018Swnj m = m_free(m); 3714890Swnj if (m == 0) 3724890Swnj panic("receive 2"); 3735018Swnj so->so_rcv.sb_mb = m; 3744890Swnj } 3754786Swnj eor = 0; 3764786Swnj do { 3774786Swnj len = MIN(m->m_len, u.u_count); 3784786Swnj if (len == m->m_len) { 3794786Swnj eor = (int)m->m_act; 3804890Swnj sbfree(&so->so_rcv, m); 3814786Swnj } 3824786Swnj splx(s); 3834786Swnj iomove(mtod(m, caddr_t), len, B_READ); 3844786Swnj s = splnet(); 3854786Swnj if (len == m->m_len) { 3864786Swnj MFREE(m, n); 3875012Swnj so->so_rcv.sb_mb = n; 3884786Swnj } else { 3894786Swnj m->m_off += len; 3904786Swnj m->m_len -= len; 3914829Swnj so->so_rcv.sb_cc -= len; 3924786Swnj } 3934829Swnj } while ((m = so->so_rcv.sb_mb) && u.u_count && !eor); 3944786Swnj if ((so->so_proto->pr_flags & PR_ATOMIC) && eor == 0) 3954786Swnj do { 3964786Swnj if (m == 0) 3974890Swnj panic("receive 3"); 3984890Swnj sbfree(&so->so_rcv, m); 3994786Swnj eor = (int)m->m_act; 4004786Swnj so->so_rcv.sb_mb = m->m_next; 4014786Swnj MFREE(m, n); 4024890Swnj m = n; 4034786Swnj } while (eor == 0); 4044890Swnj if ((so->so_proto->pr_flags & PR_WANTRCVD) && so->so_pcb) 4054890Swnj (*so->so_proto->pr_usrreq)(so, PRU_RCVD, 0, 0); 4064890Swnj release: 4074916Swnj sbunlock(&so->so_rcv); 4084890Swnj splx(s); 4094916Swnj return (error); 4104786Swnj } 4114786Swnj 4124916Swnj /*ARGSUSED*/ 4134916Swnj soioctl(so, cmd, cmdp) 4144829Swnj register struct socket *so; 4154829Swnj int cmd; 4164829Swnj register caddr_t cmdp; 4174786Swnj { 4184786Swnj 4194927Swnj COUNT(SOIOCTL); 4204829Swnj switch (cmdp) { 4214829Swnj 4224829Swnj } 4234829Swnj switch (so->so_type) { 4244829Swnj 4254829Swnj case SOCK_STREAM: 4264829Swnj break; 4274829Swnj 4284829Swnj case SOCK_DGRAM: 4294829Swnj break; 4304829Swnj 4314829Swnj case SOCK_RDM: 4324829Swnj break; 4334829Swnj 4344829Swnj case SOCK_RAW: 4354829Swnj break; 4364829Swnj 4374829Swnj } 4384786Swnj } 439