123196Smckusick /* 229155Smckusick * Copyright (c) 1982, 1986 Regents of the University of California. 332789Sbostic * All rights reserved. 423196Smckusick * 532789Sbostic * Redistribution and use in source and binary forms are permitted 6*34855Sbostic * provided that the above copyright notice and this paragraph are 7*34855Sbostic * duplicated in all such forms and that any documentation, 8*34855Sbostic * advertising materials, and other materials related to such 9*34855Sbostic * distribution and use acknowledge that the software was developed 10*34855Sbostic * by the University of California, Berkeley. The name of the 11*34855Sbostic * University may not be used to endorse or promote products derived 12*34855Sbostic * from this software without specific prior written permission. 13*34855Sbostic * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 14*34855Sbostic * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 15*34855Sbostic * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 1632789Sbostic * 17*34855Sbostic * @(#)tcp_usrreq.c 7.10 (Berkeley) 06/29/88 1823196Smckusick */ 194567Swnj 2017064Sbloom #include "param.h" 2117064Sbloom #include "systm.h" 2217064Sbloom #include "mbuf.h" 2317064Sbloom #include "socket.h" 2417064Sbloom #include "socketvar.h" 2517064Sbloom #include "protosw.h" 2617064Sbloom #include "errno.h" 2717064Sbloom #include "stat.h" 288697Sroot 298697Sroot #include "../net/if.h" 308697Sroot #include "../net/route.h" 3110896Ssam 3217064Sbloom #include "in.h" 3317064Sbloom #include "in_pcb.h" 3417064Sbloom #include "in_systm.h" 3517064Sbloom #include "ip.h" 3617064Sbloom #include "ip_var.h" 3717064Sbloom #include "tcp.h" 3817064Sbloom #include "tcp_fsm.h" 3917064Sbloom #include "tcp_seq.h" 4017064Sbloom #include "tcp_timer.h" 4117064Sbloom #include "tcp_var.h" 4217064Sbloom #include "tcpip.h" 4317064Sbloom #include "tcp_debug.h" 444497Swnj 455280Sroot /* 465280Sroot * TCP protocol interface to socket abstraction. 475280Sroot */ 485280Sroot extern char *tcpstates[]; 494954Swnj struct tcpcb *tcp_newtcpcb(); 505280Sroot 514734Swnj /* 525280Sroot * Process a TCP user request for TCP tb. If this is a send request 534731Swnj * then m is the mbuf chain of send data. If this is a timer expiration 544731Swnj * (called from the software clock routine), then timertype tells which timer. 554731Swnj */ 568601Sroot /*ARGSUSED*/ 5712766Ssam tcp_usrreq(so, req, m, nam, rights) 584809Swnj struct socket *so; 594809Swnj int req; 6012766Ssam struct mbuf *m, *nam, *rights; 614497Swnj { 6230909Skarels register struct inpcb *inp; 634911Swnj register struct tcpcb *tp; 6430909Skarels int s; 654809Swnj int error = 0; 665270Sroot int ostate; 674497Swnj 6830909Skarels if (req == PRU_CONTROL) 6930909Skarels return (in_control(so, (int)m, (caddr_t)nam, 7030909Skarels (struct ifnet *)rights)); 7130909Skarels if (rights && rights->m_len) 7212766Ssam return (EINVAL); 7330909Skarels 7430909Skarels s = splnet(); 7530909Skarels inp = sotoinpcb(so); 764886Swnj /* 775280Sroot * When a TCP is attached to a socket, then there will be 785280Sroot * a (struct inpcb) pointed at by the socket, and this 795280Sroot * structure will point at a subsidary (struct tcpcb). 804886Swnj */ 815089Swnj if (inp == 0 && req != PRU_ATTACH) { 825075Swnj splx(s); 835280Sroot return (EINVAL); /* XXX */ 845075Swnj } 855075Swnj if (inp) { 864911Swnj tp = intotcpcb(inp); 878272Sroot /* WHAT IF TP IS 0? */ 884731Swnj #ifdef KPROF 895075Swnj tcp_acounts[tp->t_state][req]++; 904731Swnj #endif 915270Sroot ostate = tp->t_state; 927511Sroot } else 937511Sroot ostate = 0; 944809Swnj switch (req) { 954497Swnj 965280Sroot /* 975280Sroot * TCP attaches to socket via PRU_ATTACH, reserving space, 988272Sroot * and an internet control block. 995280Sroot */ 1004809Swnj case PRU_ATTACH: 1014954Swnj if (inp) { 1024809Swnj error = EISCONN; 1034911Swnj break; 1044886Swnj } 1058640Sroot error = tcp_attach(so); 1065075Swnj if (error) 1074954Swnj break; 10810397Ssam if ((so->so_options & SO_LINGER) && so->so_linger == 0) 1095392Swnj so->so_linger = TCP_LINGERTIME; 1105280Sroot tp = sototcpcb(so); 1114567Swnj break; 1124497Swnj 1135280Sroot /* 1145280Sroot * PRU_DETACH detaches the TCP protocol from the socket. 1155280Sroot * If the protocol state is non-embryonic, then can't 1165280Sroot * do this directly: have to initiate a PRU_DISCONNECT, 1175280Sroot * which may finish later; embryonic TCB's can just 1185280Sroot * be discarded here. 1195280Sroot */ 1204809Swnj case PRU_DETACH: 1215280Sroot if (tp->t_state > TCPS_LISTEN) 12210397Ssam tp = tcp_disconnect(tp); 12310397Ssam else 12410397Ssam tp = tcp_close(tp); 1254809Swnj break; 1264809Swnj 1275280Sroot /* 1288272Sroot * Give the socket an address. 1298272Sroot */ 1308272Sroot case PRU_BIND: 1318272Sroot error = in_pcbbind(inp, nam); 1328272Sroot if (error) 1338272Sroot break; 1348272Sroot break; 1358272Sroot 1368272Sroot /* 1378272Sroot * Prepare to accept connections. 1388272Sroot */ 1398272Sroot case PRU_LISTEN: 1408272Sroot if (inp->inp_lport == 0) 1418272Sroot error = in_pcbbind(inp, (struct mbuf *)0); 1428272Sroot if (error == 0) 1438272Sroot tp->t_state = TCPS_LISTEN; 1448272Sroot break; 1458272Sroot 1468272Sroot /* 1475280Sroot * Initiate connection to peer. 1485280Sroot * Create a template for use in transmissions on this connection. 1495280Sroot * Enter SYN_SENT state, and mark socket as connecting. 1505280Sroot * Start keep-alive timer, and seed output sequence space. 1515280Sroot * Send initial segment on connection. 1525280Sroot */ 1534809Swnj case PRU_CONNECT: 1548272Sroot if (inp->inp_lport == 0) { 1558272Sroot error = in_pcbbind(inp, (struct mbuf *)0); 1568272Sroot if (error) 1578272Sroot break; 1588272Sroot } 1598272Sroot error = in_pcbconnect(inp, nam); 1604954Swnj if (error) 1614886Swnj break; 1625174Swnj tp->t_template = tcp_template(tp); 1635280Sroot if (tp->t_template == 0) { 1645280Sroot in_pcbdisconnect(inp); 1655280Sroot error = ENOBUFS; 1665280Sroot break; 1675280Sroot } 1684886Swnj soisconnecting(so); 16930527Skarels tcpstat.tcps_connattempt++; 1705075Swnj tp->t_state = TCPS_SYN_SENT; 17133747Skarels tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT; 1725245Sroot tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2; 1735245Sroot tcp_sendseqinit(tp); 1746506Ssam error = tcp_output(tp); 1754567Swnj break; 1764497Swnj 1775280Sroot /* 17813117Ssam * Create a TCP connection between two sockets. 17913117Ssam */ 18013117Ssam case PRU_CONNECT2: 18113117Ssam error = EOPNOTSUPP; 18213117Ssam break; 18313117Ssam 18413117Ssam /* 1855280Sroot * Initiate disconnect from peer. 1865280Sroot * If connection never passed embryonic stage, just drop; 1875280Sroot * else if don't need to let data drain, then can just drop anyways, 1885280Sroot * else have to begin TCP shutdown process: mark socket disconnecting, 1895280Sroot * drain unread data, state switch to reflect user close, and 1905280Sroot * send segment (e.g. FIN) to peer. Socket will be really disconnected 1915280Sroot * when peer sends FIN and acks ours. 1925280Sroot * 1935280Sroot * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB. 1945280Sroot */ 1955280Sroot case PRU_DISCONNECT: 19610397Ssam tp = tcp_disconnect(tp); 1975245Sroot break; 1985245Sroot 1995280Sroot /* 2005280Sroot * Accept a connection. Essentially all the work is 2015280Sroot * done at higher levels; just return the address 2025280Sroot * of the peer, storing through addr. 2035280Sroot */ 2046117Swnj case PRU_ACCEPT: { 2058272Sroot struct sockaddr_in *sin = mtod(nam, struct sockaddr_in *); 2066117Swnj 2078272Sroot nam->m_len = sizeof (struct sockaddr_in); 2088272Sroot sin->sin_family = AF_INET; 2098272Sroot sin->sin_port = inp->inp_fport; 2108272Sroot sin->sin_addr = inp->inp_faddr; 2118272Sroot break; 2126117Swnj } 2134925Swnj 2145280Sroot /* 2155280Sroot * Mark the connection as being incapable of further output. 2165280Sroot */ 2174809Swnj case PRU_SHUTDOWN: 2185089Swnj socantsendmore(so); 21910397Ssam tp = tcp_usrclosed(tp); 22010397Ssam if (tp) 22110397Ssam error = tcp_output(tp); 2224567Swnj break; 2234497Swnj 2245280Sroot /* 2255280Sroot * After a receive, possibly send window update to peer. 2265280Sroot */ 2274809Swnj case PRU_RCVD: 2285113Swnj (void) tcp_output(tp); 2294567Swnj break; 2304497Swnj 2315280Sroot /* 2325280Sroot * Do a send by putting data in output queue and updating urgent 2335280Sroot * marker if URG set. Possibly send more data. 2345280Sroot */ 2354809Swnj case PRU_SEND: 2365075Swnj sbappend(&so->so_snd, m); 2376506Ssam error = tcp_output(tp); 2384567Swnj break; 2394567Swnj 2405280Sroot /* 2415280Sroot * Abort the TCP. 2425280Sroot */ 2434809Swnj case PRU_ABORT: 24410397Ssam tp = tcp_drop(tp, ECONNABORTED); 2454567Swnj break; 2464567Swnj 2475113Swnj case PRU_SENSE: 24816989Skarels ((struct stat *) m)->st_blksize = so->so_snd.sb_hiwat; 24930871Smckusick (void) splx(s); 25016989Skarels return (0); 2515113Swnj 2525113Swnj case PRU_RCVOOB: 25324821Skarels if ((so->so_oobmark == 0 && 25424821Skarels (so->so_state & SS_RCVATMARK) == 0) || 25527195Skarels so->so_options & SO_OOBINLINE || 25624821Skarels tp->t_oobflags & TCPOOB_HADDATA) { 2575417Swnj error = EINVAL; 2585417Swnj break; 2595417Swnj } 2605549Swnj if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) { 2615442Swnj error = EWOULDBLOCK; 2625549Swnj break; 2635442Swnj } 2648310Sroot m->m_len = 1; 2655549Swnj *mtod(m, caddr_t) = tp->t_iobc; 26624821Skarels if (((int)nam & MSG_PEEK) == 0) 26724821Skarels tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA); 2685113Swnj break; 2695113Swnj 2705113Swnj case PRU_SENDOOB: 2715442Swnj if (sbspace(&so->so_snd) < -512) { 27211229Ssam m_freem(m); 2735442Swnj error = ENOBUFS; 2745442Swnj break; 2755442Swnj } 27627195Skarels /* 27727195Skarels * According to RFC961 (Assigned Protocols), 27827195Skarels * the urgent pointer points to the last octet 27927195Skarels * of urgent data. We continue, however, 28027195Skarels * to consider it to indicate the first octet 28127195Skarels * of data past the urgent section. 28227195Skarels * Otherwise, snd_up should be one lower. 28327195Skarels */ 2845417Swnj sbappend(&so->so_snd, m); 28527195Skarels tp->snd_up = tp->snd_una + so->so_snd.sb_cc; 2865549Swnj tp->t_force = 1; 2876506Ssam error = tcp_output(tp); 2885549Swnj tp->t_force = 0; 2895113Swnj break; 2905113Swnj 2916510Ssam case PRU_SOCKADDR: 2928272Sroot in_setsockaddr(inp, nam); 2936510Ssam break; 2946510Ssam 29514123Ssam case PRU_PEERADDR: 29614123Ssam in_setpeeraddr(inp, nam); 29714123Ssam break; 29814123Ssam 2995280Sroot /* 3005280Sroot * TCP slow timer went off; going through this 3015280Sroot * routine for tracing's sake. 3025280Sroot */ 3034809Swnj case PRU_SLOWTIMO: 30410397Ssam tp = tcp_timers(tp, (int)nam); 3058272Sroot req |= (int)nam << 8; /* for debug's sake */ 3064809Swnj break; 3074809Swnj 3084731Swnj default: 3094731Swnj panic("tcp_usrreq"); 3104567Swnj } 3115270Sroot if (tp && (so->so_options & SO_DEBUG)) 3125270Sroot tcp_trace(TA_USER, ostate, tp, (struct tcpiphdr *)0, req); 3134567Swnj splx(s); 3144886Swnj return (error); 3154497Swnj } 3165245Sroot 31725896Skarels tcp_ctloutput(op, so, level, optname, mp) 31824821Skarels int op; 31924821Skarels struct socket *so; 32024821Skarels int level, optname; 32125896Skarels struct mbuf **mp; 32224821Skarels { 32325896Skarels int error = 0; 32425896Skarels struct inpcb *inp = sotoinpcb(so); 32525896Skarels register struct tcpcb *tp = intotcpcb(inp); 32625896Skarels register struct mbuf *m; 32725896Skarels 32824821Skarels if (level != IPPROTO_TCP) 32926248Skarels return (ip_ctloutput(op, so, level, optname, mp)); 33025896Skarels 33125896Skarels switch (op) { 33225896Skarels 33325896Skarels case PRCO_SETOPT: 33425896Skarels m = *mp; 33525896Skarels switch (optname) { 33625896Skarels 33725896Skarels case TCP_NODELAY: 33825896Skarels if (m == NULL || m->m_len < sizeof (int)) 33925896Skarels error = EINVAL; 34025896Skarels else if (*mtod(m, int *)) 34125896Skarels tp->t_flags |= TF_NODELAY; 34225896Skarels else 34325896Skarels tp->t_flags &= ~TF_NODELAY; 34425896Skarels break; 34525896Skarels 34625896Skarels case TCP_MAXSEG: /* not yet */ 34725896Skarels default: 34825896Skarels error = EINVAL; 34925896Skarels break; 35025896Skarels } 35131041Ssam if (m) 35231041Ssam (void) m_free(m); 35325896Skarels break; 35425896Skarels 35525896Skarels case PRCO_GETOPT: 35625896Skarels *mp = m = m_get(M_WAIT, MT_SOOPTS); 35725896Skarels m->m_len = sizeof(int); 35825896Skarels 35925896Skarels switch (optname) { 36025896Skarels case TCP_NODELAY: 36125896Skarels *mtod(m, int *) = tp->t_flags & TF_NODELAY; 36225896Skarels break; 36325896Skarels case TCP_MAXSEG: 36425896Skarels *mtod(m, int *) = tp->t_maxseg; 36525896Skarels break; 36625896Skarels default: 36725896Skarels error = EINVAL; 36825896Skarels break; 36925896Skarels } 37025896Skarels break; 37125896Skarels } 37225896Skarels return (error); 37324821Skarels } 37424821Skarels 37534485Skarels u_long tcp_sendspace = 1024*4; 37634485Skarels u_long tcp_recvspace = 1024*4; 3775280Sroot /* 3785280Sroot * Attach TCP protocol to socket, allocating 3795280Sroot * internet protocol control block, tcp control block, 3805280Sroot * bufer space, and entering LISTEN state if to accept connections. 3815280Sroot */ 3828272Sroot tcp_attach(so) 3835280Sroot struct socket *so; 3845280Sroot { 3855280Sroot register struct tcpcb *tp; 3865280Sroot struct inpcb *inp; 3875280Sroot int error; 3885280Sroot 38934485Skarels if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 39034485Skarels error = soreserve(so, tcp_sendspace, tcp_recvspace); 39134485Skarels if (error) 39234485Skarels return (error); 39334485Skarels } 3947511Sroot error = in_pcballoc(so, &tcb); 3957511Sroot if (error) 39617047Skarels return (error); 3978272Sroot inp = sotoinpcb(so); 3985280Sroot tp = tcp_newtcpcb(inp); 3997511Sroot if (tp == 0) { 40017047Skarels int nofd = so->so_state & SS_NOFDREF; /* XXX */ 40117047Skarels 40217047Skarels so->so_state &= ~SS_NOFDREF; /* don't free the socket yet */ 40317047Skarels in_pcbdetach(inp); 40417047Skarels so->so_state |= nofd; 40517047Skarels return (ENOBUFS); 4067511Sroot } 4078272Sroot tp->t_state = TCPS_CLOSED; 4085280Sroot return (0); 4095280Sroot } 4105280Sroot 4115280Sroot /* 4125280Sroot * Initiate (or continue) disconnect. 4135280Sroot * If embryonic state, just send reset (once). 41413221Ssam * If in ``let data drain'' option and linger null, just drop. 4155280Sroot * Otherwise (hard), mark socket disconnecting and drop 4165280Sroot * current input data; switch states based on user close, and 4175280Sroot * send segment to peer (with FIN). 4185280Sroot */ 41910397Ssam struct tcpcb * 4205280Sroot tcp_disconnect(tp) 42110397Ssam register struct tcpcb *tp; 4225280Sroot { 4235280Sroot struct socket *so = tp->t_inpcb->inp_socket; 4245280Sroot 4255280Sroot if (tp->t_state < TCPS_ESTABLISHED) 42610397Ssam tp = tcp_close(tp); 42713221Ssam else if ((so->so_options & SO_LINGER) && so->so_linger == 0) 42810397Ssam tp = tcp_drop(tp, 0); 4295280Sroot else { 4305280Sroot soisdisconnecting(so); 4315280Sroot sbflush(&so->so_rcv); 43210397Ssam tp = tcp_usrclosed(tp); 43310397Ssam if (tp) 43410397Ssam (void) tcp_output(tp); 4355280Sroot } 43610397Ssam return (tp); 4375280Sroot } 4385280Sroot 4395280Sroot /* 4405280Sroot * User issued close, and wish to trail through shutdown states: 4415280Sroot * if never received SYN, just forget it. If got a SYN from peer, 4425280Sroot * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN. 4435280Sroot * If already got a FIN from peer, then almost done; go to LAST_ACK 4445280Sroot * state. In all other cases, have already sent FIN to peer (e.g. 4455280Sroot * after PRU_SHUTDOWN), and just have to play tedious game waiting 4465280Sroot * for peer to send FIN or not respond to keep-alives, etc. 4475897Swnj * We can let the user exit from the close as soon as the FIN is acked. 4485280Sroot */ 44910397Ssam struct tcpcb * 4505245Sroot tcp_usrclosed(tp) 45110397Ssam register struct tcpcb *tp; 4525245Sroot { 4535245Sroot 4545245Sroot switch (tp->t_state) { 4555245Sroot 45612438Ssam case TCPS_CLOSED: 4575245Sroot case TCPS_LISTEN: 4585245Sroot case TCPS_SYN_SENT: 4595245Sroot tp->t_state = TCPS_CLOSED; 46010397Ssam tp = tcp_close(tp); 4615245Sroot break; 4625245Sroot 4635245Sroot case TCPS_SYN_RECEIVED: 4645245Sroot case TCPS_ESTABLISHED: 4655245Sroot tp->t_state = TCPS_FIN_WAIT_1; 4665245Sroot break; 4675245Sroot 4685245Sroot case TCPS_CLOSE_WAIT: 4695245Sroot tp->t_state = TCPS_LAST_ACK; 4705245Sroot break; 4715245Sroot } 47210397Ssam if (tp && tp->t_state >= TCPS_FIN_WAIT_2) 4735897Swnj soisdisconnected(tp->t_inpcb->inp_socket); 47410397Ssam return (tp); 4755245Sroot } 476