1*5392Swnj /* tcp_usrreq.c 1.46 82/01/13 */ 24567Swnj 34497Swnj #include "../h/param.h" 44567Swnj #include "../h/systm.h" 54664Swnj #include "../h/mbuf.h" 64664Swnj #include "../h/socket.h" 74809Swnj #include "../h/socketvar.h" 84809Swnj #include "../h/protosw.h" 95089Swnj #include "../net/in.h" 105089Swnj #include "../net/in_pcb.h" 115089Swnj #include "../net/in_systm.h" 124954Swnj #include "../net/if.h" 134809Swnj #include "../net/ip.h" 144900Swnj #include "../net/ip_var.h" 154809Swnj #include "../net/tcp.h" 164809Swnj #include "../net/tcp_fsm.h" 175089Swnj #include "../net/tcp_seq.h" 185089Swnj #include "../net/tcp_timer.h" 194809Swnj #include "../net/tcp_var.h" 205089Swnj #include "../net/tcpip.h" 215270Sroot #include "../net/tcp_debug.h" 225113Swnj #include "../errno.h" 234497Swnj 245280Sroot /* 255280Sroot * TCP protocol interface to socket abstraction. 265280Sroot */ 275280Sroot extern char *tcpstates[]; 284954Swnj struct tcpcb *tcp_newtcpcb(); 295280Sroot 304734Swnj /* 315280Sroot * Process a TCP user request for TCP tb. If this is a send request 324731Swnj * then m is the mbuf chain of send data. If this is a timer expiration 334731Swnj * (called from the software clock routine), then timertype tells which timer. 344731Swnj */ 354809Swnj tcp_usrreq(so, req, m, addr) 364809Swnj struct socket *so; 374809Swnj int req; 384731Swnj struct mbuf *m; 394809Swnj caddr_t addr; 404497Swnj { 414886Swnj register struct inpcb *inp = sotoinpcb(so); 424911Swnj register struct tcpcb *tp; 434567Swnj int s = splnet(); 444809Swnj int error = 0; 455270Sroot int ostate; 464567Swnj COUNT(TCP_USRREQ); 474497Swnj 484886Swnj /* 495280Sroot * When a TCP is attached to a socket, then there will be 505280Sroot * a (struct inpcb) pointed at by the socket, and this 515280Sroot * structure will point at a subsidary (struct tcpcb). 525280Sroot * The normal sequence of events is: 535280Sroot * PRU_ATTACH creating these structures 545280Sroot * PRU_CONNECT connecting to a remote peer 555280Sroot * (PRU_SEND|PRU_RCVD)* exchanging data 565280Sroot * PRU_DISCONNECT disconnecting from remote peer 575280Sroot * PRU_DETACH deleting the structures 585280Sroot * With the operations from PRU_CONNECT through PRU_DISCONNECT 595280Sroot * possible repeated several times. 605280Sroot * 615280Sroot * MULTIPLE CONNECTS ARE NOT YET IMPLEMENTED. 624886Swnj */ 635089Swnj if (inp == 0 && req != PRU_ATTACH) { 645075Swnj splx(s); 655280Sroot return (EINVAL); /* XXX */ 665075Swnj } 675075Swnj if (inp) { 684911Swnj tp = intotcpcb(inp); 694731Swnj #ifdef KPROF 705075Swnj tcp_acounts[tp->t_state][req]++; 714731Swnj #endif 725270Sroot ostate = tp->t_state; 734911Swnj } 744809Swnj switch (req) { 754497Swnj 765280Sroot /* 775280Sroot * TCP attaches to socket via PRU_ATTACH, reserving space, 785280Sroot * and internet and TCP control blocks. 795280Sroot * If the socket is to receive connections, 805280Sroot * then the LISTEN state is entered. 815280Sroot */ 824809Swnj case PRU_ATTACH: 834954Swnj if (inp) { 844809Swnj error = EISCONN; 854911Swnj break; 864886Swnj } 875280Sroot error = tcp_attach(so, (struct sockaddr *)addr); 885075Swnj if (error) 894954Swnj break; 90*5392Swnj if ((so->so_options & SO_DONTLINGER) == 0) 91*5392Swnj so->so_linger = TCP_LINGERTIME; 925280Sroot tp = sototcpcb(so); 934567Swnj break; 944497Swnj 955280Sroot /* 965280Sroot * PRU_DETACH detaches the TCP protocol from the socket. 975280Sroot * If the protocol state is non-embryonic, then can't 985280Sroot * do this directly: have to initiate a PRU_DISCONNECT, 995280Sroot * which may finish later; embryonic TCB's can just 1005280Sroot * be discarded here. 1015280Sroot */ 1024809Swnj case PRU_DETACH: 1035280Sroot if (tp->t_state > TCPS_LISTEN) 1045280Sroot tcp_disconnect(tp); 1055280Sroot else { 1065280Sroot tcp_close(tp); 1075280Sroot tp = 0; 1085280Sroot } 1094809Swnj break; 1104809Swnj 1115280Sroot /* 1125280Sroot * Initiate connection to peer. 1135280Sroot * Create a template for use in transmissions on this connection. 1145280Sroot * Enter SYN_SENT state, and mark socket as connecting. 1155280Sroot * Start keep-alive timer, and seed output sequence space. 1165280Sroot * Send initial segment on connection. 1175280Sroot */ 1184809Swnj case PRU_CONNECT: 1195165Swnj error = in_pcbconnect(inp, (struct sockaddr_in *)addr); 1204954Swnj if (error) 1214886Swnj break; 1225174Swnj tp->t_template = tcp_template(tp); 1235280Sroot if (tp->t_template == 0) { 1245280Sroot in_pcbdisconnect(inp); 1255280Sroot error = ENOBUFS; 1265280Sroot break; 1275280Sroot } 1284886Swnj soisconnecting(so); 1295075Swnj tp->t_state = TCPS_SYN_SENT; 1305245Sroot tp->t_timer[TCPT_KEEP] = TCPTV_KEEP; 1315245Sroot tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2; 1325245Sroot tcp_sendseqinit(tp); 1335113Swnj (void) tcp_output(tp); 1344567Swnj break; 1354497Swnj 1365280Sroot /* 1375280Sroot * Initiate disconnect from peer. 1385280Sroot * If connection never passed embryonic stage, just drop; 1395280Sroot * else if don't need to let data drain, then can just drop anyways, 1405280Sroot * else have to begin TCP shutdown process: mark socket disconnecting, 1415280Sroot * drain unread data, state switch to reflect user close, and 1425280Sroot * send segment (e.g. FIN) to peer. Socket will be really disconnected 1435280Sroot * when peer sends FIN and acks ours. 1445280Sroot * 1455280Sroot * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB. 1465280Sroot */ 1475280Sroot case PRU_DISCONNECT: 1485280Sroot tcp_disconnect(tp); 1495245Sroot break; 1505245Sroot 1515280Sroot /* 1525280Sroot * Accept a connection. Essentially all the work is 1535280Sroot * done at higher levels; just return the address 1545280Sroot * of the peer, storing through addr. 1555280Sroot */ 1564925Swnj case PRU_ACCEPT: 1575280Sroot in_pcbconnaddr(inp, (struct sockaddr *)addr); 1584954Swnj break; 1594925Swnj 1605280Sroot /* 1615280Sroot * Mark the connection as being incapable of further output. 1625280Sroot */ 1634809Swnj case PRU_SHUTDOWN: 1645089Swnj socantsendmore(so); 1655245Sroot tcp_usrclosed(tp); 1665245Sroot (void) tcp_output(tp); 1674567Swnj break; 1684497Swnj 1695280Sroot /* 1705280Sroot * After a receive, possibly send window update to peer. 1715280Sroot */ 1724809Swnj case PRU_RCVD: 1735113Swnj (void) tcp_output(tp); 1744567Swnj break; 1754497Swnj 1765280Sroot /* 1775280Sroot * Do a send by putting data in output queue and updating urgent 1785280Sroot * marker if URG set. Possibly send more data. 1795280Sroot */ 1804809Swnj case PRU_SEND: 1815075Swnj sbappend(&so->so_snd, m); 1825089Swnj /* 1835089Swnj if (tp->t_flags & TF_PUSH) 1845075Swnj tp->snd_end = tp->snd_una + so->so_snd.sb_cc; 1855089Swnj */ 1865089Swnj if (tp->t_flags & TF_URG) 1875089Swnj tp->snd_up = tp->snd_una + so->so_snd.sb_cc + 1; 1885113Swnj (void) tcp_output(tp); 1894567Swnj break; 1904567Swnj 1915280Sroot /* 1925280Sroot * Abort the TCP. 1935280Sroot */ 1944809Swnj case PRU_ABORT: 1955075Swnj tcp_drop(tp, ECONNABORTED); 1964567Swnj break; 1974567Swnj 1985280Sroot /* SOME AS YET UNIMPLEMENTED HOOKS */ 1994809Swnj case PRU_CONTROL: 2004886Swnj error = EOPNOTSUPP; 2014809Swnj break; 2024809Swnj 2035113Swnj case PRU_SENSE: 2045113Swnj error = EOPNOTSUPP; 2055113Swnj break; 2065113Swnj 2075113Swnj case PRU_RCVOOB: 2085113Swnj error = EOPNOTSUPP; 2095113Swnj break; 2105113Swnj 2115113Swnj case PRU_SENDOOB: 2125113Swnj error = EOPNOTSUPP; 2135113Swnj break; 2145280Sroot /* END UNIMPLEMENTED HOOKS */ 2155113Swnj 2165280Sroot /* 2175280Sroot * TCP slow timer went off; going through this 2185280Sroot * routine for tracing's sake. 2195280Sroot */ 2204809Swnj case PRU_SLOWTIMO: 2215075Swnj tcp_timers(tp, (int)addr); 2225270Sroot req |= (int)addr << 8; /* for debug's sake */ 2234809Swnj break; 2244809Swnj 2254731Swnj default: 2264731Swnj panic("tcp_usrreq"); 2274567Swnj } 2285270Sroot if (tp && (so->so_options & SO_DEBUG)) 2295270Sroot tcp_trace(TA_USER, ostate, tp, (struct tcpiphdr *)0, req); 2304567Swnj splx(s); 2314886Swnj return (error); 2324497Swnj } 2335245Sroot 2345280Sroot /* 2355280Sroot * Attach TCP protocol to socket, allocating 2365280Sroot * internet protocol control block, tcp control block, 2375280Sroot * bufer space, and entering LISTEN state if to accept connections. 2385280Sroot */ 2395280Sroot tcp_attach(so, sa) 2405280Sroot struct socket *so; 2415280Sroot struct sockaddr *sa; 2425280Sroot { 2435280Sroot register struct tcpcb *tp; 2445280Sroot struct inpcb *inp; 2455280Sroot int error; 2465280Sroot 2475280Sroot error = in_pcbattach(so, &tcb, 2048, 2048, (struct sockaddr_in *)sa); 2485280Sroot if (error) 2495280Sroot return (error); 2505280Sroot inp = (struct inpcb *)so->so_pcb; 2515280Sroot tp = tcp_newtcpcb(inp); 2525280Sroot if (so->so_options & SO_ACCEPTCONN) { 2535280Sroot if (tp == 0) { 2545280Sroot in_pcbdetach(inp); 2555280Sroot return (ENOBUFS); 2565280Sroot } 2575280Sroot tp->t_state = TCPS_LISTEN; 2585280Sroot } else 2595280Sroot tp->t_state = TCPS_CLOSED; 2605280Sroot return (0); 2615280Sroot } 2625280Sroot 2635280Sroot /* 2645280Sroot * Initiate (or continue) disconnect. 2655280Sroot * If embryonic state, just send reset (once). 2665280Sroot * If not in ``let data drain'' option, just drop. 2675280Sroot * Otherwise (hard), mark socket disconnecting and drop 2685280Sroot * current input data; switch states based on user close, and 2695280Sroot * send segment to peer (with FIN). 2705280Sroot */ 2715280Sroot tcp_disconnect(tp) 2725280Sroot struct tcpcb *tp; 2735280Sroot { 2745280Sroot struct socket *so = tp->t_inpcb->inp_socket; 2755280Sroot 2765280Sroot if (tp->t_state < TCPS_ESTABLISHED) 2775280Sroot tcp_close(tp); 278*5392Swnj else if (so->so_linger == 0) 2795280Sroot tcp_drop(tp, 0); 2805280Sroot else { 2815280Sroot soisdisconnecting(so); 2825280Sroot sbflush(&so->so_rcv); 2835280Sroot tcp_usrclosed(tp); 2845280Sroot (void) tcp_output(tp); 2855280Sroot } 2865280Sroot } 2875280Sroot 2885280Sroot /* 2895280Sroot * User issued close, and wish to trail through shutdown states: 2905280Sroot * if never received SYN, just forget it. If got a SYN from peer, 2915280Sroot * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN. 2925280Sroot * If already got a FIN from peer, then almost done; go to LAST_ACK 2935280Sroot * state. In all other cases, have already sent FIN to peer (e.g. 2945280Sroot * after PRU_SHUTDOWN), and just have to play tedious game waiting 2955280Sroot * for peer to send FIN or not respond to keep-alives, etc. 2965280Sroot */ 2975245Sroot tcp_usrclosed(tp) 2985245Sroot struct tcpcb *tp; 2995245Sroot { 3005245Sroot 3015245Sroot switch (tp->t_state) { 3025245Sroot 3035245Sroot case TCPS_LISTEN: 3045245Sroot case TCPS_SYN_SENT: 3055245Sroot tp->t_state = TCPS_CLOSED; 3065245Sroot tcp_close(tp); 3075245Sroot break; 3085245Sroot 3095245Sroot case TCPS_SYN_RECEIVED: 3105245Sroot case TCPS_ESTABLISHED: 3115245Sroot tp->t_state = TCPS_FIN_WAIT_1; 3125245Sroot break; 3135245Sroot 3145245Sroot case TCPS_CLOSE_WAIT: 3155245Sroot tp->t_state = TCPS_LAST_ACK; 3165245Sroot break; 3175245Sroot } 3185245Sroot } 319