1*23196Smckusick /* 2*23196Smckusick * Copyright (c) 1982 Regents of the University of California. 3*23196Smckusick * All rights reserved. The Berkeley software License Agreement 4*23196Smckusick * specifies the terms and conditions for redistribution. 5*23196Smckusick * 6*23196Smckusick * @(#)tcp_usrreq.c 6.6 (Berkeley) 06/08/85 7*23196Smckusick */ 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: 2495442Swnj if (so->so_oobmark == 0 && 2505442Swnj (so->so_state & SS_RCVATMARK) == 0) { 2515417Swnj error = EINVAL; 2525417Swnj break; 2535417Swnj } 2545549Swnj if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) { 2555442Swnj error = EWOULDBLOCK; 2565549Swnj break; 2575442Swnj } 2588310Sroot m->m_len = 1; 2595549Swnj *mtod(m, caddr_t) = tp->t_iobc; 2605113Swnj break; 2615113Swnj 2625113Swnj case PRU_SENDOOB: 2635442Swnj if (sbspace(&so->so_snd) < -512) { 26411229Ssam m_freem(m); 2655442Swnj error = ENOBUFS; 2665442Swnj break; 2675442Swnj } 2685417Swnj tp->snd_up = tp->snd_una + so->so_snd.sb_cc + 1; 2695417Swnj sbappend(&so->so_snd, m); 2705549Swnj tp->t_force = 1; 2716506Ssam error = tcp_output(tp); 2725549Swnj tp->t_force = 0; 2735113Swnj break; 2745113Swnj 2756510Ssam case PRU_SOCKADDR: 2768272Sroot in_setsockaddr(inp, nam); 2776510Ssam break; 2786510Ssam 27914123Ssam case PRU_PEERADDR: 28014123Ssam in_setpeeraddr(inp, nam); 28114123Ssam break; 28214123Ssam 2835280Sroot /* 2845280Sroot * TCP slow timer went off; going through this 2855280Sroot * routine for tracing's sake. 2865280Sroot */ 2874809Swnj case PRU_SLOWTIMO: 28810397Ssam tp = tcp_timers(tp, (int)nam); 2898272Sroot req |= (int)nam << 8; /* for debug's sake */ 2904809Swnj break; 2914809Swnj 2924731Swnj default: 2934731Swnj panic("tcp_usrreq"); 2944567Swnj } 2955270Sroot if (tp && (so->so_options & SO_DEBUG)) 2965270Sroot tcp_trace(TA_USER, ostate, tp, (struct tcpiphdr *)0, req); 2974567Swnj splx(s); 2984886Swnj return (error); 2994497Swnj } 3005245Sroot 30118367Skarels int tcp_sendspace = 1024*4; 30218367Skarels int tcp_recvspace = 1024*4; 3035280Sroot /* 3045280Sroot * Attach TCP protocol to socket, allocating 3055280Sroot * internet protocol control block, tcp control block, 3065280Sroot * bufer space, and entering LISTEN state if to accept connections. 3075280Sroot */ 3088272Sroot tcp_attach(so) 3095280Sroot struct socket *so; 3105280Sroot { 3115280Sroot register struct tcpcb *tp; 3125280Sroot struct inpcb *inp; 3135280Sroot int error; 3145280Sroot 3159031Sroot error = soreserve(so, tcp_sendspace, tcp_recvspace); 3165280Sroot if (error) 31717047Skarels return (error); 3187511Sroot error = in_pcballoc(so, &tcb); 3197511Sroot if (error) 32017047Skarels return (error); 3218272Sroot inp = sotoinpcb(so); 3225280Sroot tp = tcp_newtcpcb(inp); 3237511Sroot if (tp == 0) { 32417047Skarels int nofd = so->so_state & SS_NOFDREF; /* XXX */ 32517047Skarels 32617047Skarels so->so_state &= ~SS_NOFDREF; /* don't free the socket yet */ 32717047Skarels in_pcbdetach(inp); 32817047Skarels so->so_state |= nofd; 32917047Skarels return (ENOBUFS); 3307511Sroot } 3318272Sroot tp->t_state = TCPS_CLOSED; 3325280Sroot return (0); 3335280Sroot } 3345280Sroot 3355280Sroot /* 3365280Sroot * Initiate (or continue) disconnect. 3375280Sroot * If embryonic state, just send reset (once). 33813221Ssam * If in ``let data drain'' option and linger null, just drop. 3395280Sroot * Otherwise (hard), mark socket disconnecting and drop 3405280Sroot * current input data; switch states based on user close, and 3415280Sroot * send segment to peer (with FIN). 3425280Sroot */ 34310397Ssam struct tcpcb * 3445280Sroot tcp_disconnect(tp) 34510397Ssam register struct tcpcb *tp; 3465280Sroot { 3475280Sroot struct socket *so = tp->t_inpcb->inp_socket; 3485280Sroot 3495280Sroot if (tp->t_state < TCPS_ESTABLISHED) 35010397Ssam tp = tcp_close(tp); 35113221Ssam else if ((so->so_options & SO_LINGER) && so->so_linger == 0) 35210397Ssam tp = tcp_drop(tp, 0); 3535280Sroot else { 3545280Sroot soisdisconnecting(so); 3555280Sroot sbflush(&so->so_rcv); 35610397Ssam tp = tcp_usrclosed(tp); 35710397Ssam if (tp) 35810397Ssam (void) tcp_output(tp); 3595280Sroot } 36010397Ssam return (tp); 3615280Sroot } 3625280Sroot 3635280Sroot /* 3645280Sroot * User issued close, and wish to trail through shutdown states: 3655280Sroot * if never received SYN, just forget it. If got a SYN from peer, 3665280Sroot * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN. 3675280Sroot * If already got a FIN from peer, then almost done; go to LAST_ACK 3685280Sroot * state. In all other cases, have already sent FIN to peer (e.g. 3695280Sroot * after PRU_SHUTDOWN), and just have to play tedious game waiting 3705280Sroot * for peer to send FIN or not respond to keep-alives, etc. 3715897Swnj * We can let the user exit from the close as soon as the FIN is acked. 3725280Sroot */ 37310397Ssam struct tcpcb * 3745245Sroot tcp_usrclosed(tp) 37510397Ssam register struct tcpcb *tp; 3765245Sroot { 3775245Sroot 3785245Sroot switch (tp->t_state) { 3795245Sroot 38012438Ssam case TCPS_CLOSED: 3815245Sroot case TCPS_LISTEN: 3825245Sroot case TCPS_SYN_SENT: 3835245Sroot tp->t_state = TCPS_CLOSED; 38410397Ssam tp = tcp_close(tp); 3855245Sroot break; 3865245Sroot 3875245Sroot case TCPS_SYN_RECEIVED: 3885245Sroot case TCPS_ESTABLISHED: 3895245Sroot tp->t_state = TCPS_FIN_WAIT_1; 3905245Sroot break; 3915245Sroot 3925245Sroot case TCPS_CLOSE_WAIT: 3935245Sroot tp->t_state = TCPS_LAST_ACK; 3945245Sroot break; 3955245Sroot } 39610397Ssam if (tp && tp->t_state >= TCPS_FIN_WAIT_2) 3975897Swnj soisdisconnected(tp->t_inpcb->inp_socket); 39810397Ssam return (tp); 3995245Sroot } 400