1*12438Ssam /* tcp_usrreq.c 1.77 83/05/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" 910896Ssam #include "../h/errno.h" 108697Sroot 118697Sroot #include "../net/if.h" 128697Sroot #include "../net/route.h" 1310896Ssam 148406Swnj #include "../netinet/in.h" 158406Swnj #include "../netinet/in_pcb.h" 168406Swnj #include "../netinet/in_systm.h" 178406Swnj #include "../netinet/ip.h" 188406Swnj #include "../netinet/ip_var.h" 198406Swnj #include "../netinet/tcp.h" 208406Swnj #include "../netinet/tcp_fsm.h" 218406Swnj #include "../netinet/tcp_seq.h" 228406Swnj #include "../netinet/tcp_timer.h" 238406Swnj #include "../netinet/tcp_var.h" 248406Swnj #include "../netinet/tcpip.h" 258406Swnj #include "../netinet/tcp_debug.h" 264497Swnj 275280Sroot /* 285280Sroot * TCP protocol interface to socket abstraction. 295280Sroot */ 305280Sroot extern char *tcpstates[]; 314954Swnj struct tcpcb *tcp_newtcpcb(); 325280Sroot 334734Swnj /* 345280Sroot * Process a TCP user request for TCP tb. If this is a send request 354731Swnj * then m is the mbuf chain of send data. If this is a timer expiration 364731Swnj * (called from the software clock routine), then timertype tells which timer. 374731Swnj */ 388601Sroot /*ARGSUSED*/ 3910261Ssam tcp_usrreq(so, req, m, nam) 404809Swnj struct socket *so; 414809Swnj int req; 428272Sroot struct mbuf *m, *nam; 434497Swnj { 444886Swnj register struct inpcb *inp = sotoinpcb(so); 454911Swnj register struct tcpcb *tp; 464567Swnj int s = splnet(); 474809Swnj int error = 0; 485270Sroot int ostate; 494497Swnj 504886Swnj /* 515280Sroot * When a TCP is attached to a socket, then there will be 525280Sroot * a (struct inpcb) pointed at by the socket, and this 535280Sroot * structure will point at a subsidary (struct tcpcb). 544886Swnj */ 555089Swnj if (inp == 0 && req != PRU_ATTACH) { 565075Swnj splx(s); 575280Sroot return (EINVAL); /* XXX */ 585075Swnj } 595075Swnj if (inp) { 604911Swnj tp = intotcpcb(inp); 618272Sroot /* WHAT IF TP IS 0? */ 624731Swnj #ifdef KPROF 635075Swnj tcp_acounts[tp->t_state][req]++; 644731Swnj #endif 655270Sroot ostate = tp->t_state; 667511Sroot } else 677511Sroot ostate = 0; 684809Swnj switch (req) { 694497Swnj 705280Sroot /* 715280Sroot * TCP attaches to socket via PRU_ATTACH, reserving space, 728272Sroot * and an internet control block. 735280Sroot */ 744809Swnj case PRU_ATTACH: 754954Swnj if (inp) { 764809Swnj error = EISCONN; 774911Swnj break; 784886Swnj } 798640Sroot error = tcp_attach(so); 805075Swnj if (error) 814954Swnj break; 8210397Ssam if ((so->so_options & SO_LINGER) && so->so_linger == 0) 835392Swnj so->so_linger = TCP_LINGERTIME; 845280Sroot tp = sototcpcb(so); 854567Swnj break; 864497Swnj 875280Sroot /* 885280Sroot * PRU_DETACH detaches the TCP protocol from the socket. 895280Sroot * If the protocol state is non-embryonic, then can't 905280Sroot * do this directly: have to initiate a PRU_DISCONNECT, 915280Sroot * which may finish later; embryonic TCB's can just 925280Sroot * be discarded here. 935280Sroot */ 944809Swnj case PRU_DETACH: 955280Sroot if (tp->t_state > TCPS_LISTEN) 9610397Ssam tp = tcp_disconnect(tp); 9710397Ssam else 9810397Ssam tp = tcp_close(tp); 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: 16210397Ssam tp = 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); 18510397Ssam tp = tcp_usrclosed(tp); 18610397Ssam if (tp) 18710397Ssam 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); 20812415Ssam if (error == ENOBUFS) /* XXX */ 20912415Ssam error = 0; /* XXX */ 2104567Swnj break; 2114567Swnj 2125280Sroot /* 2135280Sroot * Abort the TCP. 2145280Sroot */ 2154809Swnj case PRU_ABORT: 21610397Ssam tp = tcp_drop(tp, ECONNABORTED); 2174567Swnj break; 2184567Swnj 2195280Sroot /* SOME AS YET UNIMPLEMENTED HOOKS */ 2204809Swnj case PRU_CONTROL: 2214886Swnj error = EOPNOTSUPP; 2224809Swnj break; 2234809Swnj 2245113Swnj case PRU_SENSE: 2255113Swnj error = EOPNOTSUPP; 2265113Swnj break; 2275417Swnj /* END UNIMPLEMENTED HOOKS */ 2285113Swnj 2295113Swnj case PRU_RCVOOB: 2305442Swnj if (so->so_oobmark == 0 && 2315442Swnj (so->so_state & SS_RCVATMARK) == 0) { 2325417Swnj error = EINVAL; 2335417Swnj break; 2345417Swnj } 2355549Swnj if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) { 2365442Swnj error = EWOULDBLOCK; 2375549Swnj break; 2385442Swnj } 2398310Sroot m->m_len = 1; 2405549Swnj *mtod(m, caddr_t) = tp->t_iobc; 2415113Swnj break; 2425113Swnj 2435113Swnj case PRU_SENDOOB: 2445442Swnj if (sbspace(&so->so_snd) < -512) { 24511229Ssam m_freem(m); 2465442Swnj error = ENOBUFS; 2475442Swnj break; 2485442Swnj } 2495417Swnj tp->snd_up = tp->snd_una + so->so_snd.sb_cc + 1; 2505417Swnj sbappend(&so->so_snd, m); 2515549Swnj tp->t_force = 1; 2526506Ssam error = tcp_output(tp); 2535549Swnj tp->t_force = 0; 2545113Swnj break; 2555113Swnj 2566510Ssam case PRU_SOCKADDR: 2578272Sroot in_setsockaddr(inp, nam); 2586510Ssam break; 2596510Ssam 2605280Sroot /* 2615280Sroot * TCP slow timer went off; going through this 2625280Sroot * routine for tracing's sake. 2635280Sroot */ 2644809Swnj case PRU_SLOWTIMO: 26510397Ssam tp = tcp_timers(tp, (int)nam); 2668272Sroot req |= (int)nam << 8; /* for debug's sake */ 2674809Swnj break; 2684809Swnj 2694731Swnj default: 2704731Swnj panic("tcp_usrreq"); 2714567Swnj } 2725270Sroot if (tp && (so->so_options & SO_DEBUG)) 2735270Sroot tcp_trace(TA_USER, ostate, tp, (struct tcpiphdr *)0, req); 2744567Swnj splx(s); 2754886Swnj return (error); 2764497Swnj } 2775245Sroot 2785953Swnj int tcp_sendspace = 1024*2; 2796601Ssam int tcp_recvspace = 1024*2; 2805280Sroot /* 2815280Sroot * Attach TCP protocol to socket, allocating 2825280Sroot * internet protocol control block, tcp control block, 2835280Sroot * bufer space, and entering LISTEN state if to accept connections. 2845280Sroot */ 2858272Sroot tcp_attach(so) 2865280Sroot struct socket *so; 2875280Sroot { 2885280Sroot register struct tcpcb *tp; 2895280Sroot struct inpcb *inp; 2905280Sroot int error; 2915280Sroot 2929031Sroot error = soreserve(so, tcp_sendspace, tcp_recvspace); 2935280Sroot if (error) 2947511Sroot goto bad; 2957511Sroot error = in_pcballoc(so, &tcb); 2967511Sroot if (error) 2978272Sroot goto bad; 2988272Sroot inp = sotoinpcb(so); 2995280Sroot tp = tcp_newtcpcb(inp); 3007511Sroot if (tp == 0) { 3017511Sroot error = ENOBUFS; 3027511Sroot goto bad2; 3037511Sroot } 3048272Sroot tp->t_state = TCPS_CLOSED; 3055280Sroot return (0); 3067511Sroot bad2: 3077511Sroot in_pcbdetach(inp); 3087511Sroot bad: 3097511Sroot return (error); 3105280Sroot } 3115280Sroot 3125280Sroot /* 3135280Sroot * Initiate (or continue) disconnect. 3145280Sroot * If embryonic state, just send reset (once). 3155280Sroot * If not in ``let data drain'' option, just drop. 3165280Sroot * Otherwise (hard), mark socket disconnecting and drop 3175280Sroot * current input data; switch states based on user close, and 3185280Sroot * send segment to peer (with FIN). 3195280Sroot */ 32010397Ssam struct tcpcb * 3215280Sroot tcp_disconnect(tp) 32210397Ssam register struct tcpcb *tp; 3235280Sroot { 3245280Sroot struct socket *so = tp->t_inpcb->inp_socket; 3255280Sroot 3265280Sroot if (tp->t_state < TCPS_ESTABLISHED) 32710397Ssam tp = tcp_close(tp); 3285392Swnj else if (so->so_linger == 0) 32910397Ssam tp = tcp_drop(tp, 0); 3305280Sroot else { 3315280Sroot soisdisconnecting(so); 3325280Sroot sbflush(&so->so_rcv); 33310397Ssam tp = tcp_usrclosed(tp); 33410397Ssam if (tp) 33510397Ssam (void) tcp_output(tp); 3365280Sroot } 33710397Ssam return (tp); 3385280Sroot } 3395280Sroot 3405280Sroot /* 3415280Sroot * User issued close, and wish to trail through shutdown states: 3425280Sroot * if never received SYN, just forget it. If got a SYN from peer, 3435280Sroot * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN. 3445280Sroot * If already got a FIN from peer, then almost done; go to LAST_ACK 3455280Sroot * state. In all other cases, have already sent FIN to peer (e.g. 3465280Sroot * after PRU_SHUTDOWN), and just have to play tedious game waiting 3475280Sroot * for peer to send FIN or not respond to keep-alives, etc. 3485897Swnj * We can let the user exit from the close as soon as the FIN is acked. 3495280Sroot */ 35010397Ssam struct tcpcb * 3515245Sroot tcp_usrclosed(tp) 35210397Ssam register struct tcpcb *tp; 3535245Sroot { 3545245Sroot 3555245Sroot switch (tp->t_state) { 3565245Sroot 357*12438Ssam case TCPS_CLOSED: 3585245Sroot case TCPS_LISTEN: 3595245Sroot case TCPS_SYN_SENT: 3605245Sroot tp->t_state = TCPS_CLOSED; 36110397Ssam tp = tcp_close(tp); 3625245Sroot break; 3635245Sroot 3645245Sroot case TCPS_SYN_RECEIVED: 3655245Sroot case TCPS_ESTABLISHED: 3665245Sroot tp->t_state = TCPS_FIN_WAIT_1; 3675245Sroot break; 3685245Sroot 3695245Sroot case TCPS_CLOSE_WAIT: 3705245Sroot tp->t_state = TCPS_LAST_ACK; 3715245Sroot break; 3725245Sroot } 37310397Ssam if (tp && tp->t_state >= TCPS_FIN_WAIT_2) 3745897Swnj soisdisconnected(tp->t_inpcb->inp_socket); 37510397Ssam return (tp); 3765245Sroot } 377