1*8601Sroot /* tcp_usrreq.c 1.66 82/10/17 */ 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" 98406Swnj #include "../netinet/in.h" 106353Ssam #include "../net/route.h" 118406Swnj #include "../netinet/in_pcb.h" 128406Swnj #include "../netinet/in_systm.h" 134954Swnj #include "../net/if.h" 148406Swnj #include "../netinet/ip.h" 158406Swnj #include "../netinet/ip_var.h" 168406Swnj #include "../netinet/tcp.h" 178406Swnj #include "../netinet/tcp_fsm.h" 188406Swnj #include "../netinet/tcp_seq.h" 198406Swnj #include "../netinet/tcp_timer.h" 208406Swnj #include "../netinet/tcp_var.h" 218406Swnj #include "../netinet/tcpip.h" 228406Swnj #include "../netinet/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 */ 36*8601Sroot /*ARGSUSED*/ 378272Sroot tcp_usrreq(so, req, m, nam, opt) 384809Swnj struct socket *so; 394809Swnj int req; 408272Sroot struct mbuf *m, *nam; 418272Sroot struct socketopt *opt; 424497Swnj { 434886Swnj register struct inpcb *inp = sotoinpcb(so); 444911Swnj register struct tcpcb *tp; 454567Swnj int s = splnet(); 464809Swnj int error = 0; 475270Sroot int ostate; 484497Swnj 494886Swnj /* 505280Sroot * When a TCP is attached to a socket, then there will be 515280Sroot * a (struct inpcb) pointed at by the socket, and this 525280Sroot * structure will point at a subsidary (struct tcpcb). 534886Swnj */ 545089Swnj if (inp == 0 && req != PRU_ATTACH) { 555075Swnj splx(s); 565280Sroot return (EINVAL); /* XXX */ 575075Swnj } 585075Swnj if (inp) { 594911Swnj tp = intotcpcb(inp); 608272Sroot /* WHAT IF TP IS 0? */ 614731Swnj #ifdef KPROF 625075Swnj tcp_acounts[tp->t_state][req]++; 634731Swnj #endif 645270Sroot ostate = tp->t_state; 657511Sroot } else 667511Sroot ostate = 0; 674809Swnj switch (req) { 684497Swnj 695280Sroot /* 705280Sroot * TCP attaches to socket via PRU_ATTACH, reserving space, 718272Sroot * and an internet control block. 725280Sroot */ 734809Swnj case PRU_ATTACH: 744954Swnj if (inp) { 754809Swnj error = EISCONN; 764911Swnj break; 774886Swnj } 788272Sroot error = tcp_attach(so, nam); 795075Swnj if (error) 804954Swnj break; 815392Swnj if ((so->so_options & SO_DONTLINGER) == 0) 825392Swnj so->so_linger = TCP_LINGERTIME; 835280Sroot tp = sototcpcb(so); 844567Swnj break; 854497Swnj 865280Sroot /* 875280Sroot * PRU_DETACH detaches the TCP protocol from the socket. 885280Sroot * If the protocol state is non-embryonic, then can't 895280Sroot * do this directly: have to initiate a PRU_DISCONNECT, 905280Sroot * which may finish later; embryonic TCB's can just 915280Sroot * be discarded here. 925280Sroot */ 934809Swnj case PRU_DETACH: 945280Sroot if (tp->t_state > TCPS_LISTEN) 955280Sroot tcp_disconnect(tp); 965280Sroot else { 975280Sroot tcp_close(tp); 985280Sroot tp = 0; 995280Sroot } 1004809Swnj break; 1014809Swnj 1025280Sroot /* 1038272Sroot * Give the socket an address. 1048272Sroot */ 1058272Sroot case PRU_BIND: 1068272Sroot error = in_pcbbind(inp, nam); 1078272Sroot if (error) 1088272Sroot break; 1098272Sroot break; 1108272Sroot 1118272Sroot /* 1128272Sroot * Prepare to accept connections. 1138272Sroot */ 1148272Sroot case PRU_LISTEN: 1158272Sroot if (inp->inp_lport == 0) 1168272Sroot error = in_pcbbind(inp, (struct mbuf *)0); 1178272Sroot if (error == 0) 1188272Sroot tp->t_state = TCPS_LISTEN; 1198272Sroot break; 1208272Sroot 1218272Sroot /* 1225280Sroot * Initiate connection to peer. 1235280Sroot * Create a template for use in transmissions on this connection. 1245280Sroot * Enter SYN_SENT state, and mark socket as connecting. 1255280Sroot * Start keep-alive timer, and seed output sequence space. 1265280Sroot * Send initial segment on connection. 1275280Sroot */ 1284809Swnj case PRU_CONNECT: 1298272Sroot if (inp->inp_lport == 0) { 1308272Sroot error = in_pcbbind(inp, (struct mbuf *)0); 1318272Sroot if (error) 1328272Sroot break; 1338272Sroot } 1348272Sroot error = in_pcbconnect(inp, nam); 1354954Swnj if (error) 1364886Swnj break; 1375174Swnj tp->t_template = tcp_template(tp); 1385280Sroot if (tp->t_template == 0) { 1395280Sroot in_pcbdisconnect(inp); 1405280Sroot error = ENOBUFS; 1415280Sroot break; 1425280Sroot } 1434886Swnj soisconnecting(so); 1445075Swnj tp->t_state = TCPS_SYN_SENT; 1455245Sroot tp->t_timer[TCPT_KEEP] = TCPTV_KEEP; 1465245Sroot tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2; 1475245Sroot tcp_sendseqinit(tp); 1486506Ssam error = tcp_output(tp); 1494567Swnj break; 1504497Swnj 1515280Sroot /* 1525280Sroot * Initiate disconnect from peer. 1535280Sroot * If connection never passed embryonic stage, just drop; 1545280Sroot * else if don't need to let data drain, then can just drop anyways, 1555280Sroot * else have to begin TCP shutdown process: mark socket disconnecting, 1565280Sroot * drain unread data, state switch to reflect user close, and 1575280Sroot * send segment (e.g. FIN) to peer. Socket will be really disconnected 1585280Sroot * when peer sends FIN and acks ours. 1595280Sroot * 1605280Sroot * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB. 1615280Sroot */ 1625280Sroot case PRU_DISCONNECT: 1635280Sroot tcp_disconnect(tp); 1645245Sroot break; 1655245Sroot 1665280Sroot /* 1675280Sroot * Accept a connection. Essentially all the work is 1685280Sroot * done at higher levels; just return the address 1695280Sroot * of the peer, storing through addr. 1705280Sroot */ 1716117Swnj case PRU_ACCEPT: { 1728272Sroot struct sockaddr_in *sin = mtod(nam, struct sockaddr_in *); 1736117Swnj 1748272Sroot nam->m_len = sizeof (struct sockaddr_in); 1758272Sroot sin->sin_family = AF_INET; 1768272Sroot sin->sin_port = inp->inp_fport; 1778272Sroot sin->sin_addr = inp->inp_faddr; 1788272Sroot break; 1796117Swnj } 1804925Swnj 1815280Sroot /* 1825280Sroot * Mark the connection as being incapable of further output. 1835280Sroot */ 1844809Swnj case PRU_SHUTDOWN: 1855089Swnj socantsendmore(so); 1865245Sroot tcp_usrclosed(tp); 1876506Ssam error = tcp_output(tp); 1884567Swnj break; 1894497Swnj 1905280Sroot /* 1915280Sroot * After a receive, possibly send window update to peer. 1925280Sroot */ 1934809Swnj case PRU_RCVD: 1945113Swnj (void) tcp_output(tp); 1954567Swnj break; 1964497Swnj 1975280Sroot /* 1985280Sroot * Do a send by putting data in output queue and updating urgent 1995280Sroot * marker if URG set. Possibly send more data. 2005280Sroot */ 2014809Swnj case PRU_SEND: 2025075Swnj sbappend(&so->so_snd, m); 2036506Ssam #ifdef notdef 2045089Swnj if (tp->t_flags & TF_PUSH) 2055075Swnj tp->snd_end = tp->snd_una + so->so_snd.sb_cc; 2066506Ssam #endif 2076506Ssam error = tcp_output(tp); 2084567Swnj break; 2094567Swnj 2105280Sroot /* 2115280Sroot * Abort the TCP. 2125280Sroot */ 2134809Swnj case PRU_ABORT: 2145075Swnj tcp_drop(tp, ECONNABORTED); 2154567Swnj break; 2164567Swnj 2175280Sroot /* SOME AS YET UNIMPLEMENTED HOOKS */ 2184809Swnj case PRU_CONTROL: 2194886Swnj error = EOPNOTSUPP; 2204809Swnj break; 2214809Swnj 2225113Swnj case PRU_SENSE: 2235113Swnj error = EOPNOTSUPP; 2245113Swnj break; 2255417Swnj /* END UNIMPLEMENTED HOOKS */ 2265113Swnj 2275113Swnj case PRU_RCVOOB: 2285442Swnj if (so->so_oobmark == 0 && 2295442Swnj (so->so_state & SS_RCVATMARK) == 0) { 2305417Swnj error = EINVAL; 2315417Swnj break; 2325417Swnj } 2335549Swnj if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) { 2345442Swnj error = EWOULDBLOCK; 2355549Swnj break; 2365442Swnj } 2378310Sroot m->m_len = 1; 2385549Swnj *mtod(m, caddr_t) = tp->t_iobc; 2395113Swnj break; 2405113Swnj 2415113Swnj case PRU_SENDOOB: 2425442Swnj if (sbspace(&so->so_snd) < -512) { 2435442Swnj error = ENOBUFS; 2445442Swnj break; 2455442Swnj } 2465417Swnj tp->snd_up = tp->snd_una + so->so_snd.sb_cc + 1; 2475417Swnj sbappend(&so->so_snd, m); 2485549Swnj tp->t_force = 1; 2496506Ssam error = tcp_output(tp); 2505549Swnj tp->t_force = 0; 2515113Swnj break; 2525113Swnj 2536510Ssam case PRU_SOCKADDR: 2548272Sroot in_setsockaddr(inp, nam); 2556510Ssam break; 2566510Ssam 2575280Sroot /* 2585280Sroot * TCP slow timer went off; going through this 2595280Sroot * routine for tracing's sake. 2605280Sroot */ 2614809Swnj case PRU_SLOWTIMO: 2628272Sroot tcp_timers(tp, (int)nam); 2638272Sroot req |= (int)nam << 8; /* for debug's sake */ 2644809Swnj break; 2654809Swnj 2664731Swnj default: 2674731Swnj panic("tcp_usrreq"); 2684567Swnj } 2695270Sroot if (tp && (so->so_options & SO_DEBUG)) 2705270Sroot tcp_trace(TA_USER, ostate, tp, (struct tcpiphdr *)0, req); 2714567Swnj splx(s); 2724886Swnj return (error); 2734497Swnj } 2745245Sroot 2755953Swnj int tcp_sendspace = 1024*2; 2766601Ssam int tcp_recvspace = 1024*2; 2775280Sroot /* 2785280Sroot * Attach TCP protocol to socket, allocating 2795280Sroot * internet protocol control block, tcp control block, 2805280Sroot * bufer space, and entering LISTEN state if to accept connections. 2815280Sroot */ 2828272Sroot tcp_attach(so) 2835280Sroot struct socket *so; 2845280Sroot { 2855280Sroot register struct tcpcb *tp; 2865280Sroot struct inpcb *inp; 2875280Sroot int error; 2885280Sroot 2897511Sroot error = in_pcbreserve(so, tcp_sendspace, tcp_recvspace); 2905280Sroot if (error) 2917511Sroot goto bad; 2927511Sroot error = in_pcballoc(so, &tcb); 2937511Sroot if (error) 2948272Sroot goto bad; 2958272Sroot inp = sotoinpcb(so); 2965280Sroot tp = tcp_newtcpcb(inp); 2977511Sroot if (tp == 0) { 2987511Sroot error = ENOBUFS; 2997511Sroot goto bad2; 3007511Sroot } 3018272Sroot tp->t_state = TCPS_CLOSED; 3025280Sroot return (0); 3037511Sroot bad2: 3047511Sroot in_pcbdetach(inp); 3057511Sroot bad: 3067511Sroot return (error); 3075280Sroot } 3085280Sroot 3095280Sroot /* 3105280Sroot * Initiate (or continue) disconnect. 3115280Sroot * If embryonic state, just send reset (once). 3125280Sroot * If not in ``let data drain'' option, just drop. 3135280Sroot * Otherwise (hard), mark socket disconnecting and drop 3145280Sroot * current input data; switch states based on user close, and 3155280Sroot * send segment to peer (with FIN). 3165280Sroot */ 3175280Sroot tcp_disconnect(tp) 3185280Sroot struct tcpcb *tp; 3195280Sroot { 3205280Sroot struct socket *so = tp->t_inpcb->inp_socket; 3215280Sroot 3225280Sroot if (tp->t_state < TCPS_ESTABLISHED) 3235280Sroot tcp_close(tp); 3245392Swnj else if (so->so_linger == 0) 3255280Sroot tcp_drop(tp, 0); 3265280Sroot else { 3275280Sroot soisdisconnecting(so); 3285280Sroot sbflush(&so->so_rcv); 3295280Sroot tcp_usrclosed(tp); 3305280Sroot (void) tcp_output(tp); 3315280Sroot } 3325280Sroot } 3335280Sroot 3345280Sroot /* 3355280Sroot * User issued close, and wish to trail through shutdown states: 3365280Sroot * if never received SYN, just forget it. If got a SYN from peer, 3375280Sroot * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN. 3385280Sroot * If already got a FIN from peer, then almost done; go to LAST_ACK 3395280Sroot * state. In all other cases, have already sent FIN to peer (e.g. 3405280Sroot * after PRU_SHUTDOWN), and just have to play tedious game waiting 3415280Sroot * for peer to send FIN or not respond to keep-alives, etc. 3425897Swnj * We can let the user exit from the close as soon as the FIN is acked. 3435280Sroot */ 3445245Sroot tcp_usrclosed(tp) 3455245Sroot struct tcpcb *tp; 3465245Sroot { 3475245Sroot 3485245Sroot switch (tp->t_state) { 3495245Sroot 3505245Sroot case TCPS_LISTEN: 3515245Sroot case TCPS_SYN_SENT: 3525245Sroot tp->t_state = TCPS_CLOSED; 3535245Sroot tcp_close(tp); 3545245Sroot break; 3555245Sroot 3565245Sroot case TCPS_SYN_RECEIVED: 3575245Sroot case TCPS_ESTABLISHED: 3585245Sroot tp->t_state = TCPS_FIN_WAIT_1; 3595245Sroot break; 3605245Sroot 3615245Sroot case TCPS_CLOSE_WAIT: 3625245Sroot tp->t_state = TCPS_LAST_ACK; 3635245Sroot break; 3645245Sroot } 3655897Swnj if (tp->t_state >= TCPS_FIN_WAIT_2) 3665897Swnj soisdisconnected(tp->t_inpcb->inp_socket); 3675245Sroot } 368