1*18367Skarels /* tcp_usrreq.c 6.5 85/03/18 */ 24567Swnj 317064Sbloom #include "param.h" 417064Sbloom #include "systm.h" 517064Sbloom #include "mbuf.h" 617064Sbloom #include "socket.h" 717064Sbloom #include "socketvar.h" 817064Sbloom #include "protosw.h" 917064Sbloom #include "errno.h" 1017064Sbloom #include "stat.h" 118697Sroot 128697Sroot #include "../net/if.h" 138697Sroot #include "../net/route.h" 1410896Ssam 1517064Sbloom #include "in.h" 1617064Sbloom #include "in_pcb.h" 1717064Sbloom #include "in_systm.h" 1817064Sbloom #include "ip.h" 1917064Sbloom #include "ip_var.h" 2017064Sbloom #include "tcp.h" 2117064Sbloom #include "tcp_fsm.h" 2217064Sbloom #include "tcp_seq.h" 2317064Sbloom #include "tcp_timer.h" 2417064Sbloom #include "tcp_var.h" 2517064Sbloom #include "tcpip.h" 2617064Sbloom #include "tcp_debug.h" 274497Swnj 285280Sroot /* 295280Sroot * TCP protocol interface to socket abstraction. 305280Sroot */ 315280Sroot extern char *tcpstates[]; 324954Swnj struct tcpcb *tcp_newtcpcb(); 3312766Ssam int tcpsenderrors; 345280Sroot 354734Swnj /* 365280Sroot * Process a TCP user request for TCP tb. If this is a send request 374731Swnj * then m is the mbuf chain of send data. If this is a timer expiration 384731Swnj * (called from the software clock routine), then timertype tells which timer. 394731Swnj */ 408601Sroot /*ARGSUSED*/ 4112766Ssam tcp_usrreq(so, req, m, nam, rights) 424809Swnj struct socket *so; 434809Swnj int req; 4412766Ssam struct mbuf *m, *nam, *rights; 454497Swnj { 464886Swnj register struct inpcb *inp = sotoinpcb(so); 474911Swnj register struct tcpcb *tp; 484567Swnj int s = splnet(); 494809Swnj int error = 0; 505270Sroot int ostate; 514497Swnj 52*18367Skarels if (req == PRU_CONTROL) 53*18367Skarels return (in_control(so, (int)m, (caddr_t)nam, 54*18367Skarels (struct ifnet *)rights)); 5512766Ssam if (rights && rights->m_len) { 5612766Ssam splx(s); 5712766Ssam return (EINVAL); 5812766Ssam } 594886Swnj /* 605280Sroot * When a TCP is attached to a socket, then there will be 615280Sroot * a (struct inpcb) pointed at by the socket, and this 625280Sroot * structure will point at a subsidary (struct tcpcb). 634886Swnj */ 645089Swnj if (inp == 0 && req != PRU_ATTACH) { 655075Swnj splx(s); 665280Sroot return (EINVAL); /* XXX */ 675075Swnj } 685075Swnj if (inp) { 694911Swnj tp = intotcpcb(inp); 708272Sroot /* WHAT IF TP IS 0? */ 714731Swnj #ifdef KPROF 725075Swnj tcp_acounts[tp->t_state][req]++; 734731Swnj #endif 745270Sroot ostate = tp->t_state; 757511Sroot } else 767511Sroot ostate = 0; 774809Swnj switch (req) { 784497Swnj 795280Sroot /* 805280Sroot * TCP attaches to socket via PRU_ATTACH, reserving space, 818272Sroot * and an internet control block. 825280Sroot */ 834809Swnj case PRU_ATTACH: 844954Swnj if (inp) { 854809Swnj error = EISCONN; 864911Swnj break; 874886Swnj } 888640Sroot error = tcp_attach(so); 895075Swnj if (error) 904954Swnj break; 9110397Ssam if ((so->so_options & SO_LINGER) && so->so_linger == 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) 10510397Ssam tp = tcp_disconnect(tp); 10610397Ssam else 10710397Ssam tp = tcp_close(tp); 1084809Swnj break; 1094809Swnj 1105280Sroot /* 1118272Sroot * Give the socket an address. 1128272Sroot */ 1138272Sroot case PRU_BIND: 1148272Sroot error = in_pcbbind(inp, nam); 1158272Sroot if (error) 1168272Sroot break; 1178272Sroot break; 1188272Sroot 1198272Sroot /* 1208272Sroot * Prepare to accept connections. 1218272Sroot */ 1228272Sroot case PRU_LISTEN: 1238272Sroot if (inp->inp_lport == 0) 1248272Sroot error = in_pcbbind(inp, (struct mbuf *)0); 1258272Sroot if (error == 0) 1268272Sroot tp->t_state = TCPS_LISTEN; 1278272Sroot break; 1288272Sroot 1298272Sroot /* 1305280Sroot * Initiate connection to peer. 1315280Sroot * Create a template for use in transmissions on this connection. 1325280Sroot * Enter SYN_SENT state, and mark socket as connecting. 1335280Sroot * Start keep-alive timer, and seed output sequence space. 1345280Sroot * Send initial segment on connection. 1355280Sroot */ 1364809Swnj case PRU_CONNECT: 1378272Sroot if (inp->inp_lport == 0) { 1388272Sroot error = in_pcbbind(inp, (struct mbuf *)0); 1398272Sroot if (error) 1408272Sroot break; 1418272Sroot } 1428272Sroot error = in_pcbconnect(inp, nam); 1434954Swnj if (error) 1444886Swnj break; 1455174Swnj tp->t_template = tcp_template(tp); 1465280Sroot if (tp->t_template == 0) { 1475280Sroot in_pcbdisconnect(inp); 1485280Sroot error = ENOBUFS; 1495280Sroot break; 1505280Sroot } 1514886Swnj soisconnecting(so); 1525075Swnj tp->t_state = TCPS_SYN_SENT; 1535245Sroot tp->t_timer[TCPT_KEEP] = TCPTV_KEEP; 1545245Sroot tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2; 1555245Sroot tcp_sendseqinit(tp); 1566506Ssam error = tcp_output(tp); 1574567Swnj break; 1584497Swnj 1595280Sroot /* 16013117Ssam * Create a TCP connection between two sockets. 16113117Ssam */ 16213117Ssam case PRU_CONNECT2: 16313117Ssam error = EOPNOTSUPP; 16413117Ssam break; 16513117Ssam 16613117Ssam /* 1675280Sroot * Initiate disconnect from peer. 1685280Sroot * If connection never passed embryonic stage, just drop; 1695280Sroot * else if don't need to let data drain, then can just drop anyways, 1705280Sroot * else have to begin TCP shutdown process: mark socket disconnecting, 1715280Sroot * drain unread data, state switch to reflect user close, and 1725280Sroot * send segment (e.g. FIN) to peer. Socket will be really disconnected 1735280Sroot * when peer sends FIN and acks ours. 1745280Sroot * 1755280Sroot * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB. 1765280Sroot */ 1775280Sroot case PRU_DISCONNECT: 17810397Ssam tp = tcp_disconnect(tp); 1795245Sroot break; 1805245Sroot 1815280Sroot /* 1825280Sroot * Accept a connection. Essentially all the work is 1835280Sroot * done at higher levels; just return the address 1845280Sroot * of the peer, storing through addr. 1855280Sroot */ 1866117Swnj case PRU_ACCEPT: { 1878272Sroot struct sockaddr_in *sin = mtod(nam, struct sockaddr_in *); 1886117Swnj 1898272Sroot nam->m_len = sizeof (struct sockaddr_in); 1908272Sroot sin->sin_family = AF_INET; 1918272Sroot sin->sin_port = inp->inp_fport; 1928272Sroot sin->sin_addr = inp->inp_faddr; 1938272Sroot break; 1946117Swnj } 1954925Swnj 1965280Sroot /* 1975280Sroot * Mark the connection as being incapable of further output. 1985280Sroot */ 1994809Swnj case PRU_SHUTDOWN: 2005089Swnj socantsendmore(so); 20110397Ssam tp = tcp_usrclosed(tp); 20210397Ssam if (tp) 20310397Ssam error = tcp_output(tp); 2044567Swnj break; 2054497Swnj 2065280Sroot /* 2075280Sroot * After a receive, possibly send window update to peer. 2085280Sroot */ 2094809Swnj case PRU_RCVD: 2105113Swnj (void) tcp_output(tp); 2114567Swnj break; 2124497Swnj 2135280Sroot /* 2145280Sroot * Do a send by putting data in output queue and updating urgent 2155280Sroot * marker if URG set. Possibly send more data. 2165280Sroot */ 2174809Swnj case PRU_SEND: 2185075Swnj sbappend(&so->so_snd, m); 2196506Ssam #ifdef notdef 2205089Swnj if (tp->t_flags & TF_PUSH) 2215075Swnj tp->snd_end = tp->snd_una + so->so_snd.sb_cc; 2226506Ssam #endif 2236506Ssam error = tcp_output(tp); 22412766Ssam if (error) { /* XXX fix to use other path */ 22512766Ssam if (error == ENOBUFS) /* XXX */ 22612766Ssam error = 0; /* XXX */ 22712766Ssam tcpsenderrors++; 22812766Ssam } 2294567Swnj break; 2304567Swnj 2315280Sroot /* 2325280Sroot * Abort the TCP. 2335280Sroot */ 2344809Swnj case PRU_ABORT: 23510397Ssam tp = tcp_drop(tp, ECONNABORTED); 2364567Swnj break; 2374567Swnj 2385113Swnj case PRU_SENSE: 23916989Skarels ((struct stat *) m)->st_blksize = so->so_snd.sb_hiwat; 24016989Skarels return (0); 2415113Swnj 2425113Swnj case PRU_RCVOOB: 2435442Swnj if (so->so_oobmark == 0 && 2445442Swnj (so->so_state & SS_RCVATMARK) == 0) { 2455417Swnj error = EINVAL; 2465417Swnj break; 2475417Swnj } 2485549Swnj if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) { 2495442Swnj error = EWOULDBLOCK; 2505549Swnj break; 2515442Swnj } 2528310Sroot m->m_len = 1; 2535549Swnj *mtod(m, caddr_t) = tp->t_iobc; 2545113Swnj break; 2555113Swnj 2565113Swnj case PRU_SENDOOB: 2575442Swnj if (sbspace(&so->so_snd) < -512) { 25811229Ssam m_freem(m); 2595442Swnj error = ENOBUFS; 2605442Swnj break; 2615442Swnj } 2625417Swnj tp->snd_up = tp->snd_una + so->so_snd.sb_cc + 1; 2635417Swnj sbappend(&so->so_snd, m); 2645549Swnj tp->t_force = 1; 2656506Ssam error = tcp_output(tp); 2665549Swnj tp->t_force = 0; 2675113Swnj break; 2685113Swnj 2696510Ssam case PRU_SOCKADDR: 2708272Sroot in_setsockaddr(inp, nam); 2716510Ssam break; 2726510Ssam 27314123Ssam case PRU_PEERADDR: 27414123Ssam in_setpeeraddr(inp, nam); 27514123Ssam break; 27614123Ssam 2775280Sroot /* 2785280Sroot * TCP slow timer went off; going through this 2795280Sroot * routine for tracing's sake. 2805280Sroot */ 2814809Swnj case PRU_SLOWTIMO: 28210397Ssam tp = tcp_timers(tp, (int)nam); 2838272Sroot req |= (int)nam << 8; /* for debug's sake */ 2844809Swnj break; 2854809Swnj 2864731Swnj default: 2874731Swnj panic("tcp_usrreq"); 2884567Swnj } 2895270Sroot if (tp && (so->so_options & SO_DEBUG)) 2905270Sroot tcp_trace(TA_USER, ostate, tp, (struct tcpiphdr *)0, req); 2914567Swnj splx(s); 2924886Swnj return (error); 2934497Swnj } 2945245Sroot 295*18367Skarels int tcp_sendspace = 1024*4; 296*18367Skarels int tcp_recvspace = 1024*4; 2975280Sroot /* 2985280Sroot * Attach TCP protocol to socket, allocating 2995280Sroot * internet protocol control block, tcp control block, 3005280Sroot * bufer space, and entering LISTEN state if to accept connections. 3015280Sroot */ 3028272Sroot tcp_attach(so) 3035280Sroot struct socket *so; 3045280Sroot { 3055280Sroot register struct tcpcb *tp; 3065280Sroot struct inpcb *inp; 3075280Sroot int error; 3085280Sroot 3099031Sroot error = soreserve(so, tcp_sendspace, tcp_recvspace); 3105280Sroot if (error) 31117047Skarels return (error); 3127511Sroot error = in_pcballoc(so, &tcb); 3137511Sroot if (error) 31417047Skarels return (error); 3158272Sroot inp = sotoinpcb(so); 3165280Sroot tp = tcp_newtcpcb(inp); 3177511Sroot if (tp == 0) { 31817047Skarels int nofd = so->so_state & SS_NOFDREF; /* XXX */ 31917047Skarels 32017047Skarels so->so_state &= ~SS_NOFDREF; /* don't free the socket yet */ 32117047Skarels in_pcbdetach(inp); 32217047Skarels so->so_state |= nofd; 32317047Skarels return (ENOBUFS); 3247511Sroot } 3258272Sroot tp->t_state = TCPS_CLOSED; 3265280Sroot return (0); 3275280Sroot } 3285280Sroot 3295280Sroot /* 3305280Sroot * Initiate (or continue) disconnect. 3315280Sroot * If embryonic state, just send reset (once). 33213221Ssam * If in ``let data drain'' option and linger null, just drop. 3335280Sroot * Otherwise (hard), mark socket disconnecting and drop 3345280Sroot * current input data; switch states based on user close, and 3355280Sroot * send segment to peer (with FIN). 3365280Sroot */ 33710397Ssam struct tcpcb * 3385280Sroot tcp_disconnect(tp) 33910397Ssam register struct tcpcb *tp; 3405280Sroot { 3415280Sroot struct socket *so = tp->t_inpcb->inp_socket; 3425280Sroot 3435280Sroot if (tp->t_state < TCPS_ESTABLISHED) 34410397Ssam tp = tcp_close(tp); 34513221Ssam else if ((so->so_options & SO_LINGER) && so->so_linger == 0) 34610397Ssam tp = tcp_drop(tp, 0); 3475280Sroot else { 3485280Sroot soisdisconnecting(so); 3495280Sroot sbflush(&so->so_rcv); 35010397Ssam tp = tcp_usrclosed(tp); 35110397Ssam if (tp) 35210397Ssam (void) tcp_output(tp); 3535280Sroot } 35410397Ssam return (tp); 3555280Sroot } 3565280Sroot 3575280Sroot /* 3585280Sroot * User issued close, and wish to trail through shutdown states: 3595280Sroot * if never received SYN, just forget it. If got a SYN from peer, 3605280Sroot * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN. 3615280Sroot * If already got a FIN from peer, then almost done; go to LAST_ACK 3625280Sroot * state. In all other cases, have already sent FIN to peer (e.g. 3635280Sroot * after PRU_SHUTDOWN), and just have to play tedious game waiting 3645280Sroot * for peer to send FIN or not respond to keep-alives, etc. 3655897Swnj * We can let the user exit from the close as soon as the FIN is acked. 3665280Sroot */ 36710397Ssam struct tcpcb * 3685245Sroot tcp_usrclosed(tp) 36910397Ssam register struct tcpcb *tp; 3705245Sroot { 3715245Sroot 3725245Sroot switch (tp->t_state) { 3735245Sroot 37412438Ssam case TCPS_CLOSED: 3755245Sroot case TCPS_LISTEN: 3765245Sroot case TCPS_SYN_SENT: 3775245Sroot tp->t_state = TCPS_CLOSED; 37810397Ssam tp = tcp_close(tp); 3795245Sroot break; 3805245Sroot 3815245Sroot case TCPS_SYN_RECEIVED: 3825245Sroot case TCPS_ESTABLISHED: 3835245Sroot tp->t_state = TCPS_FIN_WAIT_1; 3845245Sroot break; 3855245Sroot 3865245Sroot case TCPS_CLOSE_WAIT: 3875245Sroot tp->t_state = TCPS_LAST_ACK; 3885245Sroot break; 3895245Sroot } 39010397Ssam if (tp && tp->t_state >= TCPS_FIN_WAIT_2) 3915897Swnj soisdisconnected(tp->t_inpcb->inp_socket); 39210397Ssam return (tp); 3935245Sroot } 394