123196Smckusick /* 229155Smckusick * Copyright (c) 1982, 1986 Regents of the University of California. 323196Smckusick * All rights reserved. The Berkeley software License Agreement 423196Smckusick * specifies the terms and conditions for redistribution. 523196Smckusick * 6*30909Skarels * @(#)tcp_usrreq.c 7.4 (Berkeley) 04/15/87 723196Smckusick */ 84567Swnj 917064Sbloom #include "param.h" 1017064Sbloom #include "systm.h" 1117064Sbloom #include "mbuf.h" 1217064Sbloom #include "socket.h" 1317064Sbloom #include "socketvar.h" 1417064Sbloom #include "protosw.h" 1517064Sbloom #include "errno.h" 1617064Sbloom #include "stat.h" 178697Sroot 188697Sroot #include "../net/if.h" 198697Sroot #include "../net/route.h" 2010896Ssam 2117064Sbloom #include "in.h" 2217064Sbloom #include "in_pcb.h" 2317064Sbloom #include "in_systm.h" 2417064Sbloom #include "ip.h" 2517064Sbloom #include "ip_var.h" 2617064Sbloom #include "tcp.h" 2717064Sbloom #include "tcp_fsm.h" 2817064Sbloom #include "tcp_seq.h" 2917064Sbloom #include "tcp_timer.h" 3017064Sbloom #include "tcp_var.h" 3117064Sbloom #include "tcpip.h" 3217064Sbloom #include "tcp_debug.h" 334497Swnj 345280Sroot /* 355280Sroot * TCP protocol interface to socket abstraction. 365280Sroot */ 375280Sroot extern char *tcpstates[]; 384954Swnj struct tcpcb *tcp_newtcpcb(); 3912766Ssam int tcpsenderrors; 405280Sroot 414734Swnj /* 425280Sroot * Process a TCP user request for TCP tb. If this is a send request 434731Swnj * then m is the mbuf chain of send data. If this is a timer expiration 444731Swnj * (called from the software clock routine), then timertype tells which timer. 454731Swnj */ 468601Sroot /*ARGSUSED*/ 4712766Ssam tcp_usrreq(so, req, m, nam, rights) 484809Swnj struct socket *so; 494809Swnj int req; 5012766Ssam struct mbuf *m, *nam, *rights; 514497Swnj { 52*30909Skarels register struct inpcb *inp; 534911Swnj register struct tcpcb *tp; 54*30909Skarels int s; 554809Swnj int error = 0; 565270Sroot int ostate; 574497Swnj 58*30909Skarels if (req == PRU_CONTROL) 59*30909Skarels return (in_control(so, (int)m, (caddr_t)nam, 60*30909Skarels (struct ifnet *)rights)); 61*30909Skarels if (rights && rights->m_len) 6212766Ssam return (EINVAL); 63*30909Skarels 64*30909Skarels s = splnet(); 65*30909Skarels inp = sotoinpcb(so); 664886Swnj /* 675280Sroot * When a TCP is attached to a socket, then there will be 685280Sroot * a (struct inpcb) pointed at by the socket, and this 695280Sroot * structure will point at a subsidary (struct tcpcb). 704886Swnj */ 715089Swnj if (inp == 0 && req != PRU_ATTACH) { 725075Swnj splx(s); 735280Sroot return (EINVAL); /* XXX */ 745075Swnj } 755075Swnj if (inp) { 764911Swnj tp = intotcpcb(inp); 778272Sroot /* WHAT IF TP IS 0? */ 784731Swnj #ifdef KPROF 795075Swnj tcp_acounts[tp->t_state][req]++; 804731Swnj #endif 815270Sroot ostate = tp->t_state; 827511Sroot } else 837511Sroot ostate = 0; 844809Swnj switch (req) { 854497Swnj 865280Sroot /* 875280Sroot * TCP attaches to socket via PRU_ATTACH, reserving space, 888272Sroot * and an internet control block. 895280Sroot */ 904809Swnj case PRU_ATTACH: 914954Swnj if (inp) { 924809Swnj error = EISCONN; 934911Swnj break; 944886Swnj } 958640Sroot error = tcp_attach(so); 965075Swnj if (error) 974954Swnj break; 9810397Ssam if ((so->so_options & SO_LINGER) && so->so_linger == 0) 995392Swnj so->so_linger = TCP_LINGERTIME; 1005280Sroot tp = sototcpcb(so); 1014567Swnj break; 1024497Swnj 1035280Sroot /* 1045280Sroot * PRU_DETACH detaches the TCP protocol from the socket. 1055280Sroot * If the protocol state is non-embryonic, then can't 1065280Sroot * do this directly: have to initiate a PRU_DISCONNECT, 1075280Sroot * which may finish later; embryonic TCB's can just 1085280Sroot * be discarded here. 1095280Sroot */ 1104809Swnj case PRU_DETACH: 1115280Sroot if (tp->t_state > TCPS_LISTEN) 11210397Ssam tp = tcp_disconnect(tp); 11310397Ssam else 11410397Ssam tp = tcp_close(tp); 1154809Swnj break; 1164809Swnj 1175280Sroot /* 1188272Sroot * Give the socket an address. 1198272Sroot */ 1208272Sroot case PRU_BIND: 1218272Sroot error = in_pcbbind(inp, nam); 1228272Sroot if (error) 1238272Sroot break; 1248272Sroot break; 1258272Sroot 1268272Sroot /* 1278272Sroot * Prepare to accept connections. 1288272Sroot */ 1298272Sroot case PRU_LISTEN: 1308272Sroot if (inp->inp_lport == 0) 1318272Sroot error = in_pcbbind(inp, (struct mbuf *)0); 1328272Sroot if (error == 0) 1338272Sroot tp->t_state = TCPS_LISTEN; 1348272Sroot break; 1358272Sroot 1368272Sroot /* 1375280Sroot * Initiate connection to peer. 1385280Sroot * Create a template for use in transmissions on this connection. 1395280Sroot * Enter SYN_SENT state, and mark socket as connecting. 1405280Sroot * Start keep-alive timer, and seed output sequence space. 1415280Sroot * Send initial segment on connection. 1425280Sroot */ 1434809Swnj case PRU_CONNECT: 1448272Sroot if (inp->inp_lport == 0) { 1458272Sroot error = in_pcbbind(inp, (struct mbuf *)0); 1468272Sroot if (error) 1478272Sroot break; 1488272Sroot } 1498272Sroot error = in_pcbconnect(inp, nam); 1504954Swnj if (error) 1514886Swnj break; 1525174Swnj tp->t_template = tcp_template(tp); 1535280Sroot if (tp->t_template == 0) { 1545280Sroot in_pcbdisconnect(inp); 1555280Sroot error = ENOBUFS; 1565280Sroot break; 1575280Sroot } 1584886Swnj soisconnecting(so); 15930527Skarels tcpstat.tcps_connattempt++; 1605075Swnj tp->t_state = TCPS_SYN_SENT; 1615245Sroot tp->t_timer[TCPT_KEEP] = TCPTV_KEEP; 1625245Sroot tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2; 1635245Sroot tcp_sendseqinit(tp); 1646506Ssam error = tcp_output(tp); 1654567Swnj break; 1664497Swnj 1675280Sroot /* 16813117Ssam * Create a TCP connection between two sockets. 16913117Ssam */ 17013117Ssam case PRU_CONNECT2: 17113117Ssam error = EOPNOTSUPP; 17213117Ssam break; 17313117Ssam 17413117Ssam /* 1755280Sroot * Initiate disconnect from peer. 1765280Sroot * If connection never passed embryonic stage, just drop; 1775280Sroot * else if don't need to let data drain, then can just drop anyways, 1785280Sroot * else have to begin TCP shutdown process: mark socket disconnecting, 1795280Sroot * drain unread data, state switch to reflect user close, and 1805280Sroot * send segment (e.g. FIN) to peer. Socket will be really disconnected 1815280Sroot * when peer sends FIN and acks ours. 1825280Sroot * 1835280Sroot * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB. 1845280Sroot */ 1855280Sroot case PRU_DISCONNECT: 18610397Ssam tp = tcp_disconnect(tp); 1875245Sroot break; 1885245Sroot 1895280Sroot /* 1905280Sroot * Accept a connection. Essentially all the work is 1915280Sroot * done at higher levels; just return the address 1925280Sroot * of the peer, storing through addr. 1935280Sroot */ 1946117Swnj case PRU_ACCEPT: { 1958272Sroot struct sockaddr_in *sin = mtod(nam, struct sockaddr_in *); 1966117Swnj 1978272Sroot nam->m_len = sizeof (struct sockaddr_in); 1988272Sroot sin->sin_family = AF_INET; 1998272Sroot sin->sin_port = inp->inp_fport; 2008272Sroot sin->sin_addr = inp->inp_faddr; 2018272Sroot break; 2026117Swnj } 2034925Swnj 2045280Sroot /* 2055280Sroot * Mark the connection as being incapable of further output. 2065280Sroot */ 2074809Swnj case PRU_SHUTDOWN: 2085089Swnj socantsendmore(so); 20910397Ssam tp = tcp_usrclosed(tp); 21010397Ssam if (tp) 21110397Ssam error = tcp_output(tp); 2124567Swnj break; 2134497Swnj 2145280Sroot /* 2155280Sroot * After a receive, possibly send window update to peer. 2165280Sroot */ 2174809Swnj case PRU_RCVD: 2185113Swnj (void) tcp_output(tp); 2194567Swnj break; 2204497Swnj 2215280Sroot /* 2225280Sroot * Do a send by putting data in output queue and updating urgent 2235280Sroot * marker if URG set. Possibly send more data. 2245280Sroot */ 2254809Swnj case PRU_SEND: 2265075Swnj sbappend(&so->so_snd, m); 2276506Ssam error = tcp_output(tp); 22812766Ssam if (error) { /* XXX fix to use other path */ 22912766Ssam if (error == ENOBUFS) /* XXX */ 23012766Ssam error = 0; /* XXX */ 23112766Ssam tcpsenderrors++; 23212766Ssam } 2334567Swnj break; 2344567Swnj 2355280Sroot /* 2365280Sroot * Abort the TCP. 2375280Sroot */ 2384809Swnj case PRU_ABORT: 23910397Ssam tp = tcp_drop(tp, ECONNABORTED); 2404567Swnj break; 2414567Swnj 2425113Swnj case PRU_SENSE: 24316989Skarels ((struct stat *) m)->st_blksize = so->so_snd.sb_hiwat; 24430871Smckusick (void) splx(s); 24516989Skarels return (0); 2465113Swnj 2475113Swnj case PRU_RCVOOB: 24824821Skarels if ((so->so_oobmark == 0 && 24924821Skarels (so->so_state & SS_RCVATMARK) == 0) || 25027195Skarels so->so_options & SO_OOBINLINE || 25124821Skarels tp->t_oobflags & TCPOOB_HADDATA) { 2525417Swnj error = EINVAL; 2535417Swnj break; 2545417Swnj } 2555549Swnj if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) { 2565442Swnj error = EWOULDBLOCK; 2575549Swnj break; 2585442Swnj } 2598310Sroot m->m_len = 1; 2605549Swnj *mtod(m, caddr_t) = tp->t_iobc; 26124821Skarels if (((int)nam & MSG_PEEK) == 0) 26224821Skarels tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA); 2635113Swnj break; 2645113Swnj 2655113Swnj case PRU_SENDOOB: 2665442Swnj if (sbspace(&so->so_snd) < -512) { 26711229Ssam m_freem(m); 2685442Swnj error = ENOBUFS; 2695442Swnj break; 2705442Swnj } 27127195Skarels /* 27227195Skarels * According to RFC961 (Assigned Protocols), 27327195Skarels * the urgent pointer points to the last octet 27427195Skarels * of urgent data. We continue, however, 27527195Skarels * to consider it to indicate the first octet 27627195Skarels * of data past the urgent section. 27727195Skarels * Otherwise, snd_up should be one lower. 27827195Skarels */ 2795417Swnj sbappend(&so->so_snd, m); 28027195Skarels tp->snd_up = tp->snd_una + so->so_snd.sb_cc; 2815549Swnj tp->t_force = 1; 2826506Ssam error = tcp_output(tp); 2835549Swnj tp->t_force = 0; 2845113Swnj break; 2855113Swnj 2866510Ssam case PRU_SOCKADDR: 2878272Sroot in_setsockaddr(inp, nam); 2886510Ssam break; 2896510Ssam 29014123Ssam case PRU_PEERADDR: 29114123Ssam in_setpeeraddr(inp, nam); 29214123Ssam break; 29314123Ssam 2945280Sroot /* 2955280Sroot * TCP slow timer went off; going through this 2965280Sroot * routine for tracing's sake. 2975280Sroot */ 2984809Swnj case PRU_SLOWTIMO: 29910397Ssam tp = tcp_timers(tp, (int)nam); 3008272Sroot req |= (int)nam << 8; /* for debug's sake */ 3014809Swnj break; 3024809Swnj 3034731Swnj default: 3044731Swnj panic("tcp_usrreq"); 3054567Swnj } 3065270Sroot if (tp && (so->so_options & SO_DEBUG)) 3075270Sroot tcp_trace(TA_USER, ostate, tp, (struct tcpiphdr *)0, req); 3084567Swnj splx(s); 3094886Swnj return (error); 3104497Swnj } 3115245Sroot 31225896Skarels tcp_ctloutput(op, so, level, optname, mp) 31324821Skarels int op; 31424821Skarels struct socket *so; 31524821Skarels int level, optname; 31625896Skarels struct mbuf **mp; 31724821Skarels { 31825896Skarels int error = 0; 31925896Skarels struct inpcb *inp = sotoinpcb(so); 32025896Skarels register struct tcpcb *tp = intotcpcb(inp); 32125896Skarels register struct mbuf *m; 32225896Skarels 32324821Skarels if (level != IPPROTO_TCP) 32426248Skarels return (ip_ctloutput(op, so, level, optname, mp)); 32525896Skarels 32625896Skarels switch (op) { 32725896Skarels 32825896Skarels case PRCO_SETOPT: 32925896Skarels m = *mp; 33025896Skarels switch (optname) { 33125896Skarels 33225896Skarels case TCP_NODELAY: 33325896Skarels if (m == NULL || m->m_len < sizeof (int)) 33425896Skarels error = EINVAL; 33525896Skarels else if (*mtod(m, int *)) 33625896Skarels tp->t_flags |= TF_NODELAY; 33725896Skarels else 33825896Skarels tp->t_flags &= ~TF_NODELAY; 33925896Skarels break; 34025896Skarels 34125896Skarels case TCP_MAXSEG: /* not yet */ 34225896Skarels default: 34325896Skarels error = EINVAL; 34425896Skarels break; 34525896Skarels } 34626388Skarels (void)m_free(m); 34725896Skarels break; 34825896Skarels 34925896Skarels case PRCO_GETOPT: 35025896Skarels *mp = m = m_get(M_WAIT, MT_SOOPTS); 35125896Skarels m->m_len = sizeof(int); 35225896Skarels 35325896Skarels switch (optname) { 35425896Skarels case TCP_NODELAY: 35525896Skarels *mtod(m, int *) = tp->t_flags & TF_NODELAY; 35625896Skarels break; 35725896Skarels case TCP_MAXSEG: 35825896Skarels *mtod(m, int *) = tp->t_maxseg; 35925896Skarels break; 36025896Skarels default: 36125896Skarels error = EINVAL; 36225896Skarels break; 36325896Skarels } 36425896Skarels break; 36525896Skarels } 36625896Skarels return (error); 36724821Skarels } 36824821Skarels 36918367Skarels int tcp_sendspace = 1024*4; 37018367Skarels int tcp_recvspace = 1024*4; 3715280Sroot /* 3725280Sroot * Attach TCP protocol to socket, allocating 3735280Sroot * internet protocol control block, tcp control block, 3745280Sroot * bufer space, and entering LISTEN state if to accept connections. 3755280Sroot */ 3768272Sroot tcp_attach(so) 3775280Sroot struct socket *so; 3785280Sroot { 3795280Sroot register struct tcpcb *tp; 3805280Sroot struct inpcb *inp; 3815280Sroot int error; 3825280Sroot 3839031Sroot error = soreserve(so, tcp_sendspace, tcp_recvspace); 3845280Sroot if (error) 38517047Skarels return (error); 3867511Sroot error = in_pcballoc(so, &tcb); 3877511Sroot if (error) 38817047Skarels return (error); 3898272Sroot inp = sotoinpcb(so); 3905280Sroot tp = tcp_newtcpcb(inp); 3917511Sroot if (tp == 0) { 39217047Skarels int nofd = so->so_state & SS_NOFDREF; /* XXX */ 39317047Skarels 39417047Skarels so->so_state &= ~SS_NOFDREF; /* don't free the socket yet */ 39517047Skarels in_pcbdetach(inp); 39617047Skarels so->so_state |= nofd; 39717047Skarels return (ENOBUFS); 3987511Sroot } 3998272Sroot tp->t_state = TCPS_CLOSED; 4005280Sroot return (0); 4015280Sroot } 4025280Sroot 4035280Sroot /* 4045280Sroot * Initiate (or continue) disconnect. 4055280Sroot * If embryonic state, just send reset (once). 40613221Ssam * If in ``let data drain'' option and linger null, just drop. 4075280Sroot * Otherwise (hard), mark socket disconnecting and drop 4085280Sroot * current input data; switch states based on user close, and 4095280Sroot * send segment to peer (with FIN). 4105280Sroot */ 41110397Ssam struct tcpcb * 4125280Sroot tcp_disconnect(tp) 41310397Ssam register struct tcpcb *tp; 4145280Sroot { 4155280Sroot struct socket *so = tp->t_inpcb->inp_socket; 4165280Sroot 4175280Sroot if (tp->t_state < TCPS_ESTABLISHED) 41810397Ssam tp = tcp_close(tp); 41913221Ssam else if ((so->so_options & SO_LINGER) && so->so_linger == 0) 42010397Ssam tp = tcp_drop(tp, 0); 4215280Sroot else { 4225280Sroot soisdisconnecting(so); 4235280Sroot sbflush(&so->so_rcv); 42410397Ssam tp = tcp_usrclosed(tp); 42510397Ssam if (tp) 42610397Ssam (void) tcp_output(tp); 4275280Sroot } 42810397Ssam return (tp); 4295280Sroot } 4305280Sroot 4315280Sroot /* 4325280Sroot * User issued close, and wish to trail through shutdown states: 4335280Sroot * if never received SYN, just forget it. If got a SYN from peer, 4345280Sroot * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN. 4355280Sroot * If already got a FIN from peer, then almost done; go to LAST_ACK 4365280Sroot * state. In all other cases, have already sent FIN to peer (e.g. 4375280Sroot * after PRU_SHUTDOWN), and just have to play tedious game waiting 4385280Sroot * for peer to send FIN or not respond to keep-alives, etc. 4395897Swnj * We can let the user exit from the close as soon as the FIN is acked. 4405280Sroot */ 44110397Ssam struct tcpcb * 4425245Sroot tcp_usrclosed(tp) 44310397Ssam register struct tcpcb *tp; 4445245Sroot { 4455245Sroot 4465245Sroot switch (tp->t_state) { 4475245Sroot 44812438Ssam case TCPS_CLOSED: 4495245Sroot case TCPS_LISTEN: 4505245Sroot case TCPS_SYN_SENT: 4515245Sroot tp->t_state = TCPS_CLOSED; 45210397Ssam tp = tcp_close(tp); 4535245Sroot break; 4545245Sroot 4555245Sroot case TCPS_SYN_RECEIVED: 4565245Sroot case TCPS_ESTABLISHED: 4575245Sroot tp->t_state = TCPS_FIN_WAIT_1; 4585245Sroot break; 4595245Sroot 4605245Sroot case TCPS_CLOSE_WAIT: 4615245Sroot tp->t_state = TCPS_LAST_ACK; 4625245Sroot break; 4635245Sroot } 46410397Ssam if (tp && tp->t_state >= TCPS_FIN_WAIT_2) 4655897Swnj soisdisconnected(tp->t_inpcb->inp_socket); 46610397Ssam return (tp); 4675245Sroot } 468