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*30527Skarels * @(#)tcp_usrreq.c 7.2 (Berkeley) 02/19/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 { 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); 158*30527Skarels tcpstat.tcps_connattempt++; 1595075Swnj tp->t_state = TCPS_SYN_SENT; 1605245Sroot tp->t_timer[TCPT_KEEP] = TCPTV_KEEP; 1615245Sroot tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2; 1625245Sroot tcp_sendseqinit(tp); 1636506Ssam error = tcp_output(tp); 1644567Swnj break; 1654497Swnj 1665280Sroot /* 16713117Ssam * Create a TCP connection between two sockets. 16813117Ssam */ 16913117Ssam case PRU_CONNECT2: 17013117Ssam error = EOPNOTSUPP; 17113117Ssam break; 17213117Ssam 17313117Ssam /* 1745280Sroot * Initiate disconnect from peer. 1755280Sroot * If connection never passed embryonic stage, just drop; 1765280Sroot * else if don't need to let data drain, then can just drop anyways, 1775280Sroot * else have to begin TCP shutdown process: mark socket disconnecting, 1785280Sroot * drain unread data, state switch to reflect user close, and 1795280Sroot * send segment (e.g. FIN) to peer. Socket will be really disconnected 1805280Sroot * when peer sends FIN and acks ours. 1815280Sroot * 1825280Sroot * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB. 1835280Sroot */ 1845280Sroot case PRU_DISCONNECT: 18510397Ssam tp = tcp_disconnect(tp); 1865245Sroot break; 1875245Sroot 1885280Sroot /* 1895280Sroot * Accept a connection. Essentially all the work is 1905280Sroot * done at higher levels; just return the address 1915280Sroot * of the peer, storing through addr. 1925280Sroot */ 1936117Swnj case PRU_ACCEPT: { 1948272Sroot struct sockaddr_in *sin = mtod(nam, struct sockaddr_in *); 1956117Swnj 1968272Sroot nam->m_len = sizeof (struct sockaddr_in); 1978272Sroot sin->sin_family = AF_INET; 1988272Sroot sin->sin_port = inp->inp_fport; 1998272Sroot sin->sin_addr = inp->inp_faddr; 2008272Sroot break; 2016117Swnj } 2024925Swnj 2035280Sroot /* 2045280Sroot * Mark the connection as being incapable of further output. 2055280Sroot */ 2064809Swnj case PRU_SHUTDOWN: 2075089Swnj socantsendmore(so); 20810397Ssam tp = tcp_usrclosed(tp); 20910397Ssam if (tp) 21010397Ssam error = tcp_output(tp); 2114567Swnj break; 2124497Swnj 2135280Sroot /* 2145280Sroot * After a receive, possibly send window update to peer. 2155280Sroot */ 2164809Swnj case PRU_RCVD: 2175113Swnj (void) tcp_output(tp); 2184567Swnj break; 2194497Swnj 2205280Sroot /* 2215280Sroot * Do a send by putting data in output queue and updating urgent 2225280Sroot * marker if URG set. Possibly send more data. 2235280Sroot */ 2244809Swnj case PRU_SEND: 2255075Swnj sbappend(&so->so_snd, m); 2266506Ssam #ifdef notdef 2275089Swnj if (tp->t_flags & TF_PUSH) 2285075Swnj tp->snd_end = tp->snd_una + so->so_snd.sb_cc; 2296506Ssam #endif 2306506Ssam error = tcp_output(tp); 23112766Ssam if (error) { /* XXX fix to use other path */ 23212766Ssam if (error == ENOBUFS) /* XXX */ 23312766Ssam error = 0; /* XXX */ 23412766Ssam tcpsenderrors++; 23512766Ssam } 2364567Swnj break; 2374567Swnj 2385280Sroot /* 2395280Sroot * Abort the TCP. 2405280Sroot */ 2414809Swnj case PRU_ABORT: 24210397Ssam tp = tcp_drop(tp, ECONNABORTED); 2434567Swnj break; 2444567Swnj 2455113Swnj case PRU_SENSE: 24616989Skarels ((struct stat *) m)->st_blksize = so->so_snd.sb_hiwat; 24716989Skarels return (0); 2485113Swnj 2495113Swnj case PRU_RCVOOB: 25024821Skarels if ((so->so_oobmark == 0 && 25124821Skarels (so->so_state & SS_RCVATMARK) == 0) || 25227195Skarels so->so_options & SO_OOBINLINE || 25324821Skarels tp->t_oobflags & TCPOOB_HADDATA) { 2545417Swnj error = EINVAL; 2555417Swnj break; 2565417Swnj } 2575549Swnj if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) { 2585442Swnj error = EWOULDBLOCK; 2595549Swnj break; 2605442Swnj } 2618310Sroot m->m_len = 1; 2625549Swnj *mtod(m, caddr_t) = tp->t_iobc; 26324821Skarels if (((int)nam & MSG_PEEK) == 0) 26424821Skarels tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA); 2655113Swnj break; 2665113Swnj 2675113Swnj case PRU_SENDOOB: 2685442Swnj if (sbspace(&so->so_snd) < -512) { 26911229Ssam m_freem(m); 2705442Swnj error = ENOBUFS; 2715442Swnj break; 2725442Swnj } 27327195Skarels /* 27427195Skarels * According to RFC961 (Assigned Protocols), 27527195Skarels * the urgent pointer points to the last octet 27627195Skarels * of urgent data. We continue, however, 27727195Skarels * to consider it to indicate the first octet 27827195Skarels * of data past the urgent section. 27927195Skarels * Otherwise, snd_up should be one lower. 28027195Skarels */ 2815417Swnj sbappend(&so->so_snd, m); 28227195Skarels tp->snd_up = tp->snd_una + so->so_snd.sb_cc; 2835549Swnj tp->t_force = 1; 2846506Ssam error = tcp_output(tp); 2855549Swnj tp->t_force = 0; 2865113Swnj break; 2875113Swnj 2886510Ssam case PRU_SOCKADDR: 2898272Sroot in_setsockaddr(inp, nam); 2906510Ssam break; 2916510Ssam 29214123Ssam case PRU_PEERADDR: 29314123Ssam in_setpeeraddr(inp, nam); 29414123Ssam break; 29514123Ssam 2965280Sroot /* 2975280Sroot * TCP slow timer went off; going through this 2985280Sroot * routine for tracing's sake. 2995280Sroot */ 3004809Swnj case PRU_SLOWTIMO: 30110397Ssam tp = tcp_timers(tp, (int)nam); 3028272Sroot req |= (int)nam << 8; /* for debug's sake */ 3034809Swnj break; 3044809Swnj 3054731Swnj default: 3064731Swnj panic("tcp_usrreq"); 3074567Swnj } 3085270Sroot if (tp && (so->so_options & SO_DEBUG)) 3095270Sroot tcp_trace(TA_USER, ostate, tp, (struct tcpiphdr *)0, req); 3104567Swnj splx(s); 3114886Swnj return (error); 3124497Swnj } 3135245Sroot 31425896Skarels tcp_ctloutput(op, so, level, optname, mp) 31524821Skarels int op; 31624821Skarels struct socket *so; 31724821Skarels int level, optname; 31825896Skarels struct mbuf **mp; 31924821Skarels { 32025896Skarels int error = 0; 32125896Skarels struct inpcb *inp = sotoinpcb(so); 32225896Skarels register struct tcpcb *tp = intotcpcb(inp); 32325896Skarels register struct mbuf *m; 32425896Skarels 32524821Skarels if (level != IPPROTO_TCP) 32626248Skarels return (ip_ctloutput(op, so, level, optname, mp)); 32725896Skarels 32825896Skarels switch (op) { 32925896Skarels 33025896Skarels case PRCO_SETOPT: 33125896Skarels m = *mp; 33225896Skarels switch (optname) { 33325896Skarels 33425896Skarels case TCP_NODELAY: 33525896Skarels if (m == NULL || m->m_len < sizeof (int)) 33625896Skarels error = EINVAL; 33725896Skarels else if (*mtod(m, int *)) 33825896Skarels tp->t_flags |= TF_NODELAY; 33925896Skarels else 34025896Skarels tp->t_flags &= ~TF_NODELAY; 34125896Skarels break; 34225896Skarels 34325896Skarels case TCP_MAXSEG: /* not yet */ 34425896Skarels default: 34525896Skarels error = EINVAL; 34625896Skarels break; 34725896Skarels } 34826388Skarels (void)m_free(m); 34925896Skarels break; 35025896Skarels 35125896Skarels case PRCO_GETOPT: 35225896Skarels *mp = m = m_get(M_WAIT, MT_SOOPTS); 35325896Skarels m->m_len = sizeof(int); 35425896Skarels 35525896Skarels switch (optname) { 35625896Skarels case TCP_NODELAY: 35725896Skarels *mtod(m, int *) = tp->t_flags & TF_NODELAY; 35825896Skarels break; 35925896Skarels case TCP_MAXSEG: 36025896Skarels *mtod(m, int *) = tp->t_maxseg; 36125896Skarels break; 36225896Skarels default: 36325896Skarels error = EINVAL; 36425896Skarels break; 36525896Skarels } 36625896Skarels break; 36725896Skarels } 36825896Skarels return (error); 36924821Skarels } 37024821Skarels 37118367Skarels int tcp_sendspace = 1024*4; 37218367Skarels int tcp_recvspace = 1024*4; 3735280Sroot /* 3745280Sroot * Attach TCP protocol to socket, allocating 3755280Sroot * internet protocol control block, tcp control block, 3765280Sroot * bufer space, and entering LISTEN state if to accept connections. 3775280Sroot */ 3788272Sroot tcp_attach(so) 3795280Sroot struct socket *so; 3805280Sroot { 3815280Sroot register struct tcpcb *tp; 3825280Sroot struct inpcb *inp; 3835280Sroot int error; 3845280Sroot 3859031Sroot error = soreserve(so, tcp_sendspace, tcp_recvspace); 3865280Sroot if (error) 38717047Skarels return (error); 3887511Sroot error = in_pcballoc(so, &tcb); 3897511Sroot if (error) 39017047Skarels return (error); 3918272Sroot inp = sotoinpcb(so); 3925280Sroot tp = tcp_newtcpcb(inp); 3937511Sroot if (tp == 0) { 39417047Skarels int nofd = so->so_state & SS_NOFDREF; /* XXX */ 39517047Skarels 39617047Skarels so->so_state &= ~SS_NOFDREF; /* don't free the socket yet */ 39717047Skarels in_pcbdetach(inp); 39817047Skarels so->so_state |= nofd; 39917047Skarels return (ENOBUFS); 4007511Sroot } 4018272Sroot tp->t_state = TCPS_CLOSED; 4025280Sroot return (0); 4035280Sroot } 4045280Sroot 4055280Sroot /* 4065280Sroot * Initiate (or continue) disconnect. 4075280Sroot * If embryonic state, just send reset (once). 40813221Ssam * If in ``let data drain'' option and linger null, just drop. 4095280Sroot * Otherwise (hard), mark socket disconnecting and drop 4105280Sroot * current input data; switch states based on user close, and 4115280Sroot * send segment to peer (with FIN). 4125280Sroot */ 41310397Ssam struct tcpcb * 4145280Sroot tcp_disconnect(tp) 41510397Ssam register struct tcpcb *tp; 4165280Sroot { 4175280Sroot struct socket *so = tp->t_inpcb->inp_socket; 4185280Sroot 4195280Sroot if (tp->t_state < TCPS_ESTABLISHED) 42010397Ssam tp = tcp_close(tp); 42113221Ssam else if ((so->so_options & SO_LINGER) && so->so_linger == 0) 42210397Ssam tp = tcp_drop(tp, 0); 4235280Sroot else { 4245280Sroot soisdisconnecting(so); 4255280Sroot sbflush(&so->so_rcv); 42610397Ssam tp = tcp_usrclosed(tp); 42710397Ssam if (tp) 42810397Ssam (void) tcp_output(tp); 4295280Sroot } 43010397Ssam return (tp); 4315280Sroot } 4325280Sroot 4335280Sroot /* 4345280Sroot * User issued close, and wish to trail through shutdown states: 4355280Sroot * if never received SYN, just forget it. If got a SYN from peer, 4365280Sroot * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN. 4375280Sroot * If already got a FIN from peer, then almost done; go to LAST_ACK 4385280Sroot * state. In all other cases, have already sent FIN to peer (e.g. 4395280Sroot * after PRU_SHUTDOWN), and just have to play tedious game waiting 4405280Sroot * for peer to send FIN or not respond to keep-alives, etc. 4415897Swnj * We can let the user exit from the close as soon as the FIN is acked. 4425280Sroot */ 44310397Ssam struct tcpcb * 4445245Sroot tcp_usrclosed(tp) 44510397Ssam register struct tcpcb *tp; 4465245Sroot { 4475245Sroot 4485245Sroot switch (tp->t_state) { 4495245Sroot 45012438Ssam case TCPS_CLOSED: 4515245Sroot case TCPS_LISTEN: 4525245Sroot case TCPS_SYN_SENT: 4535245Sroot tp->t_state = TCPS_CLOSED; 45410397Ssam tp = tcp_close(tp); 4555245Sroot break; 4565245Sroot 4575245Sroot case TCPS_SYN_RECEIVED: 4585245Sroot case TCPS_ESTABLISHED: 4595245Sroot tp->t_state = TCPS_FIN_WAIT_1; 4605245Sroot break; 4615245Sroot 4625245Sroot case TCPS_CLOSE_WAIT: 4635245Sroot tp->t_state = TCPS_LAST_ACK; 4645245Sroot break; 4655245Sroot } 46610397Ssam if (tp && tp->t_state >= TCPS_FIN_WAIT_2) 4675897Swnj soisdisconnected(tp->t_inpcb->inp_socket); 46810397Ssam return (tp); 4695245Sroot } 470