123196Smckusick /* 237323Skarels * Copyright (c) 1982, 1986, 1988 Regents of the University of California. 332789Sbostic * All rights reserved. 423196Smckusick * 532789Sbostic * Redistribution and use in source and binary forms are permitted 634855Sbostic * provided that the above copyright notice and this paragraph are 734855Sbostic * duplicated in all such forms and that any documentation, 834855Sbostic * advertising materials, and other materials related to such 934855Sbostic * distribution and use acknowledge that the software was developed 1034855Sbostic * by the University of California, Berkeley. The name of the 1134855Sbostic * University may not be used to endorse or promote products derived 1234855Sbostic * from this software without specific prior written permission. 1334855Sbostic * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 1434855Sbostic * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 1534855Sbostic * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 1632789Sbostic * 17*40690Skarels * @(#)tcp_usrreq.c 7.13 (Berkeley) 04/03/90 1823196Smckusick */ 194567Swnj 2017064Sbloom #include "param.h" 2117064Sbloom #include "systm.h" 2237323Skarels #include "malloc.h" 2317064Sbloom #include "mbuf.h" 2417064Sbloom #include "socket.h" 2517064Sbloom #include "socketvar.h" 2617064Sbloom #include "protosw.h" 2717064Sbloom #include "errno.h" 2817064Sbloom #include "stat.h" 298697Sroot 308697Sroot #include "../net/if.h" 318697Sroot #include "../net/route.h" 3210896Ssam 3317064Sbloom #include "in.h" 3417064Sbloom #include "in_systm.h" 3517064Sbloom #include "ip.h" 36*40690Skarels #include "in_pcb.h" 3717064Sbloom #include "ip_var.h" 3817064Sbloom #include "tcp.h" 3917064Sbloom #include "tcp_fsm.h" 4017064Sbloom #include "tcp_seq.h" 4117064Sbloom #include "tcp_timer.h" 4217064Sbloom #include "tcp_var.h" 4317064Sbloom #include "tcpip.h" 4417064Sbloom #include "tcp_debug.h" 454497Swnj 465280Sroot /* 475280Sroot * TCP protocol interface to socket abstraction. 485280Sroot */ 495280Sroot extern char *tcpstates[]; 504954Swnj struct tcpcb *tcp_newtcpcb(); 515280Sroot 524734Swnj /* 535280Sroot * Process a TCP user request for TCP tb. If this is a send request 544731Swnj * then m is the mbuf chain of send data. If this is a timer expiration 554731Swnj * (called from the software clock routine), then timertype tells which timer. 564731Swnj */ 578601Sroot /*ARGSUSED*/ 5812766Ssam tcp_usrreq(so, req, m, nam, rights) 594809Swnj struct socket *so; 604809Swnj int req; 6112766Ssam struct mbuf *m, *nam, *rights; 624497Swnj { 6330909Skarels register struct inpcb *inp; 644911Swnj register struct tcpcb *tp; 6530909Skarels int s; 664809Swnj int error = 0; 675270Sroot int ostate; 684497Swnj 6930909Skarels if (req == PRU_CONTROL) 7030909Skarels return (in_control(so, (int)m, (caddr_t)nam, 7130909Skarels (struct ifnet *)rights)); 7230909Skarels if (rights && rights->m_len) 7312766Ssam return (EINVAL); 7430909Skarels 7530909Skarels s = splnet(); 7630909Skarels inp = sotoinpcb(so); 774886Swnj /* 785280Sroot * When a TCP is attached to a socket, then there will be 795280Sroot * a (struct inpcb) pointed at by the socket, and this 805280Sroot * structure will point at a subsidary (struct tcpcb). 814886Swnj */ 825089Swnj if (inp == 0 && req != PRU_ATTACH) { 835075Swnj splx(s); 845280Sroot return (EINVAL); /* XXX */ 855075Swnj } 865075Swnj if (inp) { 874911Swnj tp = intotcpcb(inp); 888272Sroot /* WHAT IF TP IS 0? */ 894731Swnj #ifdef KPROF 905075Swnj tcp_acounts[tp->t_state][req]++; 914731Swnj #endif 925270Sroot ostate = tp->t_state; 937511Sroot } else 947511Sroot ostate = 0; 954809Swnj switch (req) { 964497Swnj 975280Sroot /* 985280Sroot * TCP attaches to socket via PRU_ATTACH, reserving space, 998272Sroot * and an internet control block. 1005280Sroot */ 1014809Swnj case PRU_ATTACH: 1024954Swnj if (inp) { 1034809Swnj error = EISCONN; 1044911Swnj break; 1054886Swnj } 1068640Sroot error = tcp_attach(so); 1075075Swnj if (error) 1084954Swnj break; 10910397Ssam if ((so->so_options & SO_LINGER) && so->so_linger == 0) 1105392Swnj so->so_linger = TCP_LINGERTIME; 1115280Sroot tp = sototcpcb(so); 1124567Swnj break; 1134497Swnj 1145280Sroot /* 1155280Sroot * PRU_DETACH detaches the TCP protocol from the socket. 1165280Sroot * If the protocol state is non-embryonic, then can't 1175280Sroot * do this directly: have to initiate a PRU_DISCONNECT, 1185280Sroot * which may finish later; embryonic TCB's can just 1195280Sroot * be discarded here. 1205280Sroot */ 1214809Swnj case PRU_DETACH: 1225280Sroot if (tp->t_state > TCPS_LISTEN) 12310397Ssam tp = tcp_disconnect(tp); 12410397Ssam else 12510397Ssam tp = tcp_close(tp); 1264809Swnj break; 1274809Swnj 1285280Sroot /* 1298272Sroot * Give the socket an address. 1308272Sroot */ 1318272Sroot case PRU_BIND: 1328272Sroot error = in_pcbbind(inp, nam); 1338272Sroot if (error) 1348272Sroot break; 1358272Sroot break; 1368272Sroot 1378272Sroot /* 1388272Sroot * Prepare to accept connections. 1398272Sroot */ 1408272Sroot case PRU_LISTEN: 1418272Sroot if (inp->inp_lport == 0) 1428272Sroot error = in_pcbbind(inp, (struct mbuf *)0); 1438272Sroot if (error == 0) 1448272Sroot tp->t_state = TCPS_LISTEN; 1458272Sroot break; 1468272Sroot 1478272Sroot /* 1485280Sroot * Initiate connection to peer. 1495280Sroot * Create a template for use in transmissions on this connection. 1505280Sroot * Enter SYN_SENT state, and mark socket as connecting. 1515280Sroot * Start keep-alive timer, and seed output sequence space. 1525280Sroot * Send initial segment on connection. 1535280Sroot */ 1544809Swnj case PRU_CONNECT: 1558272Sroot if (inp->inp_lport == 0) { 1568272Sroot error = in_pcbbind(inp, (struct mbuf *)0); 1578272Sroot if (error) 1588272Sroot break; 1598272Sroot } 1608272Sroot error = in_pcbconnect(inp, nam); 1614954Swnj if (error) 1624886Swnj break; 1635174Swnj tp->t_template = tcp_template(tp); 1645280Sroot if (tp->t_template == 0) { 1655280Sroot in_pcbdisconnect(inp); 1665280Sroot error = ENOBUFS; 1675280Sroot break; 1685280Sroot } 1694886Swnj soisconnecting(so); 17030527Skarels tcpstat.tcps_connattempt++; 1715075Swnj tp->t_state = TCPS_SYN_SENT; 17233747Skarels tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT; 1735245Sroot tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2; 1745245Sroot tcp_sendseqinit(tp); 1756506Ssam error = tcp_output(tp); 1764567Swnj break; 1774497Swnj 1785280Sroot /* 17913117Ssam * Create a TCP connection between two sockets. 18013117Ssam */ 18113117Ssam case PRU_CONNECT2: 18213117Ssam error = EOPNOTSUPP; 18313117Ssam break; 18413117Ssam 18513117Ssam /* 1865280Sroot * Initiate disconnect from peer. 1875280Sroot * If connection never passed embryonic stage, just drop; 1885280Sroot * else if don't need to let data drain, then can just drop anyways, 1895280Sroot * else have to begin TCP shutdown process: mark socket disconnecting, 1905280Sroot * drain unread data, state switch to reflect user close, and 1915280Sroot * send segment (e.g. FIN) to peer. Socket will be really disconnected 1925280Sroot * when peer sends FIN and acks ours. 1935280Sroot * 1945280Sroot * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB. 1955280Sroot */ 1965280Sroot case PRU_DISCONNECT: 19710397Ssam tp = tcp_disconnect(tp); 1985245Sroot break; 1995245Sroot 2005280Sroot /* 2015280Sroot * Accept a connection. Essentially all the work is 2025280Sroot * done at higher levels; just return the address 2035280Sroot * of the peer, storing through addr. 2045280Sroot */ 2056117Swnj case PRU_ACCEPT: { 2068272Sroot struct sockaddr_in *sin = mtod(nam, struct sockaddr_in *); 2076117Swnj 2088272Sroot nam->m_len = sizeof (struct sockaddr_in); 2098272Sroot sin->sin_family = AF_INET; 21037323Skarels sin->sin_len = sizeof(*sin); 2118272Sroot sin->sin_port = inp->inp_fport; 2128272Sroot sin->sin_addr = inp->inp_faddr; 2138272Sroot break; 2146117Swnj } 2154925Swnj 2165280Sroot /* 2175280Sroot * Mark the connection as being incapable of further output. 2185280Sroot */ 2194809Swnj case PRU_SHUTDOWN: 2205089Swnj socantsendmore(so); 22110397Ssam tp = tcp_usrclosed(tp); 22210397Ssam if (tp) 22310397Ssam error = tcp_output(tp); 2244567Swnj break; 2254497Swnj 2265280Sroot /* 2275280Sroot * After a receive, possibly send window update to peer. 2285280Sroot */ 2294809Swnj case PRU_RCVD: 2305113Swnj (void) tcp_output(tp); 2314567Swnj break; 2324497Swnj 2335280Sroot /* 2345280Sroot * Do a send by putting data in output queue and updating urgent 2355280Sroot * marker if URG set. Possibly send more data. 2365280Sroot */ 2374809Swnj case PRU_SEND: 2385075Swnj sbappend(&so->so_snd, m); 2396506Ssam error = tcp_output(tp); 2404567Swnj break; 2414567Swnj 2425280Sroot /* 2435280Sroot * Abort the TCP. 2445280Sroot */ 2454809Swnj case PRU_ABORT: 24610397Ssam tp = tcp_drop(tp, ECONNABORTED); 2474567Swnj break; 2484567Swnj 2495113Swnj case PRU_SENSE: 25016989Skarels ((struct stat *) m)->st_blksize = so->so_snd.sb_hiwat; 25130871Smckusick (void) splx(s); 25216989Skarels return (0); 2535113Swnj 2545113Swnj case PRU_RCVOOB: 25524821Skarels if ((so->so_oobmark == 0 && 25624821Skarels (so->so_state & SS_RCVATMARK) == 0) || 25727195Skarels so->so_options & SO_OOBINLINE || 25824821Skarels tp->t_oobflags & TCPOOB_HADDATA) { 2595417Swnj error = EINVAL; 2605417Swnj break; 2615417Swnj } 2625549Swnj if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) { 2635442Swnj error = EWOULDBLOCK; 2645549Swnj break; 2655442Swnj } 2668310Sroot m->m_len = 1; 2675549Swnj *mtod(m, caddr_t) = tp->t_iobc; 26824821Skarels if (((int)nam & MSG_PEEK) == 0) 26924821Skarels tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA); 2705113Swnj break; 2715113Swnj 2725113Swnj case PRU_SENDOOB: 2735442Swnj if (sbspace(&so->so_snd) < -512) { 27411229Ssam m_freem(m); 2755442Swnj error = ENOBUFS; 2765442Swnj break; 2775442Swnj } 27827195Skarels /* 27927195Skarels * According to RFC961 (Assigned Protocols), 28027195Skarels * the urgent pointer points to the last octet 28127195Skarels * of urgent data. We continue, however, 28227195Skarels * to consider it to indicate the first octet 28327195Skarels * of data past the urgent section. 28427195Skarels * Otherwise, snd_up should be one lower. 28527195Skarels */ 2865417Swnj sbappend(&so->so_snd, m); 28727195Skarels tp->snd_up = tp->snd_una + so->so_snd.sb_cc; 2885549Swnj tp->t_force = 1; 2896506Ssam error = tcp_output(tp); 2905549Swnj tp->t_force = 0; 2915113Swnj break; 2925113Swnj 2936510Ssam case PRU_SOCKADDR: 2948272Sroot in_setsockaddr(inp, nam); 2956510Ssam break; 2966510Ssam 29714123Ssam case PRU_PEERADDR: 29814123Ssam in_setpeeraddr(inp, nam); 29914123Ssam break; 30014123Ssam 3015280Sroot /* 3025280Sroot * TCP slow timer went off; going through this 3035280Sroot * routine for tracing's sake. 3045280Sroot */ 3054809Swnj case PRU_SLOWTIMO: 30610397Ssam tp = tcp_timers(tp, (int)nam); 3078272Sroot req |= (int)nam << 8; /* for debug's sake */ 3084809Swnj break; 3094809Swnj 3104731Swnj default: 3114731Swnj panic("tcp_usrreq"); 3124567Swnj } 3135270Sroot if (tp && (so->so_options & SO_DEBUG)) 3145270Sroot tcp_trace(TA_USER, ostate, tp, (struct tcpiphdr *)0, req); 3154567Swnj splx(s); 3164886Swnj return (error); 3174497Swnj } 3185245Sroot 31925896Skarels tcp_ctloutput(op, so, level, optname, mp) 32024821Skarels int op; 32124821Skarels struct socket *so; 32224821Skarels int level, optname; 32325896Skarels struct mbuf **mp; 32424821Skarels { 32525896Skarels int error = 0; 32625896Skarels struct inpcb *inp = sotoinpcb(so); 32725896Skarels register struct tcpcb *tp = intotcpcb(inp); 32825896Skarels register struct mbuf *m; 32925896Skarels 33024821Skarels if (level != IPPROTO_TCP) 33126248Skarels return (ip_ctloutput(op, so, level, optname, mp)); 33225896Skarels 33325896Skarels switch (op) { 33425896Skarels 33525896Skarels case PRCO_SETOPT: 33625896Skarels m = *mp; 33725896Skarels switch (optname) { 33825896Skarels 33925896Skarels case TCP_NODELAY: 34025896Skarels if (m == NULL || m->m_len < sizeof (int)) 34125896Skarels error = EINVAL; 34225896Skarels else if (*mtod(m, int *)) 34325896Skarels tp->t_flags |= TF_NODELAY; 34425896Skarels else 34525896Skarels tp->t_flags &= ~TF_NODELAY; 34625896Skarels break; 34725896Skarels 34825896Skarels case TCP_MAXSEG: /* not yet */ 34925896Skarels default: 35025896Skarels error = EINVAL; 35125896Skarels break; 35225896Skarels } 35331041Ssam if (m) 35431041Ssam (void) m_free(m); 35525896Skarels break; 35625896Skarels 35725896Skarels case PRCO_GETOPT: 35825896Skarels *mp = m = m_get(M_WAIT, MT_SOOPTS); 35925896Skarels m->m_len = sizeof(int); 36025896Skarels 36125896Skarels switch (optname) { 36225896Skarels case TCP_NODELAY: 36325896Skarels *mtod(m, int *) = tp->t_flags & TF_NODELAY; 36425896Skarels break; 36525896Skarels case TCP_MAXSEG: 36625896Skarels *mtod(m, int *) = tp->t_maxseg; 36725896Skarels break; 36825896Skarels default: 36925896Skarels error = EINVAL; 37025896Skarels break; 37125896Skarels } 37225896Skarels break; 37325896Skarels } 37425896Skarels return (error); 37524821Skarels } 37624821Skarels 37734485Skarels u_long tcp_sendspace = 1024*4; 37834485Skarels u_long tcp_recvspace = 1024*4; 37937323Skarels 3805280Sroot /* 3815280Sroot * Attach TCP protocol to socket, allocating 3825280Sroot * internet protocol control block, tcp control block, 3835280Sroot * bufer space, and entering LISTEN state if to accept connections. 3845280Sroot */ 3858272Sroot tcp_attach(so) 3865280Sroot struct socket *so; 3875280Sroot { 3885280Sroot register struct tcpcb *tp; 3895280Sroot struct inpcb *inp; 3905280Sroot int error; 3915280Sroot 39234485Skarels if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 39334485Skarels error = soreserve(so, tcp_sendspace, tcp_recvspace); 39434485Skarels if (error) 39534485Skarels return (error); 39634485Skarels } 3977511Sroot error = in_pcballoc(so, &tcb); 3987511Sroot if (error) 39917047Skarels return (error); 4008272Sroot inp = sotoinpcb(so); 4015280Sroot tp = tcp_newtcpcb(inp); 4027511Sroot if (tp == 0) { 40317047Skarels int nofd = so->so_state & SS_NOFDREF; /* XXX */ 40417047Skarels 40517047Skarels so->so_state &= ~SS_NOFDREF; /* don't free the socket yet */ 40617047Skarels in_pcbdetach(inp); 40717047Skarels so->so_state |= nofd; 40817047Skarels return (ENOBUFS); 4097511Sroot } 4108272Sroot tp->t_state = TCPS_CLOSED; 4115280Sroot return (0); 4125280Sroot } 4135280Sroot 4145280Sroot /* 4155280Sroot * Initiate (or continue) disconnect. 4165280Sroot * If embryonic state, just send reset (once). 41713221Ssam * If in ``let data drain'' option and linger null, just drop. 4185280Sroot * Otherwise (hard), mark socket disconnecting and drop 4195280Sroot * current input data; switch states based on user close, and 4205280Sroot * send segment to peer (with FIN). 4215280Sroot */ 42210397Ssam struct tcpcb * 4235280Sroot tcp_disconnect(tp) 42410397Ssam register struct tcpcb *tp; 4255280Sroot { 4265280Sroot struct socket *so = tp->t_inpcb->inp_socket; 4275280Sroot 4285280Sroot if (tp->t_state < TCPS_ESTABLISHED) 42910397Ssam tp = tcp_close(tp); 43013221Ssam else if ((so->so_options & SO_LINGER) && so->so_linger == 0) 43110397Ssam tp = tcp_drop(tp, 0); 4325280Sroot else { 4335280Sroot soisdisconnecting(so); 4345280Sroot sbflush(&so->so_rcv); 43510397Ssam tp = tcp_usrclosed(tp); 43610397Ssam if (tp) 43710397Ssam (void) tcp_output(tp); 4385280Sroot } 43910397Ssam return (tp); 4405280Sroot } 4415280Sroot 4425280Sroot /* 4435280Sroot * User issued close, and wish to trail through shutdown states: 4445280Sroot * if never received SYN, just forget it. If got a SYN from peer, 4455280Sroot * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN. 4465280Sroot * If already got a FIN from peer, then almost done; go to LAST_ACK 4475280Sroot * state. In all other cases, have already sent FIN to peer (e.g. 4485280Sroot * after PRU_SHUTDOWN), and just have to play tedious game waiting 4495280Sroot * for peer to send FIN or not respond to keep-alives, etc. 4505897Swnj * We can let the user exit from the close as soon as the FIN is acked. 4515280Sroot */ 45210397Ssam struct tcpcb * 4535245Sroot tcp_usrclosed(tp) 45410397Ssam register struct tcpcb *tp; 4555245Sroot { 4565245Sroot 4575245Sroot switch (tp->t_state) { 4585245Sroot 45912438Ssam case TCPS_CLOSED: 4605245Sroot case TCPS_LISTEN: 4615245Sroot case TCPS_SYN_SENT: 4625245Sroot tp->t_state = TCPS_CLOSED; 46310397Ssam tp = tcp_close(tp); 4645245Sroot break; 4655245Sroot 4665245Sroot case TCPS_SYN_RECEIVED: 4675245Sroot case TCPS_ESTABLISHED: 4685245Sroot tp->t_state = TCPS_FIN_WAIT_1; 4695245Sroot break; 4705245Sroot 4715245Sroot case TCPS_CLOSE_WAIT: 4725245Sroot tp->t_state = TCPS_LAST_ACK; 4735245Sroot break; 4745245Sroot } 47510397Ssam if (tp && tp->t_state >= TCPS_FIN_WAIT_2) 4765897Swnj soisdisconnected(tp->t_inpcb->inp_socket); 47710397Ssam return (tp); 4785245Sroot } 479