1*11229Ssam /* tcp_usrreq.c 1.75 83/02/22 */ 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); 2084567Swnj break; 2094567Swnj 2105280Sroot /* 2115280Sroot * Abort the TCP. 2125280Sroot */ 2134809Swnj case PRU_ABORT: 21410397Ssam tp = 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) { 243*11229Ssam m_freem(m); 2445442Swnj error = ENOBUFS; 2455442Swnj break; 2465442Swnj } 2475417Swnj tp->snd_up = tp->snd_una + so->so_snd.sb_cc + 1; 2485417Swnj sbappend(&so->so_snd, m); 2495549Swnj tp->t_force = 1; 2506506Ssam error = tcp_output(tp); 2515549Swnj tp->t_force = 0; 2525113Swnj break; 2535113Swnj 2546510Ssam case PRU_SOCKADDR: 2558272Sroot in_setsockaddr(inp, nam); 2566510Ssam break; 2576510Ssam 2585280Sroot /* 2595280Sroot * TCP slow timer went off; going through this 2605280Sroot * routine for tracing's sake. 2615280Sroot */ 2624809Swnj case PRU_SLOWTIMO: 26310397Ssam tp = tcp_timers(tp, (int)nam); 2648272Sroot req |= (int)nam << 8; /* for debug's sake */ 2654809Swnj break; 2664809Swnj 2674731Swnj default: 2684731Swnj panic("tcp_usrreq"); 2694567Swnj } 2705270Sroot if (tp && (so->so_options & SO_DEBUG)) 2715270Sroot tcp_trace(TA_USER, ostate, tp, (struct tcpiphdr *)0, req); 2724567Swnj splx(s); 2734886Swnj return (error); 2744497Swnj } 2755245Sroot 2765953Swnj int tcp_sendspace = 1024*2; 2776601Ssam int tcp_recvspace = 1024*2; 2785280Sroot /* 2795280Sroot * Attach TCP protocol to socket, allocating 2805280Sroot * internet protocol control block, tcp control block, 2815280Sroot * bufer space, and entering LISTEN state if to accept connections. 2825280Sroot */ 2838272Sroot tcp_attach(so) 2845280Sroot struct socket *so; 2855280Sroot { 2865280Sroot register struct tcpcb *tp; 2875280Sroot struct inpcb *inp; 2885280Sroot int error; 2895280Sroot 2909031Sroot error = soreserve(so, tcp_sendspace, tcp_recvspace); 2915280Sroot if (error) 2927511Sroot goto bad; 2937511Sroot error = in_pcballoc(so, &tcb); 2947511Sroot if (error) 2958272Sroot goto bad; 2968272Sroot inp = sotoinpcb(so); 2975280Sroot tp = tcp_newtcpcb(inp); 2987511Sroot if (tp == 0) { 2997511Sroot error = ENOBUFS; 3007511Sroot goto bad2; 3017511Sroot } 3028272Sroot tp->t_state = TCPS_CLOSED; 3035280Sroot return (0); 3047511Sroot bad2: 3057511Sroot in_pcbdetach(inp); 3067511Sroot bad: 3077511Sroot return (error); 3085280Sroot } 3095280Sroot 3105280Sroot /* 3115280Sroot * Initiate (or continue) disconnect. 3125280Sroot * If embryonic state, just send reset (once). 3135280Sroot * If not in ``let data drain'' option, just drop. 3145280Sroot * Otherwise (hard), mark socket disconnecting and drop 3155280Sroot * current input data; switch states based on user close, and 3165280Sroot * send segment to peer (with FIN). 3175280Sroot */ 31810397Ssam struct tcpcb * 3195280Sroot tcp_disconnect(tp) 32010397Ssam register struct tcpcb *tp; 3215280Sroot { 3225280Sroot struct socket *so = tp->t_inpcb->inp_socket; 3235280Sroot 3245280Sroot if (tp->t_state < TCPS_ESTABLISHED) 32510397Ssam tp = tcp_close(tp); 3265392Swnj else if (so->so_linger == 0) 32710397Ssam tp = tcp_drop(tp, 0); 3285280Sroot else { 3295280Sroot soisdisconnecting(so); 3305280Sroot sbflush(&so->so_rcv); 33110397Ssam tp = tcp_usrclosed(tp); 33210397Ssam if (tp) 33310397Ssam (void) tcp_output(tp); 3345280Sroot } 33510397Ssam return (tp); 3365280Sroot } 3375280Sroot 3385280Sroot /* 3395280Sroot * User issued close, and wish to trail through shutdown states: 3405280Sroot * if never received SYN, just forget it. If got a SYN from peer, 3415280Sroot * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN. 3425280Sroot * If already got a FIN from peer, then almost done; go to LAST_ACK 3435280Sroot * state. In all other cases, have already sent FIN to peer (e.g. 3445280Sroot * after PRU_SHUTDOWN), and just have to play tedious game waiting 3455280Sroot * for peer to send FIN or not respond to keep-alives, etc. 3465897Swnj * We can let the user exit from the close as soon as the FIN is acked. 3475280Sroot */ 34810397Ssam struct tcpcb * 3495245Sroot tcp_usrclosed(tp) 35010397Ssam register struct tcpcb *tp; 3515245Sroot { 3525245Sroot 3535245Sroot switch (tp->t_state) { 3545245Sroot 3555245Sroot case TCPS_LISTEN: 3565245Sroot case TCPS_SYN_SENT: 3575245Sroot tp->t_state = TCPS_CLOSED; 35810397Ssam tp = tcp_close(tp); 3595245Sroot break; 3605245Sroot 3615245Sroot case TCPS_SYN_RECEIVED: 3625245Sroot case TCPS_ESTABLISHED: 3635245Sroot tp->t_state = TCPS_FIN_WAIT_1; 3645245Sroot break; 3655245Sroot 3665245Sroot case TCPS_CLOSE_WAIT: 3675245Sroot tp->t_state = TCPS_LAST_ACK; 3685245Sroot break; 3695245Sroot } 37010397Ssam if (tp && tp->t_state >= TCPS_FIN_WAIT_2) 3715897Swnj soisdisconnected(tp->t_inpcb->inp_socket); 37210397Ssam return (tp); 3735245Sroot } 374