123196Smckusick /* 237323Skarels * Copyright (c) 1982, 1986, 1988 Regents of the University of California. 332789Sbostic * All rights reserved. 423196Smckusick * 544491Sbostic * %sccs.include.redist.c% 632789Sbostic * 7*56531Sbostic * @(#)tcp_usrreq.c 7.17 (Berkeley) 10/11/92 823196Smckusick */ 94567Swnj 10*56531Sbostic #include <sys/param.h> 11*56531Sbostic #include <sys/systm.h> 12*56531Sbostic #include <sys/malloc.h> 13*56531Sbostic #include <sys/mbuf.h> 14*56531Sbostic #include <sys/socket.h> 15*56531Sbostic #include <sys/socketvar.h> 16*56531Sbostic #include <sys/protosw.h> 17*56531Sbostic #include <sys/errno.h> 18*56531Sbostic #include <sys/stat.h> 198697Sroot 20*56531Sbostic #include <net/if.h> 21*56531Sbostic #include <net/route.h> 2210896Ssam 23*56531Sbostic #include <netinet/in.h> 24*56531Sbostic #include <netinet/in_systm.h> 25*56531Sbostic #include <netinet/ip.h> 26*56531Sbostic #include <netinet/in_pcb.h> 27*56531Sbostic #include <netinet/ip_var.h> 28*56531Sbostic #include <netinet/tcp.h> 29*56531Sbostic #include <netinet/tcp_fsm.h> 30*56531Sbostic #include <netinet/tcp_seq.h> 31*56531Sbostic #include <netinet/tcp_timer.h> 32*56531Sbostic #include <netinet/tcp_var.h> 33*56531Sbostic #include <netinet/tcpip.h> 34*56531Sbostic #include <netinet/tcp_debug.h> 354497Swnj 365280Sroot /* 375280Sroot * TCP protocol interface to socket abstraction. 385280Sroot */ 395280Sroot extern char *tcpstates[]; 404954Swnj struct tcpcb *tcp_newtcpcb(); 415280Sroot 424734Swnj /* 435280Sroot * Process a TCP user request for TCP tb. If this is a send request 444731Swnj * then m is the mbuf chain of send data. If this is a timer expiration 454731Swnj * (called from the software clock routine), then timertype tells which timer. 464731Swnj */ 478601Sroot /*ARGSUSED*/ 4842184Skarels tcp_usrreq(so, req, m, nam, control) 494809Swnj struct socket *so; 504809Swnj int req; 5142184Skarels struct mbuf *m, *nam, *control; 524497Swnj { 5330909Skarels register struct inpcb *inp; 544911Swnj register struct tcpcb *tp; 5530909Skarels int s; 564809Swnj int error = 0; 575270Sroot int ostate; 584497Swnj 5930909Skarels if (req == PRU_CONTROL) 6030909Skarels return (in_control(so, (int)m, (caddr_t)nam, 6142184Skarels (struct ifnet *)control)); 6242184Skarels if (control && control->m_len) { 6342184Skarels m_freem(control); 6442184Skarels if (m) 6542184Skarels m_freem(m); 6612766Ssam return (EINVAL); 6742184Skarels } 6830909Skarels 6930909Skarels s = splnet(); 7030909Skarels inp = sotoinpcb(so); 714886Swnj /* 725280Sroot * When a TCP is attached to a socket, then there will be 735280Sroot * a (struct inpcb) pointed at by the socket, and this 745280Sroot * structure will point at a subsidary (struct tcpcb). 754886Swnj */ 765089Swnj if (inp == 0 && req != PRU_ATTACH) { 775075Swnj splx(s); 785280Sroot return (EINVAL); /* XXX */ 795075Swnj } 805075Swnj if (inp) { 814911Swnj tp = intotcpcb(inp); 828272Sroot /* WHAT IF TP IS 0? */ 834731Swnj #ifdef KPROF 845075Swnj tcp_acounts[tp->t_state][req]++; 854731Swnj #endif 865270Sroot ostate = tp->t_state; 877511Sroot } else 887511Sroot ostate = 0; 894809Swnj switch (req) { 904497Swnj 915280Sroot /* 925280Sroot * TCP attaches to socket via PRU_ATTACH, reserving space, 938272Sroot * and an internet control block. 945280Sroot */ 954809Swnj case PRU_ATTACH: 964954Swnj if (inp) { 974809Swnj error = EISCONN; 984911Swnj break; 994886Swnj } 1008640Sroot error = tcp_attach(so); 1015075Swnj if (error) 1024954Swnj break; 10310397Ssam if ((so->so_options & SO_LINGER) && so->so_linger == 0) 1045392Swnj so->so_linger = TCP_LINGERTIME; 1055280Sroot tp = sototcpcb(so); 1064567Swnj break; 1074497Swnj 1085280Sroot /* 1095280Sroot * PRU_DETACH detaches the TCP protocol from the socket. 1105280Sroot * If the protocol state is non-embryonic, then can't 1115280Sroot * do this directly: have to initiate a PRU_DISCONNECT, 1125280Sroot * which may finish later; embryonic TCB's can just 1135280Sroot * be discarded here. 1145280Sroot */ 1154809Swnj case PRU_DETACH: 1165280Sroot if (tp->t_state > TCPS_LISTEN) 11710397Ssam tp = tcp_disconnect(tp); 11810397Ssam else 11910397Ssam tp = tcp_close(tp); 1204809Swnj break; 1214809Swnj 1225280Sroot /* 1238272Sroot * Give the socket an address. 1248272Sroot */ 1258272Sroot case PRU_BIND: 1268272Sroot error = in_pcbbind(inp, nam); 1278272Sroot if (error) 1288272Sroot break; 1298272Sroot break; 1308272Sroot 1318272Sroot /* 1328272Sroot * Prepare to accept connections. 1338272Sroot */ 1348272Sroot case PRU_LISTEN: 1358272Sroot if (inp->inp_lport == 0) 1368272Sroot error = in_pcbbind(inp, (struct mbuf *)0); 1378272Sroot if (error == 0) 1388272Sroot tp->t_state = TCPS_LISTEN; 1398272Sroot break; 1408272Sroot 1418272Sroot /* 1425280Sroot * Initiate connection to peer. 1435280Sroot * Create a template for use in transmissions on this connection. 1445280Sroot * Enter SYN_SENT state, and mark socket as connecting. 1455280Sroot * Start keep-alive timer, and seed output sequence space. 1465280Sroot * Send initial segment on connection. 1475280Sroot */ 1484809Swnj case PRU_CONNECT: 1498272Sroot if (inp->inp_lport == 0) { 1508272Sroot error = in_pcbbind(inp, (struct mbuf *)0); 1518272Sroot if (error) 1528272Sroot break; 1538272Sroot } 1548272Sroot error = in_pcbconnect(inp, nam); 1554954Swnj if (error) 1564886Swnj break; 1575174Swnj tp->t_template = tcp_template(tp); 1585280Sroot if (tp->t_template == 0) { 1595280Sroot in_pcbdisconnect(inp); 1605280Sroot error = ENOBUFS; 1615280Sroot break; 1625280Sroot } 1634886Swnj soisconnecting(so); 16430527Skarels tcpstat.tcps_connattempt++; 1655075Swnj tp->t_state = TCPS_SYN_SENT; 16633747Skarels tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT; 1675245Sroot tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2; 1685245Sroot tcp_sendseqinit(tp); 1696506Ssam error = tcp_output(tp); 1704567Swnj break; 1714497Swnj 1725280Sroot /* 17313117Ssam * Create a TCP connection between two sockets. 17413117Ssam */ 17513117Ssam case PRU_CONNECT2: 17613117Ssam error = EOPNOTSUPP; 17713117Ssam break; 17813117Ssam 17913117Ssam /* 1805280Sroot * Initiate disconnect from peer. 1815280Sroot * If connection never passed embryonic stage, just drop; 1825280Sroot * else if don't need to let data drain, then can just drop anyways, 1835280Sroot * else have to begin TCP shutdown process: mark socket disconnecting, 1845280Sroot * drain unread data, state switch to reflect user close, and 1855280Sroot * send segment (e.g. FIN) to peer. Socket will be really disconnected 1865280Sroot * when peer sends FIN and acks ours. 1875280Sroot * 1885280Sroot * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB. 1895280Sroot */ 1905280Sroot case PRU_DISCONNECT: 19110397Ssam tp = tcp_disconnect(tp); 1925245Sroot break; 1935245Sroot 1945280Sroot /* 1955280Sroot * Accept a connection. Essentially all the work is 1965280Sroot * done at higher levels; just return the address 1975280Sroot * of the peer, storing through addr. 1985280Sroot */ 1996117Swnj case PRU_ACCEPT: { 2008272Sroot struct sockaddr_in *sin = mtod(nam, struct sockaddr_in *); 2016117Swnj 2028272Sroot nam->m_len = sizeof (struct sockaddr_in); 2038272Sroot sin->sin_family = AF_INET; 20437323Skarels sin->sin_len = sizeof(*sin); 2058272Sroot sin->sin_port = inp->inp_fport; 2068272Sroot sin->sin_addr = inp->inp_faddr; 2078272Sroot break; 2086117Swnj } 2094925Swnj 2105280Sroot /* 2115280Sroot * Mark the connection as being incapable of further output. 2125280Sroot */ 2134809Swnj case PRU_SHUTDOWN: 2145089Swnj socantsendmore(so); 21510397Ssam tp = tcp_usrclosed(tp); 21610397Ssam if (tp) 21710397Ssam error = tcp_output(tp); 2184567Swnj break; 2194497Swnj 2205280Sroot /* 2215280Sroot * After a receive, possibly send window update to peer. 2225280Sroot */ 2234809Swnj case PRU_RCVD: 2245113Swnj (void) tcp_output(tp); 2254567Swnj break; 2264497Swnj 2275280Sroot /* 2285280Sroot * Do a send by putting data in output queue and updating urgent 2295280Sroot * marker if URG set. Possibly send more data. 2305280Sroot */ 2314809Swnj case PRU_SEND: 2325075Swnj sbappend(&so->so_snd, m); 2336506Ssam error = tcp_output(tp); 2344567Swnj break; 2354567Swnj 2365280Sroot /* 2375280Sroot * Abort the TCP. 2385280Sroot */ 2394809Swnj case PRU_ABORT: 24010397Ssam tp = tcp_drop(tp, ECONNABORTED); 2414567Swnj break; 2424567Swnj 2435113Swnj case PRU_SENSE: 24416989Skarels ((struct stat *) m)->st_blksize = so->so_snd.sb_hiwat; 24530871Smckusick (void) splx(s); 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; 32355287Smckusick register int i; 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 34355287Smckusick case TCP_MAXSEG: 34455287Smckusick if (m && (i = *mtod(m, int *)) > 0 && i <= tp->t_maxseg) 34555287Smckusick tp->t_maxseg = i; 34655287Smckusick else 34755287Smckusick error = EINVAL; 34855287Smckusick break; 34955287Smckusick 35025896Skarels default: 35125896Skarels error = EINVAL; 35225896Skarels break; 35325896Skarels } 35431041Ssam if (m) 35531041Ssam (void) m_free(m); 35625896Skarels break; 35725896Skarels 35825896Skarels case PRCO_GETOPT: 35925896Skarels *mp = m = m_get(M_WAIT, MT_SOOPTS); 36025896Skarels m->m_len = sizeof(int); 36125896Skarels 36225896Skarels switch (optname) { 36325896Skarels case TCP_NODELAY: 36425896Skarels *mtod(m, int *) = tp->t_flags & TF_NODELAY; 36525896Skarels break; 36625896Skarels case TCP_MAXSEG: 36725896Skarels *mtod(m, int *) = tp->t_maxseg; 36825896Skarels break; 36925896Skarels default: 37025896Skarels error = EINVAL; 37125896Skarels break; 37225896Skarels } 37325896Skarels break; 37425896Skarels } 37525896Skarels return (error); 37624821Skarels } 37724821Skarels 37855287Smckusick u_long tcp_sendspace = 1024*8; 37955287Smckusick u_long tcp_recvspace = 1024*8; 38037323Skarels 3815280Sroot /* 3825280Sroot * Attach TCP protocol to socket, allocating 3835280Sroot * internet protocol control block, tcp control block, 3845280Sroot * bufer space, and entering LISTEN state if to accept connections. 3855280Sroot */ 3868272Sroot tcp_attach(so) 3875280Sroot struct socket *so; 3885280Sroot { 3895280Sroot register struct tcpcb *tp; 3905280Sroot struct inpcb *inp; 3915280Sroot int error; 3925280Sroot 39334485Skarels if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 39434485Skarels error = soreserve(so, tcp_sendspace, tcp_recvspace); 39534485Skarels if (error) 39634485Skarels return (error); 39734485Skarels } 3987511Sroot error = in_pcballoc(so, &tcb); 3997511Sroot if (error) 40017047Skarels return (error); 4018272Sroot inp = sotoinpcb(so); 4025280Sroot tp = tcp_newtcpcb(inp); 4037511Sroot if (tp == 0) { 40417047Skarels int nofd = so->so_state & SS_NOFDREF; /* XXX */ 40517047Skarels 40617047Skarels so->so_state &= ~SS_NOFDREF; /* don't free the socket yet */ 40717047Skarels in_pcbdetach(inp); 40817047Skarels so->so_state |= nofd; 40917047Skarels return (ENOBUFS); 4107511Sroot } 4118272Sroot tp->t_state = TCPS_CLOSED; 4125280Sroot return (0); 4135280Sroot } 4145280Sroot 4155280Sroot /* 4165280Sroot * Initiate (or continue) disconnect. 4175280Sroot * If embryonic state, just send reset (once). 41813221Ssam * If in ``let data drain'' option and linger null, just drop. 4195280Sroot * Otherwise (hard), mark socket disconnecting and drop 4205280Sroot * current input data; switch states based on user close, and 4215280Sroot * send segment to peer (with FIN). 4225280Sroot */ 42310397Ssam struct tcpcb * 4245280Sroot tcp_disconnect(tp) 42510397Ssam register struct tcpcb *tp; 4265280Sroot { 4275280Sroot struct socket *so = tp->t_inpcb->inp_socket; 4285280Sroot 4295280Sroot if (tp->t_state < TCPS_ESTABLISHED) 43010397Ssam tp = tcp_close(tp); 43113221Ssam else if ((so->so_options & SO_LINGER) && so->so_linger == 0) 43210397Ssam tp = tcp_drop(tp, 0); 4335280Sroot else { 4345280Sroot soisdisconnecting(so); 4355280Sroot sbflush(&so->so_rcv); 43610397Ssam tp = tcp_usrclosed(tp); 43710397Ssam if (tp) 43810397Ssam (void) tcp_output(tp); 4395280Sroot } 44010397Ssam return (tp); 4415280Sroot } 4425280Sroot 4435280Sroot /* 4445280Sroot * User issued close, and wish to trail through shutdown states: 4455280Sroot * if never received SYN, just forget it. If got a SYN from peer, 4465280Sroot * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN. 4475280Sroot * If already got a FIN from peer, then almost done; go to LAST_ACK 4485280Sroot * state. In all other cases, have already sent FIN to peer (e.g. 4495280Sroot * after PRU_SHUTDOWN), and just have to play tedious game waiting 4505280Sroot * for peer to send FIN or not respond to keep-alives, etc. 4515897Swnj * We can let the user exit from the close as soon as the FIN is acked. 4525280Sroot */ 45310397Ssam struct tcpcb * 4545245Sroot tcp_usrclosed(tp) 45510397Ssam register struct tcpcb *tp; 4565245Sroot { 4575245Sroot 4585245Sroot switch (tp->t_state) { 4595245Sroot 46012438Ssam case TCPS_CLOSED: 4615245Sroot case TCPS_LISTEN: 4625245Sroot case TCPS_SYN_SENT: 4635245Sroot tp->t_state = TCPS_CLOSED; 46410397Ssam tp = tcp_close(tp); 4655245Sroot break; 4665245Sroot 4675245Sroot case TCPS_SYN_RECEIVED: 4685245Sroot case TCPS_ESTABLISHED: 4695245Sroot tp->t_state = TCPS_FIN_WAIT_1; 4705245Sroot break; 4715245Sroot 4725245Sroot case TCPS_CLOSE_WAIT: 4735245Sroot tp->t_state = TCPS_LAST_ACK; 4745245Sroot break; 4755245Sroot } 47610397Ssam if (tp && tp->t_state >= TCPS_FIN_WAIT_2) 4775897Swnj soisdisconnected(tp->t_inpcb->inp_socket); 47810397Ssam return (tp); 4795245Sroot } 480