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*31041Ssam * @(#)tcp_usrreq.c 7.5 (Berkeley) 05/08/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 { 5230909Skarels register struct inpcb *inp; 534911Swnj register struct tcpcb *tp; 5430909Skarels int s; 554809Swnj int error = 0; 565270Sroot int ostate; 574497Swnj 5830909Skarels if (req == PRU_CONTROL) 5930909Skarels return (in_control(so, (int)m, (caddr_t)nam, 6030909Skarels (struct ifnet *)rights)); 6130909Skarels if (rights && rights->m_len) 6212766Ssam return (EINVAL); 6330909Skarels 6430909Skarels s = splnet(); 6530909Skarels 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 } 346*31041Ssam if (m) 347*31041Ssam (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