1*12758Ssam /* uipc_socket2.c 4.37 83/05/27 */ 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 * 34*12758Ssam * From the passive side, a socket is created with 35*12758Ssam * 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 * 42*12758Ssam * If a socket is closed with sockets on either 437509Sroot * so_q0 or so_q, these sockets are dropped. 447509Sroot * 45*12758Ssam * If higher level protocols are implemented in 465169Swnj * the kernel, the wakeups done here will sometimes 47*12758Ssam * cause software-interrupt process scheduling. 484903Swnj */ 495169Swnj 504903Swnj soisconnecting(so) 51*12758Ssam register 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) 60*12758Ssam register 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); 68*12758Ssam sorwakeup(head); 697509Sroot wakeup((caddr_t)&head->so_timeo); 707509Sroot } 714903Swnj so->so_state &= ~(SS_ISCONNECTING|SS_ISDISCONNECTING); 724903Swnj so->so_state |= SS_ISCONNECTED; 734903Swnj wakeup((caddr_t)&so->so_timeo); 745578Swnj sorwakeup(so); 755578Swnj sowwakeup(so); 764903Swnj } 774903Swnj 784903Swnj soisdisconnecting(so) 79*12758Ssam register struct socket *so; 804903Swnj { 814903Swnj 825248Sroot so->so_state &= ~SS_ISCONNECTING; 834903Swnj so->so_state |= (SS_ISDISCONNECTING|SS_CANTRCVMORE|SS_CANTSENDMORE); 844903Swnj wakeup((caddr_t)&so->so_timeo); 855170Swnj sowwakeup(so); 865169Swnj sorwakeup(so); 874903Swnj } 884903Swnj 894903Swnj soisdisconnected(so) 90*12758Ssam register struct socket *so; 914903Swnj { 924903Swnj 934903Swnj so->so_state &= ~(SS_ISCONNECTING|SS_ISCONNECTED|SS_ISDISCONNECTING); 944903Swnj so->so_state |= (SS_CANTRCVMORE|SS_CANTSENDMORE); 954903Swnj wakeup((caddr_t)&so->so_timeo); 964903Swnj sowwakeup(so); 974903Swnj sorwakeup(so); 984903Swnj } 994903Swnj 1005169Swnj /* 1017509Sroot * When an attempt at a new connection is noted on a socket 1027509Sroot * which accepts connections, sonewconn is called. If the 1037509Sroot * connection is possible (subject to space constraints, etc.) 1047509Sroot * then we allocate a new structure, propoerly linked into the 1057509Sroot * data structure of the original socket, and return this. 1067509Sroot */ 1077509Sroot struct socket * 1087509Sroot sonewconn(head) 1097509Sroot register struct socket *head; 1107509Sroot { 1117509Sroot register struct socket *so; 112*12758Ssam register struct mbuf *m; 1137509Sroot 1147509Sroot if (head->so_qlen + head->so_q0len > 3 * head->so_qlimit / 2) 1157509Sroot goto bad; 1169636Ssam m = m_getclr(M_DONTWAIT, MT_SOCKET); 11710138Ssam if (m == NULL) 1187509Sroot goto bad; 1197509Sroot so = mtod(m, struct socket *); 1207509Sroot so->so_type = head->so_type; 1217509Sroot so->so_options = head->so_options &~ SO_ACCEPTCONN; 1227509Sroot so->so_linger = head->so_linger; 12310204Ssam so->so_state = head->so_state | SS_NOFDREF; 1247509Sroot so->so_proto = head->so_proto; 1257509Sroot so->so_timeo = head->so_timeo; 1267509Sroot so->so_pgrp = head->so_pgrp; 1277509Sroot soqinsque(head, so, 0); 128*12758Ssam if ((*so->so_proto->pr_usrreq)(so, PRU_ATTACH, 129*12758Ssam (struct mbuf *)0, (struct mbuf *)0, (struct mbuf *)0)) { 1307509Sroot (void) soqremque(so, 0); 1318818Sroot (void) m_free(m); 1327509Sroot goto bad; 1337509Sroot } 1347509Sroot return (so); 1357509Sroot bad: 1367509Sroot return ((struct socket *)0); 1377509Sroot } 1387509Sroot 1397509Sroot soqinsque(head, so, q) 1407509Sroot register struct socket *head, *so; 1417509Sroot int q; 1427509Sroot { 1437509Sroot 1447509Sroot so->so_head = head; 1457509Sroot if (q == 0) { 1467509Sroot head->so_q0len++; 1477509Sroot so->so_q0 = head->so_q0; 1487509Sroot head->so_q0 = so; 1497509Sroot } else { 1507509Sroot head->so_qlen++; 1517509Sroot so->so_q = head->so_q; 1527509Sroot head->so_q = so; 1537509Sroot } 1547509Sroot } 1557509Sroot 1567509Sroot soqremque(so, q) 1577509Sroot register struct socket *so; 1587509Sroot int q; 1597509Sroot { 1607509Sroot register struct socket *head, *prev, *next; 1617509Sroot 1627509Sroot head = so->so_head; 1637509Sroot prev = head; 1647509Sroot for (;;) { 1657509Sroot next = q ? prev->so_q : prev->so_q0; 1667509Sroot if (next == so) 1677509Sroot break; 1687509Sroot if (next == head) 1697509Sroot return (0); 1707509Sroot prev = next; 1717509Sroot } 1727509Sroot if (q == 0) { 1737509Sroot prev->so_q0 = next->so_q0; 1747509Sroot head->so_q0len--; 1757509Sroot } else { 1767509Sroot prev->so_q = next->so_q; 1777509Sroot head->so_qlen--; 1787509Sroot } 1797509Sroot next->so_q0 = next->so_q = 0; 1807509Sroot next->so_head = 0; 1817509Sroot return (1); 1827509Sroot } 1837509Sroot 1847509Sroot /* 1855169Swnj * Socantsendmore indicates that no more data will be sent on the 1865169Swnj * socket; it would normally be applied to a socket when the user 1875169Swnj * informs the system that no more data is to be sent, by the protocol 1885169Swnj * code (in case PRU_SHUTDOWN). Socantrcvmore indicates that no more data 1895169Swnj * will be received, and will normally be applied to the socket by a 1905169Swnj * protocol when it detects that the peer will send no more data. 1915169Swnj * Data queued for reading in the socket may yet be read. 1925169Swnj */ 1935169Swnj 1944917Swnj socantsendmore(so) 1954917Swnj struct socket *so; 1964917Swnj { 1974917Swnj 1984917Swnj so->so_state |= SS_CANTSENDMORE; 1994917Swnj sowwakeup(so); 2004917Swnj } 2014917Swnj 2024917Swnj socantrcvmore(so) 2034917Swnj struct socket *so; 2044917Swnj { 2054917Swnj 2064917Swnj so->so_state |= SS_CANTRCVMORE; 2074917Swnj sorwakeup(so); 2084917Swnj } 2094917Swnj 2104903Swnj /* 2115169Swnj * Socket select/wakeup routines. 2124903Swnj */ 2135169Swnj 2145169Swnj /* 2154903Swnj * Queue a process for a select on a socket buffer. 2164903Swnj */ 2174903Swnj sbselqueue(sb) 2184903Swnj struct sockbuf *sb; 2194903Swnj { 2204903Swnj register struct proc *p; 2214903Swnj 2224917Swnj if ((p = sb->sb_sel) && p->p_wchan == (caddr_t)&selwait) 2234903Swnj sb->sb_flags |= SB_COLL; 2244903Swnj else 2254903Swnj sb->sb_sel = u.u_procp; 2264903Swnj } 2274903Swnj 2284903Swnj /* 2294917Swnj * Wait for data to arrive at/drain from a socket buffer. 2304917Swnj */ 2314917Swnj sbwait(sb) 2324917Swnj struct sockbuf *sb; 2334917Swnj { 2344917Swnj 2354917Swnj sb->sb_flags |= SB_WAIT; 2364917Swnj sleep((caddr_t)&sb->sb_cc, PZERO+1); 2374917Swnj } 2384917Swnj 2394917Swnj /* 2404903Swnj * Wakeup processes waiting on a socket buffer. 2414903Swnj */ 2424903Swnj sbwakeup(sb) 243*12758Ssam register struct sockbuf *sb; 2444903Swnj { 2454903Swnj 2464903Swnj if (sb->sb_sel) { 2474903Swnj selwakeup(sb->sb_sel, sb->sb_flags & SB_COLL); 2484903Swnj sb->sb_sel = 0; 2494903Swnj sb->sb_flags &= ~SB_COLL; 2504903Swnj } 2514903Swnj if (sb->sb_flags & SB_WAIT) { 2524903Swnj sb->sb_flags &= ~SB_WAIT; 2535013Swnj wakeup((caddr_t)&sb->sb_cc); 2544903Swnj } 2554903Swnj } 2564903Swnj 2574903Swnj /* 2585169Swnj * Socket buffer (struct sockbuf) utility routines. 2595169Swnj * 2605169Swnj * Each socket contains two socket buffers: one for sending data and 2615169Swnj * one for receiving data. Each buffer contains a queue of mbufs, 2625169Swnj * information about the number of mbufs and amount of data in the 2635169Swnj * queue, and other fields allowing select() statements and notification 2645169Swnj * on data availability to be implemented. 2655169Swnj * 2665169Swnj * Before using a new socket structure it is first necessary to reserve 2675169Swnj * buffer space to the socket, by calling sbreserve. This commits 2685169Swnj * some of the available buffer space in the system buffer pool for the 2695169Swnj * socket. The space should be released by calling sbrelease when the 2705169Swnj * socket is destroyed. 2715169Swnj * 2725169Swnj * The routine sbappend() is normally called to append new mbufs 2735169Swnj * to a socket buffer, after checking that adequate space is available 2745169Swnj * comparing the function spspace() with the amount of data to be added. 2755169Swnj * Data is normally removed from a socket buffer in a protocol by 2765169Swnj * first calling m_copy on the socket buffer mbuf chain and sending this 2775169Swnj * to a peer, and then removing the data from the socket buffer with 2785169Swnj * sbdrop when the data is acknowledged by the peer (or immediately 2795170Swnj * in the case of unreliable protocols.) 2805169Swnj * 2815169Swnj * Protocols which do not require connections place both source address 2825169Swnj * and data information in socket buffer queues. The source addresses 2835169Swnj * are stored in single mbufs after each data item, and are easily found 2845169Swnj * as the data items are all marked with end of record markers. The 2855169Swnj * sbappendaddr() routine stores a datum and associated address in 2865169Swnj * a socket buffer. Note that, unlike sbappend(), this routine checks 2875169Swnj * for the caller that there will be enough space to store the data. 2885169Swnj * It fails if there is not enough space, or if it cannot find 2895169Swnj * a mbuf to store the address in. 2905169Swnj * 2915169Swnj * The higher-level routines sosend and soreceive (in socket.c) 2925170Swnj * also add data to, and remove data from socket buffers repectively. 2935169Swnj */ 2945169Swnj 2959027Sroot soreserve(so, sndcc, rcvcc) 296*12758Ssam register struct socket *so; 2979027Sroot int sndcc, rcvcc; 2989027Sroot { 2999027Sroot 3009027Sroot if (sbreserve(&so->so_snd, sndcc) == 0) 3019027Sroot goto bad; 3029027Sroot if (sbreserve(&so->so_rcv, rcvcc) == 0) 3039027Sroot goto bad2; 3049027Sroot return (0); 3059027Sroot bad2: 3069027Sroot sbrelease(&so->so_snd); 3079027Sroot bad: 3089027Sroot return (ENOBUFS); 3099027Sroot } 3109027Sroot 3115169Swnj /* 3124903Swnj * Allot mbufs to a sockbuf. 3134903Swnj */ 3144903Swnj sbreserve(sb, cc) 3154903Swnj struct sockbuf *sb; 3164903Swnj { 3174903Swnj 3187181Swnj /* someday maybe this routine will fail... */ 3194980Swnj sb->sb_hiwat = cc; 320*12758Ssam /* * 2 implies names can be no more than 1 mbuf each */ 321*12758Ssam sb->sb_mbmax = cc<<1; 3224917Swnj return (1); 3234903Swnj } 3244903Swnj 3254903Swnj /* 3264903Swnj * Free mbufs held by a socket, and reserved mbuf space. 3274903Swnj */ 3284903Swnj sbrelease(sb) 3294903Swnj struct sockbuf *sb; 3304903Swnj { 3314903Swnj 3324903Swnj sbflush(sb); 3334980Swnj sb->sb_hiwat = sb->sb_mbmax = 0; 3344903Swnj } 3354903Swnj 3364903Swnj /* 3374903Swnj * Routines to add (at the end) and remove (from the beginning) 3384903Swnj * data from a mbuf queue. 3394903Swnj */ 3404903Swnj 3414903Swnj /* 3424903Swnj * Append mbuf queue m to sockbuf sb. 3434903Swnj */ 3444903Swnj sbappend(sb, m) 3454903Swnj register struct mbuf *m; 3464903Swnj register struct sockbuf *sb; 3474903Swnj { 3486092Sroot register struct mbuf *n; 3494903Swnj 3506092Sroot n = sb->sb_mb; 3516092Sroot if (n) 3526092Sroot while (n->m_next) 3536092Sroot n = n->m_next; 3544903Swnj while (m) { 3555266Swnj if (m->m_len == 0 && (int)m->m_act == 0) { 3565304Sroot m = m_free(m); 3575266Swnj continue; 3585266Swnj } 3594903Swnj if (n && n->m_off <= MMAXOFF && m->m_off <= MMAXOFF && 3604903Swnj (int)n->m_act == 0 && (int)m->m_act == 0 && 3615042Swnj (n->m_off + n->m_len + m->m_len) <= MMAXOFF) { 3625042Swnj bcopy(mtod(m, caddr_t), mtod(n, caddr_t) + n->m_len, 3634917Swnj (unsigned)m->m_len); 3644903Swnj n->m_len += m->m_len; 3654903Swnj sb->sb_cc += m->m_len; 3664903Swnj m = m_free(m); 3674903Swnj continue; 3684903Swnj } 3694903Swnj sballoc(sb, m); 3706092Sroot if (n == 0) 3716092Sroot sb->sb_mb = m; 3726092Sroot else 3736092Sroot n->m_next = m; 3744903Swnj n = m; 3754903Swnj m = m->m_next; 3766092Sroot n->m_next = 0; 3774903Swnj } 3784903Swnj } 3794903Swnj 3805169Swnj /* 3815169Swnj * Append data and address. 3825169Swnj * Return 0 if no space in sockbuf or if 3835169Swnj * can't get mbuf to stuff address in. 3845169Swnj */ 385*12758Ssam sbappendaddr(sb, asa, m0, rights0) 3864928Swnj struct sockbuf *sb; 3874928Swnj struct sockaddr *asa; 388*12758Ssam struct mbuf *m0, *rights0; 3894928Swnj { 3904928Swnj register struct mbuf *m; 3914928Swnj register int len = sizeof (struct sockaddr); 392*12758Ssam register struct mbuf *rights; 3934928Swnj 394*12758Ssam if (rights0) 395*12758Ssam len += rights0->m_len; 3965042Swnj m = m0; 3975042Swnj if (m == 0) 3985042Swnj panic("sbappendaddr"); 3995042Swnj for (;;) { 4004928Swnj len += m->m_len; 4015042Swnj if (m->m_next == 0) { 4025042Swnj m->m_act = (struct mbuf *)1; 4035042Swnj break; 4045042Swnj } 4055042Swnj m = m->m_next; 4065042Swnj } 4075043Swnj if (len > sbspace(sb)) 4084928Swnj return (0); 4099636Ssam m = m_get(M_DONTWAIT, MT_SONAME); 410*12758Ssam if (m == NULL) 4114928Swnj return (0); 4124928Swnj m->m_len = sizeof (struct sockaddr); 4134928Swnj m->m_act = (struct mbuf *)1; 414*12758Ssam *mtod(m, struct sockaddr *) = *asa; 415*12758Ssam if (rights0 == 0 || rights0->m_len == 0) { 416*12758Ssam rights = m_get(M_DONTWAIT, MT_SONAME); 417*12758Ssam if (rights) 418*12758Ssam rights->m_len = 0; 419*12758Ssam } else 420*12758Ssam rights = m_copy(rights0, 0, rights0->m_len); 421*12758Ssam if (rights == 0) { 422*12758Ssam m_freem(m); 423*12758Ssam return (0); 424*12758Ssam } 425*12758Ssam rights->m_act = (struct mbuf *)1; 426*12758Ssam m->m_next = rights; 427*12758Ssam rights->m_next = m0; 4284928Swnj sbappend(sb, m); 4294928Swnj return (1); 4304928Swnj } 4314928Swnj 4324903Swnj /* 4334903Swnj * Free all mbufs on a sockbuf mbuf chain. 4344903Swnj * Check that resource allocations return to 0. 4354903Swnj */ 4364903Swnj sbflush(sb) 437*12758Ssam register struct sockbuf *sb; 4384903Swnj { 4394903Swnj 4404903Swnj if (sb->sb_flags & SB_LOCK) 4414903Swnj panic("sbflush"); 4425266Swnj if (sb->sb_cc) 4435266Swnj sbdrop(sb, sb->sb_cc); 4444903Swnj if (sb->sb_cc || sb->sb_mbcnt || sb->sb_mb) 4454903Swnj panic("sbflush 2"); 4464903Swnj } 4474903Swnj 4484903Swnj /* 4494903Swnj * Drop data from (the front of) a sockbuf chain. 4504903Swnj */ 4514903Swnj sbdrop(sb, len) 4524903Swnj register struct sockbuf *sb; 4534903Swnj register int len; 4544903Swnj { 4554903Swnj register struct mbuf *m = sb->sb_mb, *mn; 4564903Swnj 4574903Swnj while (len > 0) { 4584903Swnj if (m == 0) 4594903Swnj panic("sbdrop"); 4605064Swnj if (m->m_len > len) { 4614903Swnj m->m_len -= len; 4624903Swnj m->m_off += len; 4634903Swnj sb->sb_cc -= len; 4644903Swnj break; 4654903Swnj } 4665064Swnj len -= m->m_len; 4675064Swnj sbfree(sb, m); 4685064Swnj MFREE(m, mn); 4695064Swnj m = mn; 4704903Swnj } 4714903Swnj sb->sb_mb = m; 4724903Swnj } 473