123196Smckusick /* 223196Smckusick * Copyright (c) 1982 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*25896Skarels * @(#)tcp_usrreq.c 6.8 (Berkeley) 01/13/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) || 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 } 2715417Swnj tp->snd_up = tp->snd_una + so->so_snd.sb_cc + 1; 2725417Swnj sbappend(&so->so_snd, m); 2735549Swnj tp->t_force = 1; 2746506Ssam error = tcp_output(tp); 2755549Swnj tp->t_force = 0; 2765113Swnj break; 2775113Swnj 2786510Ssam case PRU_SOCKADDR: 2798272Sroot in_setsockaddr(inp, nam); 2806510Ssam break; 2816510Ssam 28214123Ssam case PRU_PEERADDR: 28314123Ssam in_setpeeraddr(inp, nam); 28414123Ssam break; 28514123Ssam 2865280Sroot /* 2875280Sroot * TCP slow timer went off; going through this 2885280Sroot * routine for tracing's sake. 2895280Sroot */ 2904809Swnj case PRU_SLOWTIMO: 29110397Ssam tp = tcp_timers(tp, (int)nam); 2928272Sroot req |= (int)nam << 8; /* for debug's sake */ 2934809Swnj break; 2944809Swnj 2954731Swnj default: 2964731Swnj panic("tcp_usrreq"); 2974567Swnj } 2985270Sroot if (tp && (so->so_options & SO_DEBUG)) 2995270Sroot tcp_trace(TA_USER, ostate, tp, (struct tcpiphdr *)0, req); 3004567Swnj splx(s); 3014886Swnj return (error); 3024497Swnj } 3035245Sroot 304*25896Skarels tcp_ctloutput(op, so, level, optname, mp) 30524821Skarels int op; 30624821Skarels struct socket *so; 30724821Skarels int level, optname; 308*25896Skarels struct mbuf **mp; 30924821Skarels { 310*25896Skarels int error = 0; 311*25896Skarels struct inpcb *inp = sotoinpcb(so); 312*25896Skarels register struct tcpcb *tp = intotcpcb(inp); 313*25896Skarels register struct mbuf *m; 314*25896Skarels 31524821Skarels if (level != IPPROTO_TCP) 31624821Skarels return (ip_ctloutput(op, so, level, optname, m)); 317*25896Skarels 318*25896Skarels switch (op) { 319*25896Skarels 320*25896Skarels case PRCO_SETOPT: 321*25896Skarels m = *mp; 322*25896Skarels switch (optname) { 323*25896Skarels 324*25896Skarels case TCP_NODELAY: 325*25896Skarels if (m == NULL || m->m_len < sizeof (int)) 326*25896Skarels error = EINVAL; 327*25896Skarels else if (*mtod(m, int *)) 328*25896Skarels tp->t_flags |= TF_NODELAY; 329*25896Skarels else 330*25896Skarels tp->t_flags &= ~TF_NODELAY; 331*25896Skarels break; 332*25896Skarels 333*25896Skarels case TCP_MAXSEG: /* not yet */ 334*25896Skarels default: 335*25896Skarels error = EINVAL; 336*25896Skarels break; 337*25896Skarels } 338*25896Skarels m_free(m); 339*25896Skarels break; 340*25896Skarels 341*25896Skarels case PRCO_GETOPT: 342*25896Skarels *mp = m = m_get(M_WAIT, MT_SOOPTS); 343*25896Skarels m->m_len = sizeof(int); 344*25896Skarels 345*25896Skarels switch (optname) { 346*25896Skarels case TCP_NODELAY: 347*25896Skarels *mtod(m, int *) = tp->t_flags & TF_NODELAY; 348*25896Skarels break; 349*25896Skarels case TCP_MAXSEG: 350*25896Skarels *mtod(m, int *) = tp->t_maxseg; 351*25896Skarels break; 352*25896Skarels default: 353*25896Skarels error = EINVAL; 354*25896Skarels break; 355*25896Skarels } 356*25896Skarels break; 357*25896Skarels } 358*25896Skarels return (error); 35924821Skarels } 36024821Skarels 36118367Skarels int tcp_sendspace = 1024*4; 36218367Skarels int tcp_recvspace = 1024*4; 3635280Sroot /* 3645280Sroot * Attach TCP protocol to socket, allocating 3655280Sroot * internet protocol control block, tcp control block, 3665280Sroot * bufer space, and entering LISTEN state if to accept connections. 3675280Sroot */ 3688272Sroot tcp_attach(so) 3695280Sroot struct socket *so; 3705280Sroot { 3715280Sroot register struct tcpcb *tp; 3725280Sroot struct inpcb *inp; 3735280Sroot int error; 3745280Sroot 3759031Sroot error = soreserve(so, tcp_sendspace, tcp_recvspace); 3765280Sroot if (error) 37717047Skarels return (error); 3787511Sroot error = in_pcballoc(so, &tcb); 3797511Sroot if (error) 38017047Skarels return (error); 3818272Sroot inp = sotoinpcb(so); 3825280Sroot tp = tcp_newtcpcb(inp); 3837511Sroot if (tp == 0) { 38417047Skarels int nofd = so->so_state & SS_NOFDREF; /* XXX */ 38517047Skarels 38617047Skarels so->so_state &= ~SS_NOFDREF; /* don't free the socket yet */ 38717047Skarels in_pcbdetach(inp); 38817047Skarels so->so_state |= nofd; 38917047Skarels return (ENOBUFS); 3907511Sroot } 3918272Sroot tp->t_state = TCPS_CLOSED; 3925280Sroot return (0); 3935280Sroot } 3945280Sroot 3955280Sroot /* 3965280Sroot * Initiate (or continue) disconnect. 3975280Sroot * If embryonic state, just send reset (once). 39813221Ssam * If in ``let data drain'' option and linger null, just drop. 3995280Sroot * Otherwise (hard), mark socket disconnecting and drop 4005280Sroot * current input data; switch states based on user close, and 4015280Sroot * send segment to peer (with FIN). 4025280Sroot */ 40310397Ssam struct tcpcb * 4045280Sroot tcp_disconnect(tp) 40510397Ssam register struct tcpcb *tp; 4065280Sroot { 4075280Sroot struct socket *so = tp->t_inpcb->inp_socket; 4085280Sroot 4095280Sroot if (tp->t_state < TCPS_ESTABLISHED) 41010397Ssam tp = tcp_close(tp); 41113221Ssam else if ((so->so_options & SO_LINGER) && so->so_linger == 0) 41210397Ssam tp = tcp_drop(tp, 0); 4135280Sroot else { 4145280Sroot soisdisconnecting(so); 4155280Sroot sbflush(&so->so_rcv); 41610397Ssam tp = tcp_usrclosed(tp); 41710397Ssam if (tp) 41810397Ssam (void) tcp_output(tp); 4195280Sroot } 42010397Ssam return (tp); 4215280Sroot } 4225280Sroot 4235280Sroot /* 4245280Sroot * User issued close, and wish to trail through shutdown states: 4255280Sroot * if never received SYN, just forget it. If got a SYN from peer, 4265280Sroot * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN. 4275280Sroot * If already got a FIN from peer, then almost done; go to LAST_ACK 4285280Sroot * state. In all other cases, have already sent FIN to peer (e.g. 4295280Sroot * after PRU_SHUTDOWN), and just have to play tedious game waiting 4305280Sroot * for peer to send FIN or not respond to keep-alives, etc. 4315897Swnj * We can let the user exit from the close as soon as the FIN is acked. 4325280Sroot */ 43310397Ssam struct tcpcb * 4345245Sroot tcp_usrclosed(tp) 43510397Ssam register struct tcpcb *tp; 4365245Sroot { 4375245Sroot 4385245Sroot switch (tp->t_state) { 4395245Sroot 44012438Ssam case TCPS_CLOSED: 4415245Sroot case TCPS_LISTEN: 4425245Sroot case TCPS_SYN_SENT: 4435245Sroot tp->t_state = TCPS_CLOSED; 44410397Ssam tp = tcp_close(tp); 4455245Sroot break; 4465245Sroot 4475245Sroot case TCPS_SYN_RECEIVED: 4485245Sroot case TCPS_ESTABLISHED: 4495245Sroot tp->t_state = TCPS_FIN_WAIT_1; 4505245Sroot break; 4515245Sroot 4525245Sroot case TCPS_CLOSE_WAIT: 4535245Sroot tp->t_state = TCPS_LAST_ACK; 4545245Sroot break; 4555245Sroot } 45610397Ssam if (tp && tp->t_state >= TCPS_FIN_WAIT_2) 4575897Swnj soisdisconnected(tp->t_inpcb->inp_socket); 45810397Ssam return (tp); 4595245Sroot } 460