1*8549Sroot /* uipc_socket2.c 4.29 82/10/16 */ 24903Swnj 34903Swnj #include "../h/param.h" 44903Swnj #include "../h/systm.h" 54903Swnj #include "../h/dir.h" 64903Swnj #include "../h/user.h" 74903Swnj #include "../h/proc.h" 84903Swnj #include "../h/file.h" 94903Swnj #include "../h/inode.h" 104903Swnj #include "../h/buf.h" 114903Swnj #include "../h/mbuf.h" 124903Swnj #include "../h/protosw.h" 134903Swnj #include "../h/socket.h" 144903Swnj #include "../h/socketvar.h" 154903Swnj 164903Swnj /* 174903Swnj * Primitive routines for operating on sockets and socket buffers 184903Swnj */ 194903Swnj 204903Swnj /* 214903Swnj * Procedures to manipulate state flags of socket 227509Sroot * and do appropriate wakeups. Normal sequence from the 237509Sroot * active (originating) side is that soisconnecting() is 247509Sroot * called during processing of connect() call, 255169Swnj * resulting in an eventual call to soisconnected() if/when the 265169Swnj * connection is established. When the connection is torn down 275169Swnj * soisdisconnecting() is called during processing of disconnect() call, 285169Swnj * and soisdisconnected() is called when the connection to the peer 295169Swnj * is totally severed. The semantics of these routines are such that 305169Swnj * connectionless protocols can call soisconnected() and soisdisconnected() 315169Swnj * only, bypassing the in-progress calls when setting up a ``connection'' 325169Swnj * takes no time. 335169Swnj * 347509Sroot * From the passive side, a socket is created with SO_ACCEPTCONN 357509Sroot * creating two queues of sockets: so_q0 for connections in progress 367509Sroot * and so_q for connections already made and awaiting user acceptance. 377509Sroot * As a protocol is preparing incoming connections, it creates a socket 387509Sroot * structure queued on so_q0 by calling sonewconn(). When the connection 397509Sroot * is established, soisconnected() is called, and transfers the 407509Sroot * socket structure to so_q, making it available to accept(). 417509Sroot * 427509Sroot * If a SO_ACCEPTCONN socket is closed with sockets on either 437509Sroot * so_q0 or so_q, these sockets are dropped. 447509Sroot * 457509Sroot * If and when higher level protocols are implemented in 465169Swnj * the kernel, the wakeups done here will sometimes 475169Swnj * be implemented as software-interrupt process scheduling. 484903Swnj */ 495169Swnj 504903Swnj soisconnecting(so) 514903Swnj struct socket *so; 524903Swnj { 534903Swnj 544903Swnj so->so_state &= ~(SS_ISCONNECTED|SS_ISDISCONNECTING); 554903Swnj so->so_state |= SS_ISCONNECTING; 564903Swnj wakeup((caddr_t)&so->so_timeo); 574903Swnj } 584903Swnj 594903Swnj soisconnected(so) 604903Swnj struct socket *so; 614903Swnj { 627509Sroot register struct socket *head = so->so_head; 634903Swnj 647509Sroot if (head) { 657509Sroot if (soqremque(so, 0) == 0) 667509Sroot panic("soisconnected"); 677509Sroot soqinsque(head, so, 1); 687509Sroot wakeup((caddr_t)&head->so_timeo); 697509Sroot } 704903Swnj so->so_state &= ~(SS_ISCONNECTING|SS_ISDISCONNECTING); 714903Swnj so->so_state |= SS_ISCONNECTED; 724903Swnj wakeup((caddr_t)&so->so_timeo); 735578Swnj sorwakeup(so); 745578Swnj sowwakeup(so); 754903Swnj } 764903Swnj 774903Swnj soisdisconnecting(so) 784903Swnj struct socket *so; 794903Swnj { 804903Swnj 815248Sroot so->so_state &= ~SS_ISCONNECTING; 824903Swnj so->so_state |= (SS_ISDISCONNECTING|SS_CANTRCVMORE|SS_CANTSENDMORE); 834903Swnj wakeup((caddr_t)&so->so_timeo); 845170Swnj sowwakeup(so); 855169Swnj sorwakeup(so); 864903Swnj } 874903Swnj 884903Swnj soisdisconnected(so) 894903Swnj struct socket *so; 904903Swnj { 914903Swnj 924903Swnj so->so_state &= ~(SS_ISCONNECTING|SS_ISCONNECTED|SS_ISDISCONNECTING); 934903Swnj so->so_state |= (SS_CANTRCVMORE|SS_CANTSENDMORE); 944903Swnj wakeup((caddr_t)&so->so_timeo); 954903Swnj sowwakeup(so); 964903Swnj sorwakeup(so); 974903Swnj } 984903Swnj 995169Swnj /* 1007509Sroot * When an attempt at a new connection is noted on a socket 1017509Sroot * which accepts connections, sonewconn is called. If the 1027509Sroot * connection is possible (subject to space constraints, etc.) 1037509Sroot * then we allocate a new structure, propoerly linked into the 1047509Sroot * data structure of the original socket, and return this. 1057509Sroot */ 1067509Sroot struct socket * 1077509Sroot sonewconn(head) 1087509Sroot register struct socket *head; 1097509Sroot { 1107509Sroot register struct socket *so; 1117509Sroot struct mbuf *m; 1127509Sroot 1137509Sroot if (head->so_qlen + head->so_q0len > 3 * head->so_qlimit / 2) 1147509Sroot goto bad; 1157509Sroot m = m_getclr(M_DONTWAIT); 1167509Sroot if (m == 0) 1177509Sroot goto bad; 1187509Sroot so = mtod(m, struct socket *); 1197509Sroot so->so_type = head->so_type; 1207509Sroot so->so_options = head->so_options &~ SO_ACCEPTCONN; 1217509Sroot so->so_linger = head->so_linger; 1227509Sroot so->so_state = head->so_state; 1237509Sroot so->so_proto = head->so_proto; 1247509Sroot so->so_timeo = head->so_timeo; 1257509Sroot so->so_pgrp = head->so_pgrp; 1267509Sroot soqinsque(head, so, 0); 1278301Sroot if ((*so->so_proto->pr_usrreq)(so, PRU_ATTACH, 0, 0, 0)) { 1287509Sroot (void) soqremque(so, 0); 1297509Sroot m_free(m); 1307509Sroot goto bad; 1317509Sroot } 1327509Sroot return (so); 1337509Sroot bad: 1347509Sroot return ((struct socket *)0); 1357509Sroot } 1367509Sroot 1377509Sroot soqinsque(head, so, q) 1387509Sroot register struct socket *head, *so; 1397509Sroot int q; 1407509Sroot { 1417509Sroot 1427509Sroot so->so_head = head; 1437509Sroot if (q == 0) { 1447509Sroot head->so_q0len++; 1457509Sroot so->so_q0 = head->so_q0; 1467509Sroot head->so_q0 = so; 1477509Sroot } else { 1487509Sroot head->so_qlen++; 1497509Sroot so->so_q = head->so_q; 1507509Sroot head->so_q = so; 1517509Sroot } 1527509Sroot } 1537509Sroot 1547509Sroot soqremque(so, q) 1557509Sroot register struct socket *so; 1567509Sroot int q; 1577509Sroot { 1587509Sroot register struct socket *head, *prev, *next; 1597509Sroot 1607509Sroot head = so->so_head; 1617509Sroot prev = head; 1627509Sroot for (;;) { 1637509Sroot next = q ? prev->so_q : prev->so_q0; 1647509Sroot if (next == so) 1657509Sroot break; 1667509Sroot if (next == head) 1677509Sroot return (0); 1687509Sroot prev = next; 1697509Sroot } 1707509Sroot if (q == 0) { 1717509Sroot prev->so_q0 = next->so_q0; 1727509Sroot head->so_q0len--; 1737509Sroot } else { 1747509Sroot prev->so_q = next->so_q; 1757509Sroot head->so_qlen--; 1767509Sroot } 1777509Sroot next->so_q0 = next->so_q = 0; 1787509Sroot next->so_head = 0; 1797509Sroot return (1); 1807509Sroot } 1817509Sroot 1827509Sroot /* 1835169Swnj * Socantsendmore indicates that no more data will be sent on the 1845169Swnj * socket; it would normally be applied to a socket when the user 1855169Swnj * informs the system that no more data is to be sent, by the protocol 1865169Swnj * code (in case PRU_SHUTDOWN). Socantrcvmore indicates that no more data 1875169Swnj * will be received, and will normally be applied to the socket by a 1885169Swnj * protocol when it detects that the peer will send no more data. 1895169Swnj * Data queued for reading in the socket may yet be read. 1905169Swnj */ 1915169Swnj 1924917Swnj socantsendmore(so) 1934917Swnj struct socket *so; 1944917Swnj { 1954917Swnj 1964917Swnj so->so_state |= SS_CANTSENDMORE; 1974917Swnj sowwakeup(so); 1984917Swnj } 1994917Swnj 2004917Swnj socantrcvmore(so) 2014917Swnj struct socket *so; 2024917Swnj { 2034917Swnj 2044917Swnj so->so_state |= SS_CANTRCVMORE; 2054917Swnj sorwakeup(so); 2064917Swnj } 2074917Swnj 2084903Swnj /* 2095169Swnj * Socket select/wakeup routines. 2104903Swnj */ 2115169Swnj 2125169Swnj /* 2135169Swnj * Interface routine to select() system 2145169Swnj * call for sockets. 2155169Swnj */ 2165577Swnj soselect(so, rw) 2174903Swnj register struct socket *so; 2185577Swnj int rw; 2194903Swnj { 2205578Swnj int s = splnet(); 2214903Swnj 2225577Swnj switch (rw) { 2235577Swnj 2245577Swnj case FREAD: 2255578Swnj if (soreadable(so)) { 2265578Swnj splx(s); 2274903Swnj return (1); 2285578Swnj } 2294903Swnj sbselqueue(&so->so_rcv); 2305577Swnj break; 2315577Swnj 2325577Swnj case FWRITE: 2335578Swnj if (sowriteable(so)) { 2345578Swnj splx(s); 2354903Swnj return (1); 2365578Swnj } 2374903Swnj sbselqueue(&so->so_snd); 2385577Swnj break; 2394903Swnj } 2405578Swnj splx(s); 2414903Swnj return (0); 2424903Swnj } 2434903Swnj 2444903Swnj /* 2454903Swnj * Queue a process for a select on a socket buffer. 2464903Swnj */ 2474903Swnj sbselqueue(sb) 2484903Swnj struct sockbuf *sb; 2494903Swnj { 2504903Swnj register struct proc *p; 2514903Swnj 2524917Swnj if ((p = sb->sb_sel) && p->p_wchan == (caddr_t)&selwait) 2534903Swnj sb->sb_flags |= SB_COLL; 2544903Swnj else 2554903Swnj sb->sb_sel = u.u_procp; 2564903Swnj } 2574903Swnj 2584903Swnj /* 2594917Swnj * Wait for data to arrive at/drain from a socket buffer. 2604917Swnj */ 2614917Swnj sbwait(sb) 2624917Swnj struct sockbuf *sb; 2634917Swnj { 2644917Swnj 2654917Swnj sb->sb_flags |= SB_WAIT; 2664917Swnj sleep((caddr_t)&sb->sb_cc, PZERO+1); 2674917Swnj } 2684917Swnj 2694917Swnj /* 2704903Swnj * Wakeup processes waiting on a socket buffer. 2714903Swnj */ 2724903Swnj sbwakeup(sb) 2734903Swnj struct sockbuf *sb; 2744903Swnj { 2754903Swnj 2764903Swnj if (sb->sb_sel) { 2774903Swnj selwakeup(sb->sb_sel, sb->sb_flags & SB_COLL); 2784903Swnj sb->sb_sel = 0; 2794903Swnj sb->sb_flags &= ~SB_COLL; 2804903Swnj } 2814903Swnj if (sb->sb_flags & SB_WAIT) { 2824903Swnj sb->sb_flags &= ~SB_WAIT; 2835013Swnj wakeup((caddr_t)&sb->sb_cc); 2844903Swnj } 2854903Swnj } 2864903Swnj 2874903Swnj /* 2885169Swnj * Socket buffer (struct sockbuf) utility routines. 2895169Swnj * 2905169Swnj * Each socket contains two socket buffers: one for sending data and 2915169Swnj * one for receiving data. Each buffer contains a queue of mbufs, 2925169Swnj * information about the number of mbufs and amount of data in the 2935169Swnj * queue, and other fields allowing select() statements and notification 2945169Swnj * on data availability to be implemented. 2955169Swnj * 2965169Swnj * Before using a new socket structure it is first necessary to reserve 2975169Swnj * buffer space to the socket, by calling sbreserve. This commits 2985169Swnj * some of the available buffer space in the system buffer pool for the 2995169Swnj * socket. The space should be released by calling sbrelease when the 3005169Swnj * socket is destroyed. 3015169Swnj * 3025169Swnj * The routine sbappend() is normally called to append new mbufs 3035169Swnj * to a socket buffer, after checking that adequate space is available 3045169Swnj * comparing the function spspace() with the amount of data to be added. 3055169Swnj * Data is normally removed from a socket buffer in a protocol by 3065169Swnj * first calling m_copy on the socket buffer mbuf chain and sending this 3075169Swnj * to a peer, and then removing the data from the socket buffer with 3085169Swnj * sbdrop when the data is acknowledged by the peer (or immediately 3095170Swnj * in the case of unreliable protocols.) 3105169Swnj * 3115169Swnj * Protocols which do not require connections place both source address 3125169Swnj * and data information in socket buffer queues. The source addresses 3135169Swnj * are stored in single mbufs after each data item, and are easily found 3145169Swnj * as the data items are all marked with end of record markers. The 3155169Swnj * sbappendaddr() routine stores a datum and associated address in 3165169Swnj * a socket buffer. Note that, unlike sbappend(), this routine checks 3175169Swnj * for the caller that there will be enough space to store the data. 3185169Swnj * It fails if there is not enough space, or if it cannot find 3195169Swnj * a mbuf to store the address in. 3205169Swnj * 3215169Swnj * The higher-level routines sosend and soreceive (in socket.c) 3225170Swnj * also add data to, and remove data from socket buffers repectively. 3235169Swnj */ 3245169Swnj 3255169Swnj /* 3264903Swnj * Allot mbufs to a sockbuf. 3274903Swnj */ 3284903Swnj sbreserve(sb, cc) 3294903Swnj struct sockbuf *sb; 3304903Swnj { 3314903Swnj 3327181Swnj /* someday maybe this routine will fail... */ 3334980Swnj sb->sb_hiwat = cc; 3345042Swnj sb->sb_mbmax = cc*2; 3354917Swnj return (1); 3364903Swnj } 3374903Swnj 3384903Swnj /* 3394903Swnj * Free mbufs held by a socket, and reserved mbuf space. 3404903Swnj */ 3414903Swnj sbrelease(sb) 3424903Swnj struct sockbuf *sb; 3434903Swnj { 3444903Swnj 3454903Swnj sbflush(sb); 3464980Swnj sb->sb_hiwat = sb->sb_mbmax = 0; 3474903Swnj } 3484903Swnj 3494903Swnj /* 3504903Swnj * Routines to add (at the end) and remove (from the beginning) 3514903Swnj * data from a mbuf queue. 3524903Swnj */ 3534903Swnj 3544903Swnj /* 3554903Swnj * Append mbuf queue m to sockbuf sb. 3564903Swnj */ 3574903Swnj sbappend(sb, m) 3584903Swnj register struct mbuf *m; 3594903Swnj register struct sockbuf *sb; 3604903Swnj { 3616092Sroot register struct mbuf *n; 3624903Swnj 363*8549Sroot SBCHECK(sb, "sbappend begin"); 364*8549Sroot #ifdef notdef 365*8549Sroot { struct mbuf *p; 366*8549Sroot printf("sba: "); 367*8549Sroot for (p = sb->sb_mb; p; p = p->m_next) printf("%x:(%x,%d) ",p,p->m_off,p->m_len); 368*8549Sroot printf("+= "); 369*8549Sroot for (p = m; p; p = p->m_next) printf("%x:(%x,%d) ",p,p->m_off,p->m_len); 370*8549Sroot printf("\n"); 371*8549Sroot } 372*8549Sroot #endif 3736092Sroot n = sb->sb_mb; 3746092Sroot if (n) 3756092Sroot while (n->m_next) 3766092Sroot n = n->m_next; 3774903Swnj while (m) { 3785266Swnj if (m->m_len == 0 && (int)m->m_act == 0) { 3795304Sroot m = m_free(m); 3805266Swnj continue; 3815266Swnj } 3824903Swnj if (n && n->m_off <= MMAXOFF && m->m_off <= MMAXOFF && 3834903Swnj (int)n->m_act == 0 && (int)m->m_act == 0 && 3845042Swnj (n->m_off + n->m_len + m->m_len) <= MMAXOFF) { 3855042Swnj bcopy(mtod(m, caddr_t), mtod(n, caddr_t) + n->m_len, 3864917Swnj (unsigned)m->m_len); 3874903Swnj n->m_len += m->m_len; 3884903Swnj sb->sb_cc += m->m_len; 3894903Swnj m = m_free(m); 3904903Swnj continue; 3914903Swnj } 3924903Swnj sballoc(sb, m); 3936092Sroot if (n == 0) 3946092Sroot sb->sb_mb = m; 3956092Sroot else 3966092Sroot n->m_next = m; 3974903Swnj n = m; 3984903Swnj m = m->m_next; 3996092Sroot n->m_next = 0; 4004903Swnj } 401*8549Sroot #ifdef notdef 402*8549Sroot { struct mbuf *p; 403*8549Sroot printf("res: "); 404*8549Sroot for (p = sb->sb_mb; p; p = p->m_next) printf("%x:(%x,%d) ",p,p->m_off,p->m_len); 405*8549Sroot printf("+= "); 406*8549Sroot for (p = m; p; p = p->m_next) printf("%x:(%x,%d) ",p,p->m_off,p->m_len); 407*8549Sroot printf("\n"); 4084903Swnj } 409*8549Sroot #endif 410*8549Sroot SBCHECK(sb, "sbappend end"); 411*8549Sroot } 4124903Swnj 4135169Swnj /* 4145169Swnj * Append data and address. 4155169Swnj * Return 0 if no space in sockbuf or if 4165169Swnj * can't get mbuf to stuff address in. 4175169Swnj */ 4184928Swnj sbappendaddr(sb, asa, m0) 4194928Swnj struct sockbuf *sb; 4204928Swnj struct sockaddr *asa; 4214928Swnj struct mbuf *m0; 4224928Swnj { 4234928Swnj struct sockaddr *msa; 4244928Swnj register struct mbuf *m; 4254928Swnj register int len = sizeof (struct sockaddr); 4264928Swnj 427*8549Sroot SBCHECK(sb, "sbappendaddr begin"); 4285042Swnj m = m0; 4295042Swnj if (m == 0) 4305042Swnj panic("sbappendaddr"); 4315042Swnj for (;;) { 4324928Swnj len += m->m_len; 4335042Swnj if (m->m_next == 0) { 4345042Swnj m->m_act = (struct mbuf *)1; 4355042Swnj break; 4365042Swnj } 4375042Swnj m = m->m_next; 4385042Swnj } 4395043Swnj if (len > sbspace(sb)) 4404928Swnj return (0); 4415586Sroot m = m_get(M_DONTWAIT); 4425043Swnj if (m == 0) 4434928Swnj return (0); 4444928Swnj m->m_len = sizeof (struct sockaddr); 4454928Swnj msa = mtod(m, struct sockaddr *); 4464928Swnj *msa = *asa; 4474928Swnj m->m_act = (struct mbuf *)1; 4484928Swnj sbappend(sb, m); 4494928Swnj sbappend(sb, m0); 450*8549Sroot SBCHECK(sb, "sbappendaddr end"); 4514928Swnj return (1); 4524928Swnj } 4534928Swnj 454*8549Sroot SBCHECK(sb, str) 455*8549Sroot struct sockbuf *sb; 456*8549Sroot char *str; 457*8549Sroot { 458*8549Sroot register int cnt = sb->sb_cc; 459*8549Sroot register int mbcnt = sb->sb_mbcnt; 460*8549Sroot register struct mbuf *m; 461*8549Sroot 462*8549Sroot for (m = sb->sb_mb; m; m = m->m_next) { 463*8549Sroot cnt -= m->m_len; 464*8549Sroot mbcnt -= MSIZE; 465*8549Sroot if (m->m_off > MMAXOFF) 466*8549Sroot mbcnt -= CLBYTES; 467*8549Sroot } 468*8549Sroot if (cnt || mbcnt) { 469*8549Sroot printf("cnt %d mbcnt %d\n", cnt, mbcnt); 470*8549Sroot panic(str); 471*8549Sroot } 472*8549Sroot } 473*8549Sroot 4744903Swnj /* 4754903Swnj * Free all mbufs on a sockbuf mbuf chain. 4764903Swnj * Check that resource allocations return to 0. 4774903Swnj */ 4784903Swnj sbflush(sb) 4794903Swnj struct sockbuf *sb; 4804903Swnj { 4814903Swnj 4824903Swnj if (sb->sb_flags & SB_LOCK) 4834903Swnj panic("sbflush"); 4845266Swnj if (sb->sb_cc) 4855266Swnj sbdrop(sb, sb->sb_cc); 4864903Swnj if (sb->sb_cc || sb->sb_mbcnt || sb->sb_mb) 4874903Swnj panic("sbflush 2"); 4884903Swnj } 4894903Swnj 4904903Swnj /* 4914903Swnj * Drop data from (the front of) a sockbuf chain. 4924903Swnj */ 4934903Swnj sbdrop(sb, len) 4944903Swnj register struct sockbuf *sb; 4954903Swnj register int len; 4964903Swnj { 4974903Swnj register struct mbuf *m = sb->sb_mb, *mn; 4984903Swnj 4994903Swnj while (len > 0) { 5004903Swnj if (m == 0) 5014903Swnj panic("sbdrop"); 5025064Swnj if (m->m_len > len) { 5034903Swnj m->m_len -= len; 5044903Swnj m->m_off += len; 5054903Swnj sb->sb_cc -= len; 5064903Swnj break; 5074903Swnj } 5085064Swnj len -= m->m_len; 5095064Swnj sbfree(sb, m); 5105064Swnj MFREE(m, mn); 5115064Swnj m = mn; 5124903Swnj } 5134903Swnj sb->sb_mb = m; 5144903Swnj } 515