1*5549Swnj /* tcp_usrreq.c 1.49 82/01/18 */ 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; 905392Swnj if ((so->so_options & SO_DONTLINGER) == 0) 915392Swnj 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 */ 1865113Swnj (void) tcp_output(tp); 1874567Swnj break; 1884567Swnj 1895280Sroot /* 1905280Sroot * Abort the TCP. 1915280Sroot */ 1924809Swnj case PRU_ABORT: 1935075Swnj tcp_drop(tp, ECONNABORTED); 1944567Swnj break; 1954567Swnj 1965280Sroot /* SOME AS YET UNIMPLEMENTED HOOKS */ 1974809Swnj case PRU_CONTROL: 1984886Swnj error = EOPNOTSUPP; 1994809Swnj break; 2004809Swnj 2015113Swnj case PRU_SENSE: 2025113Swnj error = EOPNOTSUPP; 2035113Swnj break; 2045417Swnj /* END UNIMPLEMENTED HOOKS */ 2055113Swnj 2065113Swnj case PRU_RCVOOB: 2075442Swnj if (so->so_oobmark == 0 && 2085442Swnj (so->so_state & SS_RCVATMARK) == 0) { 2095417Swnj error = EINVAL; 2105417Swnj break; 2115417Swnj } 212*5549Swnj if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) { 2135442Swnj error = EWOULDBLOCK; 214*5549Swnj break; 2155442Swnj } 216*5549Swnj *mtod(m, caddr_t) = tp->t_iobc; 2175113Swnj break; 2185113Swnj 2195113Swnj case PRU_SENDOOB: 220*5549Swnj #ifdef TCPTRUEOOB 221*5549Swnj if (tp->t_flags & TF_DOOOB) { 222*5549Swnj tp->t_oobseq++; 223*5549Swnj tp->t_oobc = *mtod(m, caddr_t); 224*5549Swnj tp->t_oobmark = tp->snd_una + so->so_snd.sb_cc; 225*5549Swnj printf("sendoob seq now %x oobc %x\n", tp->t_oobseq, tp->t_oobc); 226*5549Swnj tp->t_oobflags |= TCPOOB_NEEDACK; 227*5549Swnj (void) tcp_output(tp); 228*5549Swnj } 229*5549Swnj #endif 2305442Swnj if (sbspace(&so->so_snd) < -512) { 2315442Swnj error = ENOBUFS; 2325442Swnj break; 2335442Swnj } 2345417Swnj tp->snd_up = tp->snd_una + so->so_snd.sb_cc + 1; 2355417Swnj sbappend(&so->so_snd, m); 2365417Swnj /* 2375417Swnj if (tp->t_flags & TF_PUSH) 2385417Swnj tp->snd_end = tp->snd_una + so->so_snd.sb_cc; 2395417Swnj */ 240*5549Swnj tp->t_force = 1; 241*5549Swnj (void) tcp_output(tp); 242*5549Swnj tp->t_force = 0; 2435113Swnj break; 2445113Swnj 2455280Sroot /* 2465280Sroot * TCP slow timer went off; going through this 2475280Sroot * routine for tracing's sake. 2485280Sroot */ 2494809Swnj case PRU_SLOWTIMO: 2505075Swnj tcp_timers(tp, (int)addr); 2515270Sroot req |= (int)addr << 8; /* for debug's sake */ 2524809Swnj break; 2534809Swnj 2544731Swnj default: 2554731Swnj panic("tcp_usrreq"); 2564567Swnj } 2575270Sroot if (tp && (so->so_options & SO_DEBUG)) 2585270Sroot tcp_trace(TA_USER, ostate, tp, (struct tcpiphdr *)0, req); 2594567Swnj splx(s); 2604886Swnj return (error); 2614497Swnj } 2625245Sroot 2635280Sroot /* 2645280Sroot * Attach TCP protocol to socket, allocating 2655280Sroot * internet protocol control block, tcp control block, 2665280Sroot * bufer space, and entering LISTEN state if to accept connections. 2675280Sroot */ 2685280Sroot tcp_attach(so, sa) 2695280Sroot struct socket *so; 2705280Sroot struct sockaddr *sa; 2715280Sroot { 2725280Sroot register struct tcpcb *tp; 2735280Sroot struct inpcb *inp; 2745280Sroot int error; 2755280Sroot 2765280Sroot error = in_pcbattach(so, &tcb, 2048, 2048, (struct sockaddr_in *)sa); 2775280Sroot if (error) 2785280Sroot return (error); 2795280Sroot inp = (struct inpcb *)so->so_pcb; 2805280Sroot tp = tcp_newtcpcb(inp); 2815280Sroot if (so->so_options & SO_ACCEPTCONN) { 2825280Sroot if (tp == 0) { 2835280Sroot in_pcbdetach(inp); 2845280Sroot return (ENOBUFS); 2855280Sroot } 2865280Sroot tp->t_state = TCPS_LISTEN; 2875280Sroot } else 2885280Sroot tp->t_state = TCPS_CLOSED; 2895280Sroot return (0); 2905280Sroot } 2915280Sroot 2925280Sroot /* 2935280Sroot * Initiate (or continue) disconnect. 2945280Sroot * If embryonic state, just send reset (once). 2955280Sroot * If not in ``let data drain'' option, just drop. 2965280Sroot * Otherwise (hard), mark socket disconnecting and drop 2975280Sroot * current input data; switch states based on user close, and 2985280Sroot * send segment to peer (with FIN). 2995280Sroot */ 3005280Sroot tcp_disconnect(tp) 3015280Sroot struct tcpcb *tp; 3025280Sroot { 3035280Sroot struct socket *so = tp->t_inpcb->inp_socket; 3045280Sroot 3055280Sroot if (tp->t_state < TCPS_ESTABLISHED) 3065280Sroot tcp_close(tp); 3075392Swnj else if (so->so_linger == 0) 3085280Sroot tcp_drop(tp, 0); 3095280Sroot else { 3105280Sroot soisdisconnecting(so); 3115280Sroot sbflush(&so->so_rcv); 3125280Sroot tcp_usrclosed(tp); 3135280Sroot (void) tcp_output(tp); 3145280Sroot } 3155280Sroot } 3165280Sroot 3175280Sroot /* 3185280Sroot * User issued close, and wish to trail through shutdown states: 3195280Sroot * if never received SYN, just forget it. If got a SYN from peer, 3205280Sroot * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN. 3215280Sroot * If already got a FIN from peer, then almost done; go to LAST_ACK 3225280Sroot * state. In all other cases, have already sent FIN to peer (e.g. 3235280Sroot * after PRU_SHUTDOWN), and just have to play tedious game waiting 3245280Sroot * for peer to send FIN or not respond to keep-alives, etc. 3255280Sroot */ 3265245Sroot tcp_usrclosed(tp) 3275245Sroot struct tcpcb *tp; 3285245Sroot { 3295245Sroot 3305245Sroot switch (tp->t_state) { 3315245Sroot 3325245Sroot case TCPS_LISTEN: 3335245Sroot case TCPS_SYN_SENT: 3345245Sroot tp->t_state = TCPS_CLOSED; 3355245Sroot tcp_close(tp); 3365245Sroot break; 3375245Sroot 3385245Sroot case TCPS_SYN_RECEIVED: 3395245Sroot case TCPS_ESTABLISHED: 3405245Sroot tp->t_state = TCPS_FIN_WAIT_1; 3415245Sroot break; 3425245Sroot 3435245Sroot case TCPS_CLOSE_WAIT: 3445245Sroot tp->t_state = TCPS_LAST_ACK; 3455245Sroot break; 3465245Sroot } 3475245Sroot } 348