1*8310Sroot /* tcp_usrreq.c 1.63 82/10/05 */ 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 */ 368272Sroot tcp_usrreq(so, req, m, nam, opt) 374809Swnj struct socket *so; 384809Swnj int req; 398272Sroot struct mbuf *m, *nam; 408272Sroot struct socketopt *opt; 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). 524886Swnj */ 535089Swnj if (inp == 0 && req != PRU_ATTACH) { 545075Swnj splx(s); 555280Sroot return (EINVAL); /* XXX */ 565075Swnj } 575075Swnj if (inp) { 584911Swnj tp = intotcpcb(inp); 598272Sroot /* WHAT IF TP IS 0? */ 604731Swnj #ifdef KPROF 615075Swnj tcp_acounts[tp->t_state][req]++; 624731Swnj #endif 635270Sroot ostate = tp->t_state; 647511Sroot } else 657511Sroot ostate = 0; 664809Swnj switch (req) { 674497Swnj 685280Sroot /* 695280Sroot * TCP attaches to socket via PRU_ATTACH, reserving space, 708272Sroot * and an internet control block. 715280Sroot */ 724809Swnj case PRU_ATTACH: 734954Swnj if (inp) { 744809Swnj error = EISCONN; 754911Swnj break; 764886Swnj } 778272Sroot error = tcp_attach(so, nam); 785075Swnj if (error) 794954Swnj break; 805392Swnj if ((so->so_options & SO_DONTLINGER) == 0) 815392Swnj so->so_linger = TCP_LINGERTIME; 825280Sroot tp = sototcpcb(so); 834567Swnj break; 844497Swnj 855280Sroot /* 865280Sroot * PRU_DETACH detaches the TCP protocol from the socket. 875280Sroot * If the protocol state is non-embryonic, then can't 885280Sroot * do this directly: have to initiate a PRU_DISCONNECT, 895280Sroot * which may finish later; embryonic TCB's can just 905280Sroot * be discarded here. 915280Sroot */ 924809Swnj case PRU_DETACH: 935280Sroot if (tp->t_state > TCPS_LISTEN) 945280Sroot tcp_disconnect(tp); 955280Sroot else { 965280Sroot tcp_close(tp); 975280Sroot tp = 0; 985280Sroot } 994809Swnj break; 1004809Swnj 1015280Sroot /* 1028272Sroot * Give the socket an address. 1038272Sroot */ 1048272Sroot case PRU_BIND: 1058272Sroot error = in_pcbbind(inp, nam); 1068272Sroot if (error) 1078272Sroot break; 1088272Sroot break; 1098272Sroot 1108272Sroot /* 1118272Sroot * Prepare to accept connections. 1128272Sroot */ 1138272Sroot case PRU_LISTEN: 1148272Sroot if (inp->inp_lport == 0) 1158272Sroot error = in_pcbbind(inp, (struct mbuf *)0); 1168272Sroot if (error == 0) 1178272Sroot tp->t_state = TCPS_LISTEN; 1188272Sroot break; 1198272Sroot 1208272Sroot /* 1215280Sroot * Initiate connection to peer. 1225280Sroot * Create a template for use in transmissions on this connection. 1235280Sroot * Enter SYN_SENT state, and mark socket as connecting. 1245280Sroot * Start keep-alive timer, and seed output sequence space. 1255280Sroot * Send initial segment on connection. 1265280Sroot */ 1274809Swnj case PRU_CONNECT: 1288272Sroot if (inp->inp_lport == 0) { 1298272Sroot error = in_pcbbind(inp, (struct mbuf *)0); 1308272Sroot if (error) 1318272Sroot break; 1328272Sroot } 1338272Sroot error = in_pcbconnect(inp, nam); 1344954Swnj if (error) 1354886Swnj break; 1365174Swnj tp->t_template = tcp_template(tp); 1375280Sroot if (tp->t_template == 0) { 1385280Sroot in_pcbdisconnect(inp); 1395280Sroot error = ENOBUFS; 1405280Sroot break; 1415280Sroot } 1424886Swnj soisconnecting(so); 1435075Swnj tp->t_state = TCPS_SYN_SENT; 1445245Sroot tp->t_timer[TCPT_KEEP] = TCPTV_KEEP; 1455245Sroot tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2; 1465245Sroot tcp_sendseqinit(tp); 1476506Ssam error = tcp_output(tp); 1484567Swnj break; 1494497Swnj 1505280Sroot /* 1515280Sroot * Initiate disconnect from peer. 1525280Sroot * If connection never passed embryonic stage, just drop; 1535280Sroot * else if don't need to let data drain, then can just drop anyways, 1545280Sroot * else have to begin TCP shutdown process: mark socket disconnecting, 1555280Sroot * drain unread data, state switch to reflect user close, and 1565280Sroot * send segment (e.g. FIN) to peer. Socket will be really disconnected 1575280Sroot * when peer sends FIN and acks ours. 1585280Sroot * 1595280Sroot * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB. 1605280Sroot */ 1615280Sroot case PRU_DISCONNECT: 1625280Sroot tcp_disconnect(tp); 1635245Sroot break; 1645245Sroot 1655280Sroot /* 1665280Sroot * Accept a connection. Essentially all the work is 1675280Sroot * done at higher levels; just return the address 1685280Sroot * of the peer, storing through addr. 1695280Sroot */ 1706117Swnj case PRU_ACCEPT: { 1718272Sroot struct sockaddr_in *sin = mtod(nam, struct sockaddr_in *); 1726117Swnj 1738272Sroot nam->m_len = sizeof (struct sockaddr_in); 1748272Sroot sin->sin_family = AF_INET; 1758272Sroot sin->sin_port = inp->inp_fport; 1768272Sroot sin->sin_addr = inp->inp_faddr; 1778272Sroot break; 1786117Swnj } 1794925Swnj 1805280Sroot /* 1815280Sroot * Mark the connection as being incapable of further output. 1825280Sroot */ 1834809Swnj case PRU_SHUTDOWN: 1845089Swnj socantsendmore(so); 1855245Sroot tcp_usrclosed(tp); 1866506Ssam error = tcp_output(tp); 1874567Swnj break; 1884497Swnj 1895280Sroot /* 1905280Sroot * After a receive, possibly send window update to peer. 1915280Sroot */ 1924809Swnj case PRU_RCVD: 1935113Swnj (void) tcp_output(tp); 1944567Swnj break; 1954497Swnj 1965280Sroot /* 1975280Sroot * Do a send by putting data in output queue and updating urgent 1985280Sroot * marker if URG set. Possibly send more data. 1995280Sroot */ 2004809Swnj case PRU_SEND: 2015075Swnj sbappend(&so->so_snd, m); 2026506Ssam #ifdef notdef 2035089Swnj if (tp->t_flags & TF_PUSH) 2045075Swnj tp->snd_end = tp->snd_una + so->so_snd.sb_cc; 2056506Ssam #endif 2066506Ssam error = tcp_output(tp); 2074567Swnj break; 2084567Swnj 2095280Sroot /* 2105280Sroot * Abort the TCP. 2115280Sroot */ 2124809Swnj case PRU_ABORT: 2135075Swnj tcp_drop(tp, ECONNABORTED); 2144567Swnj break; 2154567Swnj 2165280Sroot /* SOME AS YET UNIMPLEMENTED HOOKS */ 2174809Swnj case PRU_CONTROL: 2184886Swnj error = EOPNOTSUPP; 2194809Swnj break; 2204809Swnj 2215113Swnj case PRU_SENSE: 2225113Swnj error = EOPNOTSUPP; 2235113Swnj break; 2245417Swnj /* END UNIMPLEMENTED HOOKS */ 2255113Swnj 2265113Swnj case PRU_RCVOOB: 2275442Swnj if (so->so_oobmark == 0 && 2285442Swnj (so->so_state & SS_RCVATMARK) == 0) { 2295417Swnj error = EINVAL; 2305417Swnj break; 2315417Swnj } 2325549Swnj if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) { 2335442Swnj error = EWOULDBLOCK; 2345549Swnj break; 2355442Swnj } 236*8310Sroot m->m_len = 1; 2375549Swnj *mtod(m, caddr_t) = tp->t_iobc; 2385113Swnj break; 2395113Swnj 2405113Swnj case PRU_SENDOOB: 2415549Swnj #ifdef TCPTRUEOOB 2425549Swnj if (tp->t_flags & TF_DOOOB) { 2435549Swnj tp->t_oobseq++; 2445549Swnj tp->t_oobc = *mtod(m, caddr_t); 2455549Swnj tp->t_oobmark = tp->snd_una + so->so_snd.sb_cc; 2465549Swnj tp->t_oobflags |= TCPOOB_NEEDACK; 2476506Ssam /* what to do ...? */ 2486506Ssam if (error = tcp_output(tp)) 2496506Ssam break; 2505549Swnj } 2515549Swnj #endif 2525442Swnj if (sbspace(&so->so_snd) < -512) { 2535442Swnj error = ENOBUFS; 2545442Swnj break; 2555442Swnj } 2565417Swnj tp->snd_up = tp->snd_una + so->so_snd.sb_cc + 1; 2575417Swnj sbappend(&so->so_snd, m); 2586506Ssam #ifdef notdef 2595417Swnj if (tp->t_flags & TF_PUSH) 2605417Swnj tp->snd_end = tp->snd_una + so->so_snd.sb_cc; 2616506Ssam #endif 2625549Swnj tp->t_force = 1; 2636506Ssam error = tcp_output(tp); 2645549Swnj tp->t_force = 0; 2655113Swnj break; 2665113Swnj 2676510Ssam case PRU_SOCKADDR: 2688272Sroot in_setsockaddr(inp, nam); 2696510Ssam break; 2706510Ssam 2715280Sroot /* 2725280Sroot * TCP slow timer went off; going through this 2735280Sroot * routine for tracing's sake. 2745280Sroot */ 2754809Swnj case PRU_SLOWTIMO: 2768272Sroot tcp_timers(tp, (int)nam); 2778272Sroot req |= (int)nam << 8; /* for debug's sake */ 2784809Swnj break; 2794809Swnj 2804731Swnj default: 2814731Swnj panic("tcp_usrreq"); 2824567Swnj } 2835270Sroot if (tp && (so->so_options & SO_DEBUG)) 2845270Sroot tcp_trace(TA_USER, ostate, tp, (struct tcpiphdr *)0, req); 2854567Swnj splx(s); 2864886Swnj return (error); 2874497Swnj } 2885245Sroot 2895953Swnj int tcp_sendspace = 1024*2; 2906601Ssam int tcp_recvspace = 1024*2; 2915280Sroot /* 2925280Sroot * Attach TCP protocol to socket, allocating 2935280Sroot * internet protocol control block, tcp control block, 2945280Sroot * bufer space, and entering LISTEN state if to accept connections. 2955280Sroot */ 2968272Sroot tcp_attach(so) 2975280Sroot struct socket *so; 2985280Sroot { 2995280Sroot register struct tcpcb *tp; 3005280Sroot struct inpcb *inp; 3015280Sroot int error; 3025280Sroot 3037511Sroot error = in_pcbreserve(so, tcp_sendspace, tcp_recvspace); 3045280Sroot if (error) 3057511Sroot goto bad; 3067511Sroot error = in_pcballoc(so, &tcb); 3077511Sroot if (error) 3088272Sroot goto bad; 3098272Sroot inp = sotoinpcb(so); 3105280Sroot tp = tcp_newtcpcb(inp); 3117511Sroot if (tp == 0) { 3127511Sroot error = ENOBUFS; 3137511Sroot goto bad2; 3147511Sroot } 3158272Sroot tp->t_state = TCPS_CLOSED; 3165280Sroot return (0); 3177511Sroot bad2: 3187511Sroot in_pcbdetach(inp); 3197511Sroot bad: 3207511Sroot return (error); 3215280Sroot } 3225280Sroot 3235280Sroot /* 3245280Sroot * Initiate (or continue) disconnect. 3255280Sroot * If embryonic state, just send reset (once). 3265280Sroot * If not in ``let data drain'' option, just drop. 3275280Sroot * Otherwise (hard), mark socket disconnecting and drop 3285280Sroot * current input data; switch states based on user close, and 3295280Sroot * send segment to peer (with FIN). 3305280Sroot */ 3315280Sroot tcp_disconnect(tp) 3325280Sroot struct tcpcb *tp; 3335280Sroot { 3345280Sroot struct socket *so = tp->t_inpcb->inp_socket; 3355280Sroot 3365280Sroot if (tp->t_state < TCPS_ESTABLISHED) 3375280Sroot tcp_close(tp); 3385392Swnj else if (so->so_linger == 0) 3395280Sroot tcp_drop(tp, 0); 3405280Sroot else { 3415280Sroot soisdisconnecting(so); 3425280Sroot sbflush(&so->so_rcv); 3435280Sroot tcp_usrclosed(tp); 3445280Sroot (void) tcp_output(tp); 3455280Sroot } 3465280Sroot } 3475280Sroot 3485280Sroot /* 3495280Sroot * User issued close, and wish to trail through shutdown states: 3505280Sroot * if never received SYN, just forget it. If got a SYN from peer, 3515280Sroot * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN. 3525280Sroot * If already got a FIN from peer, then almost done; go to LAST_ACK 3535280Sroot * state. In all other cases, have already sent FIN to peer (e.g. 3545280Sroot * after PRU_SHUTDOWN), and just have to play tedious game waiting 3555280Sroot * for peer to send FIN or not respond to keep-alives, etc. 3565897Swnj * We can let the user exit from the close as soon as the FIN is acked. 3575280Sroot */ 3585245Sroot tcp_usrclosed(tp) 3595245Sroot struct tcpcb *tp; 3605245Sroot { 3615245Sroot 3625245Sroot switch (tp->t_state) { 3635245Sroot 3645245Sroot case TCPS_LISTEN: 3655245Sroot case TCPS_SYN_SENT: 3665245Sroot tp->t_state = TCPS_CLOSED; 3675245Sroot tcp_close(tp); 3685245Sroot break; 3695245Sroot 3705245Sroot case TCPS_SYN_RECEIVED: 3715245Sroot case TCPS_ESTABLISHED: 3725245Sroot tp->t_state = TCPS_FIN_WAIT_1; 3735245Sroot break; 3745245Sroot 3755245Sroot case TCPS_CLOSE_WAIT: 3765245Sroot tp->t_state = TCPS_LAST_ACK; 3775245Sroot break; 3785245Sroot } 3795897Swnj if (tp->t_state >= TCPS_FIN_WAIT_2) 3805897Swnj soisdisconnected(tp->t_inpcb->inp_socket); 3815245Sroot } 382