1*7511Sroot /* tcp_usrreq.c 1.60 82/07/21 */ 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" 106353Ssam #include "../net/route.h" 115089Swnj #include "../net/in_pcb.h" 125089Swnj #include "../net/in_systm.h" 134954Swnj #include "../net/if.h" 144809Swnj #include "../net/ip.h" 154900Swnj #include "../net/ip_var.h" 164809Swnj #include "../net/tcp.h" 174809Swnj #include "../net/tcp_fsm.h" 185089Swnj #include "../net/tcp_seq.h" 195089Swnj #include "../net/tcp_timer.h" 204809Swnj #include "../net/tcp_var.h" 215089Swnj #include "../net/tcpip.h" 225270Sroot #include "../net/tcp_debug.h" 236506Ssam #include <errno.h> 244497Swnj 255280Sroot /* 265280Sroot * TCP protocol interface to socket abstraction. 275280Sroot */ 285280Sroot extern char *tcpstates[]; 294954Swnj struct tcpcb *tcp_newtcpcb(); 305280Sroot 314734Swnj /* 325280Sroot * Process a TCP user request for TCP tb. If this is a send request 334731Swnj * then m is the mbuf chain of send data. If this is a timer expiration 344731Swnj * (called from the software clock routine), then timertype tells which timer. 354731Swnj */ 364809Swnj tcp_usrreq(so, req, m, addr) 374809Swnj struct socket *so; 384809Swnj int req; 394731Swnj struct mbuf *m; 404809Swnj caddr_t addr; 414497Swnj { 424886Swnj register struct inpcb *inp = sotoinpcb(so); 434911Swnj register struct tcpcb *tp; 444567Swnj int s = splnet(); 454809Swnj int error = 0; 465270Sroot int ostate; 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; 73*7511Sroot } else 74*7511Sroot ostate = 0; 754809Swnj switch (req) { 764497Swnj 775280Sroot /* 785280Sroot * TCP attaches to socket via PRU_ATTACH, reserving space, 795280Sroot * and internet and TCP control blocks. 805280Sroot * If the socket is to receive connections, 815280Sroot * then the LISTEN state is entered. 825280Sroot */ 834809Swnj case PRU_ATTACH: 844954Swnj if (inp) { 854809Swnj error = EISCONN; 864911Swnj break; 874886Swnj } 885280Sroot error = tcp_attach(so, (struct sockaddr *)addr); 895075Swnj if (error) 904954Swnj break; 915392Swnj if ((so->so_options & SO_DONTLINGER) == 0) 925392Swnj so->so_linger = TCP_LINGERTIME; 935280Sroot tp = sototcpcb(so); 944567Swnj break; 954497Swnj 965280Sroot /* 975280Sroot * PRU_DETACH detaches the TCP protocol from the socket. 985280Sroot * If the protocol state is non-embryonic, then can't 995280Sroot * do this directly: have to initiate a PRU_DISCONNECT, 1005280Sroot * which may finish later; embryonic TCB's can just 1015280Sroot * be discarded here. 1025280Sroot */ 1034809Swnj case PRU_DETACH: 1045280Sroot if (tp->t_state > TCPS_LISTEN) 1055280Sroot tcp_disconnect(tp); 1065280Sroot else { 1075280Sroot tcp_close(tp); 1085280Sroot tp = 0; 1095280Sroot } 1104809Swnj break; 1114809Swnj 1125280Sroot /* 1135280Sroot * Initiate connection to peer. 1145280Sroot * Create a template for use in transmissions on this connection. 1155280Sroot * Enter SYN_SENT state, and mark socket as connecting. 1165280Sroot * Start keep-alive timer, and seed output sequence space. 1175280Sroot * Send initial segment on connection. 1185280Sroot */ 1194809Swnj case PRU_CONNECT: 1205165Swnj error = in_pcbconnect(inp, (struct sockaddr_in *)addr); 1214954Swnj if (error) 1224886Swnj break; 1235174Swnj tp->t_template = tcp_template(tp); 1245280Sroot if (tp->t_template == 0) { 1255280Sroot in_pcbdisconnect(inp); 1265280Sroot error = ENOBUFS; 1275280Sroot break; 1285280Sroot } 1294886Swnj soisconnecting(so); 1305075Swnj tp->t_state = TCPS_SYN_SENT; 1315245Sroot tp->t_timer[TCPT_KEEP] = TCPTV_KEEP; 1325245Sroot tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2; 1335245Sroot tcp_sendseqinit(tp); 1346506Ssam error = tcp_output(tp); 1354567Swnj break; 1364497Swnj 1375280Sroot /* 1385280Sroot * Initiate disconnect from peer. 1395280Sroot * If connection never passed embryonic stage, just drop; 1405280Sroot * else if don't need to let data drain, then can just drop anyways, 1415280Sroot * else have to begin TCP shutdown process: mark socket disconnecting, 1425280Sroot * drain unread data, state switch to reflect user close, and 1435280Sroot * send segment (e.g. FIN) to peer. Socket will be really disconnected 1445280Sroot * when peer sends FIN and acks ours. 1455280Sroot * 1465280Sroot * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB. 1475280Sroot */ 1485280Sroot case PRU_DISCONNECT: 1495280Sroot tcp_disconnect(tp); 1505245Sroot break; 1515245Sroot 1525280Sroot /* 1535280Sroot * Accept a connection. Essentially all the work is 1545280Sroot * done at higher levels; just return the address 1555280Sroot * of the peer, storing through addr. 1565280Sroot */ 1576117Swnj case PRU_ACCEPT: { 1586117Swnj struct sockaddr_in *sin = (struct sockaddr_in *)addr; 1596117Swnj 1606117Swnj if (sin) { 1616117Swnj bzero((caddr_t)sin, sizeof (*sin)); 1626117Swnj sin->sin_family = AF_INET; 1636117Swnj sin->sin_port = inp->inp_fport; 1646117Swnj sin->sin_addr = inp->inp_faddr; 1656117Swnj } 1666117Swnj } 1674954Swnj break; 1684925Swnj 1695280Sroot /* 1705280Sroot * Mark the connection as being incapable of further output. 1715280Sroot */ 1724809Swnj case PRU_SHUTDOWN: 1735089Swnj socantsendmore(so); 1745245Sroot tcp_usrclosed(tp); 1756506Ssam error = tcp_output(tp); 1764567Swnj break; 1774497Swnj 1785280Sroot /* 1795280Sroot * After a receive, possibly send window update to peer. 1805280Sroot */ 1814809Swnj case PRU_RCVD: 1825113Swnj (void) tcp_output(tp); 1834567Swnj break; 1844497Swnj 1855280Sroot /* 1865280Sroot * Do a send by putting data in output queue and updating urgent 1875280Sroot * marker if URG set. Possibly send more data. 1885280Sroot */ 1894809Swnj case PRU_SEND: 1905075Swnj sbappend(&so->so_snd, m); 1916506Ssam #ifdef notdef 1925089Swnj if (tp->t_flags & TF_PUSH) 1935075Swnj tp->snd_end = tp->snd_una + so->so_snd.sb_cc; 1946506Ssam #endif 1956506Ssam error = tcp_output(tp); 1964567Swnj break; 1974567Swnj 1985280Sroot /* 1995280Sroot * Abort the TCP. 2005280Sroot */ 2014809Swnj case PRU_ABORT: 2025075Swnj tcp_drop(tp, ECONNABORTED); 2034567Swnj break; 2044567Swnj 2055280Sroot /* SOME AS YET UNIMPLEMENTED HOOKS */ 2064809Swnj case PRU_CONTROL: 2074886Swnj error = EOPNOTSUPP; 2084809Swnj break; 2094809Swnj 2105113Swnj case PRU_SENSE: 2115113Swnj error = EOPNOTSUPP; 2125113Swnj break; 2135417Swnj /* END UNIMPLEMENTED HOOKS */ 2145113Swnj 2155113Swnj case PRU_RCVOOB: 2165442Swnj if (so->so_oobmark == 0 && 2175442Swnj (so->so_state & SS_RCVATMARK) == 0) { 2185417Swnj error = EINVAL; 2195417Swnj break; 2205417Swnj } 2215549Swnj if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) { 2225442Swnj error = EWOULDBLOCK; 2235549Swnj break; 2245442Swnj } 2255549Swnj *mtod(m, caddr_t) = tp->t_iobc; 2265113Swnj break; 2275113Swnj 2285113Swnj case PRU_SENDOOB: 2295549Swnj #ifdef TCPTRUEOOB 2305549Swnj if (tp->t_flags & TF_DOOOB) { 2315549Swnj tp->t_oobseq++; 2325549Swnj tp->t_oobc = *mtod(m, caddr_t); 2335549Swnj tp->t_oobmark = tp->snd_una + so->so_snd.sb_cc; 2345549Swnj tp->t_oobflags |= TCPOOB_NEEDACK; 2356506Ssam /* what to do ...? */ 2366506Ssam if (error = tcp_output(tp)) 2376506Ssam break; 2385549Swnj } 2395549Swnj #endif 2405442Swnj if (sbspace(&so->so_snd) < -512) { 2415442Swnj error = ENOBUFS; 2425442Swnj break; 2435442Swnj } 2445417Swnj tp->snd_up = tp->snd_una + so->so_snd.sb_cc + 1; 2455417Swnj sbappend(&so->so_snd, m); 2466506Ssam #ifdef notdef 2475417Swnj if (tp->t_flags & TF_PUSH) 2485417Swnj tp->snd_end = tp->snd_una + so->so_snd.sb_cc; 2496506Ssam #endif 2505549Swnj tp->t_force = 1; 2516506Ssam error = tcp_output(tp); 2525549Swnj tp->t_force = 0; 2535113Swnj break; 2545113Swnj 2556510Ssam case PRU_SOCKADDR: 2566510Ssam in_setsockaddr((struct sockaddr_in *)addr, inp); 2576510Ssam break; 2586510Ssam 2595280Sroot /* 2605280Sroot * TCP slow timer went off; going through this 2615280Sroot * routine for tracing's sake. 2625280Sroot */ 2634809Swnj case PRU_SLOWTIMO: 2645075Swnj tcp_timers(tp, (int)addr); 2655270Sroot req |= (int)addr << 8; /* for debug's sake */ 2664809Swnj break; 2674809Swnj 2684731Swnj default: 2694731Swnj panic("tcp_usrreq"); 2704567Swnj } 2715270Sroot if (tp && (so->so_options & SO_DEBUG)) 2725270Sroot tcp_trace(TA_USER, ostate, tp, (struct tcpiphdr *)0, req); 2734567Swnj splx(s); 2744886Swnj return (error); 2754497Swnj } 2765245Sroot 2775953Swnj int tcp_sendspace = 1024*2; 2786601Ssam int tcp_recvspace = 1024*2; 2795280Sroot /* 2805280Sroot * Attach TCP protocol to socket, allocating 2815280Sroot * internet protocol control block, tcp control block, 2825280Sroot * bufer space, and entering LISTEN state if to accept connections. 2835280Sroot */ 2845280Sroot tcp_attach(so, sa) 2855280Sroot struct socket *so; 2865280Sroot struct sockaddr *sa; 2875280Sroot { 2885280Sroot register struct tcpcb *tp; 2895280Sroot struct inpcb *inp; 2905280Sroot int error; 2915280Sroot 292*7511Sroot error = in_pcbreserve(so, tcp_sendspace, tcp_recvspace); 2935280Sroot if (error) 294*7511Sroot goto bad; 295*7511Sroot error = in_pcballoc(so, &tcb); 296*7511Sroot if (error) 297*7511Sroot goto bad2; 2985280Sroot inp = (struct inpcb *)so->so_pcb; 299*7511Sroot if (sa || ((so->so_options & SO_ACCEPTCONN) == 0 && so->so_head == 0)) { 300*7511Sroot error = in_pcbbind(inp, sa); 301*7511Sroot if (error) 302*7511Sroot goto bad2; 303*7511Sroot } 3045280Sroot tp = tcp_newtcpcb(inp); 305*7511Sroot if (tp == 0) { 306*7511Sroot error = ENOBUFS; 307*7511Sroot goto bad2; 308*7511Sroot } 309*7511Sroot tp->t_state = 310*7511Sroot (so->so_options & SO_ACCEPTCONN) ? TCPS_LISTEN : TCPS_CLOSED; 3115280Sroot return (0); 312*7511Sroot bad2: 313*7511Sroot in_pcbdetach(inp); 314*7511Sroot bad: 315*7511Sroot return (error); 3165280Sroot } 3175280Sroot 3185280Sroot /* 3195280Sroot * Initiate (or continue) disconnect. 3205280Sroot * If embryonic state, just send reset (once). 3215280Sroot * If not in ``let data drain'' option, just drop. 3225280Sroot * Otherwise (hard), mark socket disconnecting and drop 3235280Sroot * current input data; switch states based on user close, and 3245280Sroot * send segment to peer (with FIN). 3255280Sroot */ 3265280Sroot tcp_disconnect(tp) 3275280Sroot struct tcpcb *tp; 3285280Sroot { 3295280Sroot struct socket *so = tp->t_inpcb->inp_socket; 3305280Sroot 3315280Sroot if (tp->t_state < TCPS_ESTABLISHED) 3325280Sroot tcp_close(tp); 3335392Swnj else if (so->so_linger == 0) 3345280Sroot tcp_drop(tp, 0); 3355280Sroot else { 3365280Sroot soisdisconnecting(so); 3375280Sroot sbflush(&so->so_rcv); 3385280Sroot tcp_usrclosed(tp); 3395280Sroot (void) tcp_output(tp); 3405280Sroot } 3415280Sroot } 3425280Sroot 3435280Sroot /* 3445280Sroot * User issued close, and wish to trail through shutdown states: 3455280Sroot * if never received SYN, just forget it. If got a SYN from peer, 3465280Sroot * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN. 3475280Sroot * If already got a FIN from peer, then almost done; go to LAST_ACK 3485280Sroot * state. In all other cases, have already sent FIN to peer (e.g. 3495280Sroot * after PRU_SHUTDOWN), and just have to play tedious game waiting 3505280Sroot * for peer to send FIN or not respond to keep-alives, etc. 3515897Swnj * We can let the user exit from the close as soon as the FIN is acked. 3525280Sroot */ 3535245Sroot tcp_usrclosed(tp) 3545245Sroot struct tcpcb *tp; 3555245Sroot { 3565245Sroot 3575245Sroot switch (tp->t_state) { 3585245Sroot 3595245Sroot case TCPS_LISTEN: 3605245Sroot case TCPS_SYN_SENT: 3615245Sroot tp->t_state = TCPS_CLOSED; 3625245Sroot tcp_close(tp); 3635245Sroot break; 3645245Sroot 3655245Sroot case TCPS_SYN_RECEIVED: 3665245Sroot case TCPS_ESTABLISHED: 3675245Sroot tp->t_state = TCPS_FIN_WAIT_1; 3685245Sroot break; 3695245Sroot 3705245Sroot case TCPS_CLOSE_WAIT: 3715245Sroot tp->t_state = TCPS_LAST_ACK; 3725245Sroot break; 3735245Sroot } 3745897Swnj if (tp->t_state >= TCPS_FIN_WAIT_2) 3755897Swnj soisdisconnected(tp->t_inpcb->inp_socket); 3765245Sroot } 377