123196Smckusick /* 2*29155Smckusick * 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*29155Smckusick * @(#)tcp_usrreq.c 7.1 (Berkeley) 06/05/86 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 { 524886Swnj register struct inpcb *inp = sotoinpcb(so); 534911Swnj register struct tcpcb *tp; 544567Swnj int s = splnet(); 554809Swnj int error = 0; 565270Sroot int ostate; 574497Swnj 5818367Skarels if (req == PRU_CONTROL) 5918367Skarels return (in_control(so, (int)m, (caddr_t)nam, 6018367Skarels (struct ifnet *)rights)); 6112766Ssam if (rights && rights->m_len) { 6212766Ssam splx(s); 6312766Ssam return (EINVAL); 6412766Ssam } 654886Swnj /* 665280Sroot * When a TCP is attached to a socket, then there will be 675280Sroot * a (struct inpcb) pointed at by the socket, and this 685280Sroot * structure will point at a subsidary (struct tcpcb). 694886Swnj */ 705089Swnj if (inp == 0 && req != PRU_ATTACH) { 715075Swnj splx(s); 725280Sroot return (EINVAL); /* XXX */ 735075Swnj } 745075Swnj if (inp) { 754911Swnj tp = intotcpcb(inp); 768272Sroot /* WHAT IF TP IS 0? */ 774731Swnj #ifdef KPROF 785075Swnj tcp_acounts[tp->t_state][req]++; 794731Swnj #endif 805270Sroot ostate = tp->t_state; 817511Sroot } else 827511Sroot ostate = 0; 834809Swnj switch (req) { 844497Swnj 855280Sroot /* 865280Sroot * TCP attaches to socket via PRU_ATTACH, reserving space, 878272Sroot * and an internet control block. 885280Sroot */ 894809Swnj case PRU_ATTACH: 904954Swnj if (inp) { 914809Swnj error = EISCONN; 924911Swnj break; 934886Swnj } 948640Sroot error = tcp_attach(so); 955075Swnj if (error) 964954Swnj break; 9710397Ssam if ((so->so_options & SO_LINGER) && so->so_linger == 0) 985392Swnj so->so_linger = TCP_LINGERTIME; 995280Sroot tp = sototcpcb(so); 1004567Swnj break; 1014497Swnj 1025280Sroot /* 1035280Sroot * PRU_DETACH detaches the TCP protocol from the socket. 1045280Sroot * If the protocol state is non-embryonic, then can't 1055280Sroot * do this directly: have to initiate a PRU_DISCONNECT, 1065280Sroot * which may finish later; embryonic TCB's can just 1075280Sroot * be discarded here. 1085280Sroot */ 1094809Swnj case PRU_DETACH: 1105280Sroot if (tp->t_state > TCPS_LISTEN) 11110397Ssam tp = tcp_disconnect(tp); 11210397Ssam else 11310397Ssam tp = tcp_close(tp); 1144809Swnj break; 1154809Swnj 1165280Sroot /* 1178272Sroot * Give the socket an address. 1188272Sroot */ 1198272Sroot case PRU_BIND: 1208272Sroot error = in_pcbbind(inp, nam); 1218272Sroot if (error) 1228272Sroot break; 1238272Sroot break; 1248272Sroot 1258272Sroot /* 1268272Sroot * Prepare to accept connections. 1278272Sroot */ 1288272Sroot case PRU_LISTEN: 1298272Sroot if (inp->inp_lport == 0) 1308272Sroot error = in_pcbbind(inp, (struct mbuf *)0); 1318272Sroot if (error == 0) 1328272Sroot tp->t_state = TCPS_LISTEN; 1338272Sroot break; 1348272Sroot 1358272Sroot /* 1365280Sroot * Initiate connection to peer. 1375280Sroot * Create a template for use in transmissions on this connection. 1385280Sroot * Enter SYN_SENT state, and mark socket as connecting. 1395280Sroot * Start keep-alive timer, and seed output sequence space. 1405280Sroot * Send initial segment on connection. 1415280Sroot */ 1424809Swnj case PRU_CONNECT: 1438272Sroot if (inp->inp_lport == 0) { 1448272Sroot error = in_pcbbind(inp, (struct mbuf *)0); 1458272Sroot if (error) 1468272Sroot break; 1478272Sroot } 1488272Sroot error = in_pcbconnect(inp, nam); 1494954Swnj if (error) 1504886Swnj break; 1515174Swnj tp->t_template = tcp_template(tp); 1525280Sroot if (tp->t_template == 0) { 1535280Sroot in_pcbdisconnect(inp); 1545280Sroot error = ENOBUFS; 1555280Sroot break; 1565280Sroot } 1574886Swnj soisconnecting(so); 1585075Swnj tp->t_state = TCPS_SYN_SENT; 1595245Sroot tp->t_timer[TCPT_KEEP] = TCPTV_KEEP; 1605245Sroot tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2; 1615245Sroot tcp_sendseqinit(tp); 1626506Ssam error = tcp_output(tp); 1634567Swnj break; 1644497Swnj 1655280Sroot /* 16613117Ssam * Create a TCP connection between two sockets. 16713117Ssam */ 16813117Ssam case PRU_CONNECT2: 16913117Ssam error = EOPNOTSUPP; 17013117Ssam break; 17113117Ssam 17213117Ssam /* 1735280Sroot * Initiate disconnect from peer. 1745280Sroot * If connection never passed embryonic stage, just drop; 1755280Sroot * else if don't need to let data drain, then can just drop anyways, 1765280Sroot * else have to begin TCP shutdown process: mark socket disconnecting, 1775280Sroot * drain unread data, state switch to reflect user close, and 1785280Sroot * send segment (e.g. FIN) to peer. Socket will be really disconnected 1795280Sroot * when peer sends FIN and acks ours. 1805280Sroot * 1815280Sroot * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB. 1825280Sroot */ 1835280Sroot case PRU_DISCONNECT: 18410397Ssam tp = tcp_disconnect(tp); 1855245Sroot break; 1865245Sroot 1875280Sroot /* 1885280Sroot * Accept a connection. Essentially all the work is 1895280Sroot * done at higher levels; just return the address 1905280Sroot * of the peer, storing through addr. 1915280Sroot */ 1926117Swnj case PRU_ACCEPT: { 1938272Sroot struct sockaddr_in *sin = mtod(nam, struct sockaddr_in *); 1946117Swnj 1958272Sroot nam->m_len = sizeof (struct sockaddr_in); 1968272Sroot sin->sin_family = AF_INET; 1978272Sroot sin->sin_port = inp->inp_fport; 1988272Sroot sin->sin_addr = inp->inp_faddr; 1998272Sroot break; 2006117Swnj } 2014925Swnj 2025280Sroot /* 2035280Sroot * Mark the connection as being incapable of further output. 2045280Sroot */ 2054809Swnj case PRU_SHUTDOWN: 2065089Swnj socantsendmore(so); 20710397Ssam tp = tcp_usrclosed(tp); 20810397Ssam if (tp) 20910397Ssam error = tcp_output(tp); 2104567Swnj break; 2114497Swnj 2125280Sroot /* 2135280Sroot * After a receive, possibly send window update to peer. 2145280Sroot */ 2154809Swnj case PRU_RCVD: 2165113Swnj (void) tcp_output(tp); 2174567Swnj break; 2184497Swnj 2195280Sroot /* 2205280Sroot * Do a send by putting data in output queue and updating urgent 2215280Sroot * marker if URG set. Possibly send more data. 2225280Sroot */ 2234809Swnj case PRU_SEND: 2245075Swnj sbappend(&so->so_snd, m); 2256506Ssam #ifdef notdef 2265089Swnj if (tp->t_flags & TF_PUSH) 2275075Swnj tp->snd_end = tp->snd_una + so->so_snd.sb_cc; 2286506Ssam #endif 2296506Ssam error = tcp_output(tp); 23012766Ssam if (error) { /* XXX fix to use other path */ 23112766Ssam if (error == ENOBUFS) /* XXX */ 23212766Ssam error = 0; /* XXX */ 23312766Ssam tcpsenderrors++; 23412766Ssam } 2354567Swnj break; 2364567Swnj 2375280Sroot /* 2385280Sroot * Abort the TCP. 2395280Sroot */ 2404809Swnj case PRU_ABORT: 24110397Ssam tp = tcp_drop(tp, ECONNABORTED); 2424567Swnj break; 2434567Swnj 2445113Swnj case PRU_SENSE: 24516989Skarels ((struct stat *) m)->st_blksize = so->so_snd.sb_hiwat; 24616989Skarels return (0); 2475113Swnj 2485113Swnj case PRU_RCVOOB: 24924821Skarels if ((so->so_oobmark == 0 && 25024821Skarels (so->so_state & SS_RCVATMARK) == 0) || 25127195Skarels so->so_options & SO_OOBINLINE || 25224821Skarels tp->t_oobflags & TCPOOB_HADDATA) { 2535417Swnj error = EINVAL; 2545417Swnj break; 2555417Swnj } 2565549Swnj if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) { 2575442Swnj error = EWOULDBLOCK; 2585549Swnj break; 2595442Swnj } 2608310Sroot m->m_len = 1; 2615549Swnj *mtod(m, caddr_t) = tp->t_iobc; 26224821Skarels if (((int)nam & MSG_PEEK) == 0) 26324821Skarels tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA); 2645113Swnj break; 2655113Swnj 2665113Swnj case PRU_SENDOOB: 2675442Swnj if (sbspace(&so->so_snd) < -512) { 26811229Ssam m_freem(m); 2695442Swnj error = ENOBUFS; 2705442Swnj break; 2715442Swnj } 27227195Skarels /* 27327195Skarels * According to RFC961 (Assigned Protocols), 27427195Skarels * the urgent pointer points to the last octet 27527195Skarels * of urgent data. We continue, however, 27627195Skarels * to consider it to indicate the first octet 27727195Skarels * of data past the urgent section. 27827195Skarels * Otherwise, snd_up should be one lower. 27927195Skarels */ 2805417Swnj sbappend(&so->so_snd, m); 28127195Skarels tp->snd_up = tp->snd_una + so->so_snd.sb_cc; 2825549Swnj tp->t_force = 1; 2836506Ssam error = tcp_output(tp); 2845549Swnj tp->t_force = 0; 2855113Swnj break; 2865113Swnj 2876510Ssam case PRU_SOCKADDR: 2888272Sroot in_setsockaddr(inp, nam); 2896510Ssam break; 2906510Ssam 29114123Ssam case PRU_PEERADDR: 29214123Ssam in_setpeeraddr(inp, nam); 29314123Ssam break; 29414123Ssam 2955280Sroot /* 2965280Sroot * TCP slow timer went off; going through this 2975280Sroot * routine for tracing's sake. 2985280Sroot */ 2994809Swnj case PRU_SLOWTIMO: 30010397Ssam tp = tcp_timers(tp, (int)nam); 3018272Sroot req |= (int)nam << 8; /* for debug's sake */ 3024809Swnj break; 3034809Swnj 3044731Swnj default: 3054731Swnj panic("tcp_usrreq"); 3064567Swnj } 3075270Sroot if (tp && (so->so_options & SO_DEBUG)) 3085270Sroot tcp_trace(TA_USER, ostate, tp, (struct tcpiphdr *)0, req); 3094567Swnj splx(s); 3104886Swnj return (error); 3114497Swnj } 3125245Sroot 31325896Skarels tcp_ctloutput(op, so, level, optname, mp) 31424821Skarels int op; 31524821Skarels struct socket *so; 31624821Skarels int level, optname; 31725896Skarels struct mbuf **mp; 31824821Skarels { 31925896Skarels int error = 0; 32025896Skarels struct inpcb *inp = sotoinpcb(so); 32125896Skarels register struct tcpcb *tp = intotcpcb(inp); 32225896Skarels register struct mbuf *m; 32325896Skarels 32424821Skarels if (level != IPPROTO_TCP) 32526248Skarels return (ip_ctloutput(op, so, level, optname, mp)); 32625896Skarels 32725896Skarels switch (op) { 32825896Skarels 32925896Skarels case PRCO_SETOPT: 33025896Skarels m = *mp; 33125896Skarels switch (optname) { 33225896Skarels 33325896Skarels case TCP_NODELAY: 33425896Skarels if (m == NULL || m->m_len < sizeof (int)) 33525896Skarels error = EINVAL; 33625896Skarels else if (*mtod(m, int *)) 33725896Skarels tp->t_flags |= TF_NODELAY; 33825896Skarels else 33925896Skarels tp->t_flags &= ~TF_NODELAY; 34025896Skarels break; 34125896Skarels 34225896Skarels case TCP_MAXSEG: /* not yet */ 34325896Skarels default: 34425896Skarels error = EINVAL; 34525896Skarels break; 34625896Skarels } 34726388Skarels (void)m_free(m); 34825896Skarels break; 34925896Skarels 35025896Skarels case PRCO_GETOPT: 35125896Skarels *mp = m = m_get(M_WAIT, MT_SOOPTS); 35225896Skarels m->m_len = sizeof(int); 35325896Skarels 35425896Skarels switch (optname) { 35525896Skarels case TCP_NODELAY: 35625896Skarels *mtod(m, int *) = tp->t_flags & TF_NODELAY; 35725896Skarels break; 35825896Skarels case TCP_MAXSEG: 35925896Skarels *mtod(m, int *) = tp->t_maxseg; 36025896Skarels break; 36125896Skarels default: 36225896Skarels error = EINVAL; 36325896Skarels break; 36425896Skarels } 36525896Skarels break; 36625896Skarels } 36725896Skarels return (error); 36824821Skarels } 36924821Skarels 37018367Skarels int tcp_sendspace = 1024*4; 37118367Skarels int tcp_recvspace = 1024*4; 3725280Sroot /* 3735280Sroot * Attach TCP protocol to socket, allocating 3745280Sroot * internet protocol control block, tcp control block, 3755280Sroot * bufer space, and entering LISTEN state if to accept connections. 3765280Sroot */ 3778272Sroot tcp_attach(so) 3785280Sroot struct socket *so; 3795280Sroot { 3805280Sroot register struct tcpcb *tp; 3815280Sroot struct inpcb *inp; 3825280Sroot int error; 3835280Sroot 3849031Sroot error = soreserve(so, tcp_sendspace, tcp_recvspace); 3855280Sroot if (error) 38617047Skarels return (error); 3877511Sroot error = in_pcballoc(so, &tcb); 3887511Sroot if (error) 38917047Skarels return (error); 3908272Sroot inp = sotoinpcb(so); 3915280Sroot tp = tcp_newtcpcb(inp); 3927511Sroot if (tp == 0) { 39317047Skarels int nofd = so->so_state & SS_NOFDREF; /* XXX */ 39417047Skarels 39517047Skarels so->so_state &= ~SS_NOFDREF; /* don't free the socket yet */ 39617047Skarels in_pcbdetach(inp); 39717047Skarels so->so_state |= nofd; 39817047Skarels return (ENOBUFS); 3997511Sroot } 4008272Sroot tp->t_state = TCPS_CLOSED; 4015280Sroot return (0); 4025280Sroot } 4035280Sroot 4045280Sroot /* 4055280Sroot * Initiate (or continue) disconnect. 4065280Sroot * If embryonic state, just send reset (once). 40713221Ssam * If in ``let data drain'' option and linger null, just drop. 4085280Sroot * Otherwise (hard), mark socket disconnecting and drop 4095280Sroot * current input data; switch states based on user close, and 4105280Sroot * send segment to peer (with FIN). 4115280Sroot */ 41210397Ssam struct tcpcb * 4135280Sroot tcp_disconnect(tp) 41410397Ssam register struct tcpcb *tp; 4155280Sroot { 4165280Sroot struct socket *so = tp->t_inpcb->inp_socket; 4175280Sroot 4185280Sroot if (tp->t_state < TCPS_ESTABLISHED) 41910397Ssam tp = tcp_close(tp); 42013221Ssam else if ((so->so_options & SO_LINGER) && so->so_linger == 0) 42110397Ssam tp = tcp_drop(tp, 0); 4225280Sroot else { 4235280Sroot soisdisconnecting(so); 4245280Sroot sbflush(&so->so_rcv); 42510397Ssam tp = tcp_usrclosed(tp); 42610397Ssam if (tp) 42710397Ssam (void) tcp_output(tp); 4285280Sroot } 42910397Ssam return (tp); 4305280Sroot } 4315280Sroot 4325280Sroot /* 4335280Sroot * User issued close, and wish to trail through shutdown states: 4345280Sroot * if never received SYN, just forget it. If got a SYN from peer, 4355280Sroot * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN. 4365280Sroot * If already got a FIN from peer, then almost done; go to LAST_ACK 4375280Sroot * state. In all other cases, have already sent FIN to peer (e.g. 4385280Sroot * after PRU_SHUTDOWN), and just have to play tedious game waiting 4395280Sroot * for peer to send FIN or not respond to keep-alives, etc. 4405897Swnj * We can let the user exit from the close as soon as the FIN is acked. 4415280Sroot */ 44210397Ssam struct tcpcb * 4435245Sroot tcp_usrclosed(tp) 44410397Ssam register struct tcpcb *tp; 4455245Sroot { 4465245Sroot 4475245Sroot switch (tp->t_state) { 4485245Sroot 44912438Ssam case TCPS_CLOSED: 4505245Sroot case TCPS_LISTEN: 4515245Sroot case TCPS_SYN_SENT: 4525245Sroot tp->t_state = TCPS_CLOSED; 45310397Ssam tp = tcp_close(tp); 4545245Sroot break; 4555245Sroot 4565245Sroot case TCPS_SYN_RECEIVED: 4575245Sroot case TCPS_ESTABLISHED: 4585245Sroot tp->t_state = TCPS_FIN_WAIT_1; 4595245Sroot break; 4605245Sroot 4615245Sroot case TCPS_CLOSE_WAIT: 4625245Sroot tp->t_state = TCPS_LAST_ACK; 4635245Sroot break; 4645245Sroot } 46510397Ssam if (tp && tp->t_state >= TCPS_FIN_WAIT_2) 4665897Swnj soisdisconnected(tp->t_inpcb->inp_socket); 46710397Ssam return (tp); 4685245Sroot } 469