1*15829Scooper /* uipc_socket2.c 6.2 84/01/11 */ 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 * 3412758Ssam * From the passive side, a socket is created with 3512758Ssam * 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 * 4212758Ssam * If a socket is closed with sockets on either 437509Sroot * so_q0 or so_q, these sockets are dropped. 447509Sroot * 4512758Ssam * If higher level protocols are implemented in 465169Swnj * the kernel, the wakeups done here will sometimes 4712758Ssam * cause software-interrupt process scheduling. 484903Swnj */ 495169Swnj 504903Swnj soisconnecting(so) 5112758Ssam 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) 6012758Ssam 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); 6812758Ssam 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) 7912758Ssam 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) 9012758Ssam 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; 11212758Ssam 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); 12812758Ssam if ((*so->so_proto->pr_usrreq)(so, PRU_ATTACH, 12912758Ssam (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) 24312758Ssam 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 /* 258*15829Scooper * Wakeup socket readers and writers. 259*15829Scooper * Do asynchronous notification via SIGIO 260*15829Scooper * if the socket has the SS_ASYNC flag set. 261*15829Scooper */ 262*15829Scooper sowakeup(so, sb) 263*15829Scooper register struct socket *so; 264*15829Scooper struct sockbuf *sb; 265*15829Scooper { 266*15829Scooper register struct proc *p; 267*15829Scooper 268*15829Scooper sbwakeup(sb); 269*15829Scooper if (so->so_state & SS_ASYNC) { 270*15829Scooper if (so->so_pgrp == 0) 271*15829Scooper return; 272*15829Scooper else if (so->so_pgrp > 0) 273*15829Scooper gsignal(so->so_pgrp, SIGIO); 274*15829Scooper else if ((p = pfind(-so->so_pgrp)) != 0) 275*15829Scooper psignal(p, SIGIO); 276*15829Scooper } 277*15829Scooper } 278*15829Scooper 279*15829Scooper /* 2805169Swnj * Socket buffer (struct sockbuf) utility routines. 2815169Swnj * 2825169Swnj * Each socket contains two socket buffers: one for sending data and 2835169Swnj * one for receiving data. Each buffer contains a queue of mbufs, 2845169Swnj * information about the number of mbufs and amount of data in the 2855169Swnj * queue, and other fields allowing select() statements and notification 2865169Swnj * on data availability to be implemented. 2875169Swnj * 2885169Swnj * Before using a new socket structure it is first necessary to reserve 2895169Swnj * buffer space to the socket, by calling sbreserve. This commits 2905169Swnj * some of the available buffer space in the system buffer pool for the 2915169Swnj * socket. The space should be released by calling sbrelease when the 2925169Swnj * socket is destroyed. 2935169Swnj * 2945169Swnj * The routine sbappend() is normally called to append new mbufs 2955169Swnj * to a socket buffer, after checking that adequate space is available 2965169Swnj * comparing the function spspace() with the amount of data to be added. 2975169Swnj * Data is normally removed from a socket buffer in a protocol by 2985169Swnj * first calling m_copy on the socket buffer mbuf chain and sending this 2995169Swnj * to a peer, and then removing the data from the socket buffer with 3005169Swnj * sbdrop when the data is acknowledged by the peer (or immediately 3015170Swnj * in the case of unreliable protocols.) 3025169Swnj * 3035169Swnj * Protocols which do not require connections place both source address 3045169Swnj * and data information in socket buffer queues. The source addresses 3055169Swnj * are stored in single mbufs after each data item, and are easily found 3065169Swnj * as the data items are all marked with end of record markers. The 3075169Swnj * sbappendaddr() routine stores a datum and associated address in 3085169Swnj * a socket buffer. Note that, unlike sbappend(), this routine checks 3095169Swnj * for the caller that there will be enough space to store the data. 3105169Swnj * It fails if there is not enough space, or if it cannot find 3115169Swnj * a mbuf to store the address in. 3125169Swnj * 3135169Swnj * The higher-level routines sosend and soreceive (in socket.c) 3145170Swnj * also add data to, and remove data from socket buffers repectively. 3155169Swnj */ 3165169Swnj 3179027Sroot soreserve(so, sndcc, rcvcc) 31812758Ssam register struct socket *so; 3199027Sroot int sndcc, rcvcc; 3209027Sroot { 3219027Sroot 3229027Sroot if (sbreserve(&so->so_snd, sndcc) == 0) 3239027Sroot goto bad; 3249027Sroot if (sbreserve(&so->so_rcv, rcvcc) == 0) 3259027Sroot goto bad2; 3269027Sroot return (0); 3279027Sroot bad2: 3289027Sroot sbrelease(&so->so_snd); 3299027Sroot bad: 3309027Sroot return (ENOBUFS); 3319027Sroot } 3329027Sroot 3335169Swnj /* 3344903Swnj * Allot mbufs to a sockbuf. 3354903Swnj */ 3364903Swnj sbreserve(sb, cc) 3374903Swnj struct sockbuf *sb; 3384903Swnj { 3394903Swnj 3407181Swnj /* someday maybe this routine will fail... */ 3414980Swnj sb->sb_hiwat = cc; 34212758Ssam /* * 2 implies names can be no more than 1 mbuf each */ 34312758Ssam sb->sb_mbmax = cc<<1; 3444917Swnj return (1); 3454903Swnj } 3464903Swnj 3474903Swnj /* 3484903Swnj * Free mbufs held by a socket, and reserved mbuf space. 3494903Swnj */ 3504903Swnj sbrelease(sb) 3514903Swnj struct sockbuf *sb; 3524903Swnj { 3534903Swnj 3544903Swnj sbflush(sb); 3554980Swnj sb->sb_hiwat = sb->sb_mbmax = 0; 3564903Swnj } 3574903Swnj 3584903Swnj /* 3594903Swnj * Routines to add (at the end) and remove (from the beginning) 3604903Swnj * data from a mbuf queue. 3614903Swnj */ 3624903Swnj 3634903Swnj /* 3644903Swnj * Append mbuf queue m to sockbuf sb. 3654903Swnj */ 3664903Swnj sbappend(sb, m) 3674903Swnj register struct mbuf *m; 3684903Swnj register struct sockbuf *sb; 3694903Swnj { 3706092Sroot register struct mbuf *n; 3714903Swnj 3726092Sroot n = sb->sb_mb; 3736092Sroot if (n) 3746092Sroot while (n->m_next) 3756092Sroot n = n->m_next; 3764903Swnj while (m) { 3775266Swnj if (m->m_len == 0 && (int)m->m_act == 0) { 3785304Sroot m = m_free(m); 3795266Swnj continue; 3805266Swnj } 3814903Swnj if (n && n->m_off <= MMAXOFF && m->m_off <= MMAXOFF && 3824903Swnj (int)n->m_act == 0 && (int)m->m_act == 0 && 3835042Swnj (n->m_off + n->m_len + m->m_len) <= MMAXOFF) { 3845042Swnj bcopy(mtod(m, caddr_t), mtod(n, caddr_t) + n->m_len, 3854917Swnj (unsigned)m->m_len); 3864903Swnj n->m_len += m->m_len; 3874903Swnj sb->sb_cc += m->m_len; 3884903Swnj m = m_free(m); 3894903Swnj continue; 3904903Swnj } 3914903Swnj sballoc(sb, m); 3926092Sroot if (n == 0) 3936092Sroot sb->sb_mb = m; 3946092Sroot else 3956092Sroot n->m_next = m; 3964903Swnj n = m; 3974903Swnj m = m->m_next; 3986092Sroot n->m_next = 0; 3994903Swnj } 4004903Swnj } 4014903Swnj 4025169Swnj /* 4035169Swnj * Append data and address. 4045169Swnj * Return 0 if no space in sockbuf or if 4055169Swnj * can't get mbuf to stuff address in. 4065169Swnj */ 40712758Ssam sbappendaddr(sb, asa, m0, rights0) 4084928Swnj struct sockbuf *sb; 4094928Swnj struct sockaddr *asa; 41012758Ssam struct mbuf *m0, *rights0; 4114928Swnj { 4124928Swnj register struct mbuf *m; 4134928Swnj register int len = sizeof (struct sockaddr); 41412758Ssam register struct mbuf *rights; 4154928Swnj 41612758Ssam if (rights0) 41712758Ssam len += rights0->m_len; 4185042Swnj m = m0; 4195042Swnj if (m == 0) 4205042Swnj panic("sbappendaddr"); 4215042Swnj for (;;) { 4224928Swnj len += m->m_len; 4235042Swnj if (m->m_next == 0) { 4245042Swnj m->m_act = (struct mbuf *)1; 4255042Swnj break; 4265042Swnj } 4275042Swnj m = m->m_next; 4285042Swnj } 4295043Swnj if (len > sbspace(sb)) 4304928Swnj return (0); 4319636Ssam m = m_get(M_DONTWAIT, MT_SONAME); 43212758Ssam if (m == NULL) 4334928Swnj return (0); 4344928Swnj m->m_len = sizeof (struct sockaddr); 4354928Swnj m->m_act = (struct mbuf *)1; 43612758Ssam *mtod(m, struct sockaddr *) = *asa; 43712758Ssam if (rights0 == 0 || rights0->m_len == 0) { 43812758Ssam rights = m_get(M_DONTWAIT, MT_SONAME); 43912758Ssam if (rights) 44012758Ssam rights->m_len = 0; 44112758Ssam } else 44212758Ssam rights = m_copy(rights0, 0, rights0->m_len); 44312758Ssam if (rights == 0) { 44412758Ssam m_freem(m); 44512758Ssam return (0); 44612758Ssam } 44712758Ssam rights->m_act = (struct mbuf *)1; 44812758Ssam m->m_next = rights; 44912758Ssam rights->m_next = m0; 4504928Swnj sbappend(sb, m); 4514928Swnj return (1); 4524928Swnj } 4534928Swnj 4544903Swnj /* 4554903Swnj * Free all mbufs on a sockbuf mbuf chain. 4564903Swnj * Check that resource allocations return to 0. 4574903Swnj */ 4584903Swnj sbflush(sb) 45912758Ssam register struct sockbuf *sb; 4604903Swnj { 4614903Swnj 4624903Swnj if (sb->sb_flags & SB_LOCK) 4634903Swnj panic("sbflush"); 4645266Swnj if (sb->sb_cc) 4655266Swnj sbdrop(sb, sb->sb_cc); 4664903Swnj if (sb->sb_cc || sb->sb_mbcnt || sb->sb_mb) 4674903Swnj panic("sbflush 2"); 4684903Swnj } 4694903Swnj 4704903Swnj /* 4714903Swnj * Drop data from (the front of) a sockbuf chain. 4724903Swnj */ 4734903Swnj sbdrop(sb, len) 4744903Swnj register struct sockbuf *sb; 4754903Swnj register int len; 4764903Swnj { 4774903Swnj register struct mbuf *m = sb->sb_mb, *mn; 4784903Swnj 4794903Swnj while (len > 0) { 4804903Swnj if (m == 0) 4814903Swnj panic("sbdrop"); 4825064Swnj if (m->m_len > len) { 4834903Swnj m->m_len -= len; 4844903Swnj m->m_off += len; 4854903Swnj sb->sb_cc -= len; 4864903Swnj break; 4874903Swnj } 4885064Swnj len -= m->m_len; 4895064Swnj sbfree(sb, m); 4905064Swnj MFREE(m, mn); 4915064Swnj m = mn; 4924903Swnj } 4934903Swnj sb->sb_mb = m; 4944903Swnj } 495