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 632789Sbostic * provided that this notice is preserved and that due credit is given 732789Sbostic * to the University of California at Berkeley. The name of the University 832789Sbostic * may not be used to endorse or promote products derived from this 932789Sbostic * software without specific prior written permission. This software 1032789Sbostic * is provided ``as is'' without express or implied warranty. 1132789Sbostic * 12*33747Skarels * @(#)tcp_usrreq.c 7.8 (Berkeley) 03/16/88 1323196Smckusick */ 144567Swnj 1517064Sbloom #include "param.h" 1617064Sbloom #include "systm.h" 1717064Sbloom #include "mbuf.h" 1817064Sbloom #include "socket.h" 1917064Sbloom #include "socketvar.h" 2017064Sbloom #include "protosw.h" 2117064Sbloom #include "errno.h" 2217064Sbloom #include "stat.h" 238697Sroot 248697Sroot #include "../net/if.h" 258697Sroot #include "../net/route.h" 2610896Ssam 2717064Sbloom #include "in.h" 2817064Sbloom #include "in_pcb.h" 2917064Sbloom #include "in_systm.h" 3017064Sbloom #include "ip.h" 3117064Sbloom #include "ip_var.h" 3217064Sbloom #include "tcp.h" 3317064Sbloom #include "tcp_fsm.h" 3417064Sbloom #include "tcp_seq.h" 3517064Sbloom #include "tcp_timer.h" 3617064Sbloom #include "tcp_var.h" 3717064Sbloom #include "tcpip.h" 3817064Sbloom #include "tcp_debug.h" 394497Swnj 405280Sroot /* 415280Sroot * TCP protocol interface to socket abstraction. 425280Sroot */ 435280Sroot extern char *tcpstates[]; 444954Swnj struct tcpcb *tcp_newtcpcb(); 455280Sroot 464734Swnj /* 475280Sroot * Process a TCP user request for TCP tb. If this is a send request 484731Swnj * then m is the mbuf chain of send data. If this is a timer expiration 494731Swnj * (called from the software clock routine), then timertype tells which timer. 504731Swnj */ 518601Sroot /*ARGSUSED*/ 5212766Ssam tcp_usrreq(so, req, m, nam, rights) 534809Swnj struct socket *so; 544809Swnj int req; 5512766Ssam struct mbuf *m, *nam, *rights; 564497Swnj { 5730909Skarels register struct inpcb *inp; 584911Swnj register struct tcpcb *tp; 5930909Skarels int s; 604809Swnj int error = 0; 615270Sroot int ostate; 624497Swnj 6330909Skarels if (req == PRU_CONTROL) 6430909Skarels return (in_control(so, (int)m, (caddr_t)nam, 6530909Skarels (struct ifnet *)rights)); 6630909Skarels if (rights && rights->m_len) 6712766Ssam return (EINVAL); 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; 166*33747Skarels 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; 2048272Sroot sin->sin_port = inp->inp_fport; 2058272Sroot sin->sin_addr = inp->inp_faddr; 2068272Sroot break; 2076117Swnj } 2084925Swnj 2095280Sroot /* 2105280Sroot * Mark the connection as being incapable of further output. 2115280Sroot */ 2124809Swnj case PRU_SHUTDOWN: 2135089Swnj socantsendmore(so); 21410397Ssam tp = tcp_usrclosed(tp); 21510397Ssam if (tp) 21610397Ssam error = tcp_output(tp); 2174567Swnj break; 2184497Swnj 2195280Sroot /* 2205280Sroot * After a receive, possibly send window update to peer. 2215280Sroot */ 2224809Swnj case PRU_RCVD: 2235113Swnj (void) tcp_output(tp); 2244567Swnj break; 2254497Swnj 2265280Sroot /* 2275280Sroot * Do a send by putting data in output queue and updating urgent 2285280Sroot * marker if URG set. Possibly send more data. 2295280Sroot */ 2304809Swnj case PRU_SEND: 2315075Swnj sbappend(&so->so_snd, m); 2326506Ssam error = tcp_output(tp); 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 } 34631041Ssam if (m) 34731041Ssam (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