1*6117Swnj /* tcp_usrreq.c 1.53 82/03/11 */ 24567Swnj 34497Swnj #include "../h/param.h" 44567Swnj #include "../h/systm.h" 54664Swnj #include "../h/mbuf.h" 64664Swnj #include "../h/socket.h" 74809Swnj #include "../h/socketvar.h" 84809Swnj #include "../h/protosw.h" 95089Swnj #include "../net/in.h" 105089Swnj #include "../net/in_pcb.h" 115089Swnj #include "../net/in_systm.h" 124954Swnj #include "../net/if.h" 134809Swnj #include "../net/ip.h" 144900Swnj #include "../net/ip_var.h" 154809Swnj #include "../net/tcp.h" 164809Swnj #include "../net/tcp_fsm.h" 175089Swnj #include "../net/tcp_seq.h" 185089Swnj #include "../net/tcp_timer.h" 194809Swnj #include "../net/tcp_var.h" 205089Swnj #include "../net/tcpip.h" 215270Sroot #include "../net/tcp_debug.h" 225113Swnj #include "../errno.h" 234497Swnj 245280Sroot /* 255280Sroot * TCP protocol interface to socket abstraction. 265280Sroot */ 275280Sroot extern char *tcpstates[]; 284954Swnj struct tcpcb *tcp_newtcpcb(); 295280Sroot 304734Swnj /* 315280Sroot * Process a TCP user request for TCP tb. If this is a send request 324731Swnj * then m is the mbuf chain of send data. If this is a timer expiration 334731Swnj * (called from the software clock routine), then timertype tells which timer. 344731Swnj */ 354809Swnj tcp_usrreq(so, req, m, addr) 364809Swnj struct socket *so; 374809Swnj int req; 384731Swnj struct mbuf *m; 394809Swnj caddr_t addr; 404497Swnj { 414886Swnj register struct inpcb *inp = sotoinpcb(so); 424911Swnj register struct tcpcb *tp; 434567Swnj int s = splnet(); 444809Swnj int error = 0; 455270Sroot int ostate; 464567Swnj COUNT(TCP_USRREQ); 474497Swnj 484886Swnj /* 495280Sroot * When a TCP is attached to a socket, then there will be 505280Sroot * a (struct inpcb) pointed at by the socket, and this 515280Sroot * structure will point at a subsidary (struct tcpcb). 525280Sroot * The normal sequence of events is: 535280Sroot * PRU_ATTACH creating these structures 545280Sroot * PRU_CONNECT connecting to a remote peer 555280Sroot * (PRU_SEND|PRU_RCVD)* exchanging data 565280Sroot * PRU_DISCONNECT disconnecting from remote peer 575280Sroot * PRU_DETACH deleting the structures 585280Sroot * With the operations from PRU_CONNECT through PRU_DISCONNECT 595280Sroot * possible repeated several times. 605280Sroot * 615280Sroot * MULTIPLE CONNECTS ARE NOT YET IMPLEMENTED. 624886Swnj */ 635089Swnj if (inp == 0 && req != PRU_ATTACH) { 645075Swnj splx(s); 655280Sroot return (EINVAL); /* XXX */ 665075Swnj } 675075Swnj if (inp) { 684911Swnj tp = intotcpcb(inp); 694731Swnj #ifdef KPROF 705075Swnj tcp_acounts[tp->t_state][req]++; 714731Swnj #endif 725270Sroot ostate = tp->t_state; 734911Swnj } 744809Swnj switch (req) { 754497Swnj 765280Sroot /* 775280Sroot * TCP attaches to socket via PRU_ATTACH, reserving space, 785280Sroot * and internet and TCP control blocks. 795280Sroot * If the socket is to receive connections, 805280Sroot * then the LISTEN state is entered. 815280Sroot */ 824809Swnj case PRU_ATTACH: 834954Swnj if (inp) { 844809Swnj error = EISCONN; 854911Swnj break; 864886Swnj } 875280Sroot error = tcp_attach(so, (struct sockaddr *)addr); 885075Swnj if (error) 894954Swnj break; 905392Swnj if ((so->so_options & SO_DONTLINGER) == 0) 915392Swnj so->so_linger = TCP_LINGERTIME; 925280Sroot tp = sototcpcb(so); 934567Swnj break; 944497Swnj 955280Sroot /* 965280Sroot * PRU_DETACH detaches the TCP protocol from the socket. 975280Sroot * If the protocol state is non-embryonic, then can't 985280Sroot * do this directly: have to initiate a PRU_DISCONNECT, 995280Sroot * which may finish later; embryonic TCB's can just 1005280Sroot * be discarded here. 1015280Sroot */ 1024809Swnj case PRU_DETACH: 1035280Sroot if (tp->t_state > TCPS_LISTEN) 1045280Sroot tcp_disconnect(tp); 1055280Sroot else { 1065280Sroot tcp_close(tp); 1075280Sroot tp = 0; 1085280Sroot } 1094809Swnj break; 1104809Swnj 1115280Sroot /* 1125280Sroot * Initiate connection to peer. 1135280Sroot * Create a template for use in transmissions on this connection. 1145280Sroot * Enter SYN_SENT state, and mark socket as connecting. 1155280Sroot * Start keep-alive timer, and seed output sequence space. 1165280Sroot * Send initial segment on connection. 1175280Sroot */ 1184809Swnj case PRU_CONNECT: 1195165Swnj error = in_pcbconnect(inp, (struct sockaddr_in *)addr); 1204954Swnj if (error) 1214886Swnj break; 1225174Swnj tp->t_template = tcp_template(tp); 1235280Sroot if (tp->t_template == 0) { 1245280Sroot in_pcbdisconnect(inp); 1255280Sroot error = ENOBUFS; 1265280Sroot break; 1275280Sroot } 1284886Swnj soisconnecting(so); 1295075Swnj tp->t_state = TCPS_SYN_SENT; 1305245Sroot tp->t_timer[TCPT_KEEP] = TCPTV_KEEP; 1315245Sroot tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2; 1325245Sroot tcp_sendseqinit(tp); 1335113Swnj (void) tcp_output(tp); 1344567Swnj break; 1354497Swnj 1365280Sroot /* 1375280Sroot * Initiate disconnect from peer. 1385280Sroot * If connection never passed embryonic stage, just drop; 1395280Sroot * else if don't need to let data drain, then can just drop anyways, 1405280Sroot * else have to begin TCP shutdown process: mark socket disconnecting, 1415280Sroot * drain unread data, state switch to reflect user close, and 1425280Sroot * send segment (e.g. FIN) to peer. Socket will be really disconnected 1435280Sroot * when peer sends FIN and acks ours. 1445280Sroot * 1455280Sroot * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB. 1465280Sroot */ 1475280Sroot case PRU_DISCONNECT: 1485280Sroot tcp_disconnect(tp); 1495245Sroot break; 1505245Sroot 1515280Sroot /* 1525280Sroot * Accept a connection. Essentially all the work is 1535280Sroot * done at higher levels; just return the address 1545280Sroot * of the peer, storing through addr. 1555280Sroot */ 156*6117Swnj case PRU_ACCEPT: { 157*6117Swnj struct sockaddr_in *sin = (struct sockaddr_in *)addr; 158*6117Swnj 159*6117Swnj if (sin) { 160*6117Swnj bzero((caddr_t)sin, sizeof (*sin)); 161*6117Swnj sin->sin_family = AF_INET; 162*6117Swnj sin->sin_port = inp->inp_fport; 163*6117Swnj sin->sin_addr = inp->inp_faddr; 164*6117Swnj } 165*6117Swnj } 1664954Swnj break; 1674925Swnj 1685280Sroot /* 1695280Sroot * Mark the connection as being incapable of further output. 1705280Sroot */ 1714809Swnj case PRU_SHUTDOWN: 1725089Swnj socantsendmore(so); 1735245Sroot tcp_usrclosed(tp); 1745245Sroot (void) tcp_output(tp); 1754567Swnj break; 1764497Swnj 1775280Sroot /* 1785280Sroot * After a receive, possibly send window update to peer. 1795280Sroot */ 1804809Swnj case PRU_RCVD: 1815113Swnj (void) tcp_output(tp); 1824567Swnj break; 1834497Swnj 1845280Sroot /* 1855280Sroot * Do a send by putting data in output queue and updating urgent 1865280Sroot * marker if URG set. Possibly send more data. 1875280Sroot */ 1884809Swnj case PRU_SEND: 1895075Swnj sbappend(&so->so_snd, m); 1905089Swnj /* 1915089Swnj if (tp->t_flags & TF_PUSH) 1925075Swnj tp->snd_end = tp->snd_una + so->so_snd.sb_cc; 1935089Swnj */ 1945113Swnj (void) tcp_output(tp); 1954567Swnj break; 1964567Swnj 1975280Sroot /* 1985280Sroot * Abort the TCP. 1995280Sroot */ 2004809Swnj case PRU_ABORT: 2015075Swnj tcp_drop(tp, ECONNABORTED); 2024567Swnj break; 2034567Swnj 2045280Sroot /* SOME AS YET UNIMPLEMENTED HOOKS */ 2054809Swnj case PRU_CONTROL: 2064886Swnj error = EOPNOTSUPP; 2074809Swnj break; 2084809Swnj 2095113Swnj case PRU_SENSE: 2105113Swnj error = EOPNOTSUPP; 2115113Swnj break; 2125417Swnj /* END UNIMPLEMENTED HOOKS */ 2135113Swnj 2145113Swnj case PRU_RCVOOB: 2155442Swnj if (so->so_oobmark == 0 && 2165442Swnj (so->so_state & SS_RCVATMARK) == 0) { 2175417Swnj error = EINVAL; 2185417Swnj break; 2195417Swnj } 2205549Swnj if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) { 2215442Swnj error = EWOULDBLOCK; 2225549Swnj break; 2235442Swnj } 2245549Swnj *mtod(m, caddr_t) = tp->t_iobc; 2255113Swnj break; 2265113Swnj 2275113Swnj case PRU_SENDOOB: 2285549Swnj #ifdef TCPTRUEOOB 2295549Swnj if (tp->t_flags & TF_DOOOB) { 2305549Swnj tp->t_oobseq++; 2315549Swnj tp->t_oobc = *mtod(m, caddr_t); 2325549Swnj tp->t_oobmark = tp->snd_una + so->so_snd.sb_cc; 2335549Swnj printf("sendoob seq now %x oobc %x\n", tp->t_oobseq, tp->t_oobc); 2345549Swnj tp->t_oobflags |= TCPOOB_NEEDACK; 2355549Swnj (void) tcp_output(tp); 2365549Swnj } 2375549Swnj #endif 2385442Swnj if (sbspace(&so->so_snd) < -512) { 2395442Swnj error = ENOBUFS; 2405442Swnj break; 2415442Swnj } 2425417Swnj tp->snd_up = tp->snd_una + so->so_snd.sb_cc + 1; 2435417Swnj sbappend(&so->so_snd, m); 2445417Swnj /* 2455417Swnj if (tp->t_flags & TF_PUSH) 2465417Swnj tp->snd_end = tp->snd_una + so->so_snd.sb_cc; 2475417Swnj */ 2485549Swnj tp->t_force = 1; 2495549Swnj (void) tcp_output(tp); 2505549Swnj tp->t_force = 0; 2515113Swnj break; 2525113Swnj 2535280Sroot /* 2545280Sroot * TCP slow timer went off; going through this 2555280Sroot * routine for tracing's sake. 2565280Sroot */ 2574809Swnj case PRU_SLOWTIMO: 2585075Swnj tcp_timers(tp, (int)addr); 2595270Sroot req |= (int)addr << 8; /* for debug's sake */ 2604809Swnj break; 2614809Swnj 2624731Swnj default: 2634731Swnj panic("tcp_usrreq"); 2644567Swnj } 2655270Sroot if (tp && (so->so_options & SO_DEBUG)) 2665270Sroot tcp_trace(TA_USER, ostate, tp, (struct tcpiphdr *)0, req); 2674567Swnj splx(s); 2684886Swnj return (error); 2694497Swnj } 2705245Sroot 2715953Swnj int tcp_sendspace = 1024*2; 2725953Swnj int tcp_recvspace = 1024*3; 2735280Sroot /* 2745280Sroot * Attach TCP protocol to socket, allocating 2755280Sroot * internet protocol control block, tcp control block, 2765280Sroot * bufer space, and entering LISTEN state if to accept connections. 2775280Sroot */ 2785280Sroot tcp_attach(so, sa) 2795280Sroot struct socket *so; 2805280Sroot struct sockaddr *sa; 2815280Sroot { 2825280Sroot register struct tcpcb *tp; 2835280Sroot struct inpcb *inp; 2845280Sroot int error; 2855280Sroot 2865953Swnj error = in_pcbattach(so, &tcb, 2875953Swnj tcp_sendspace, tcp_recvspace, (struct sockaddr_in *)sa); 2885280Sroot if (error) 2895280Sroot return (error); 2905280Sroot inp = (struct inpcb *)so->so_pcb; 2915280Sroot tp = tcp_newtcpcb(inp); 2925280Sroot if (so->so_options & SO_ACCEPTCONN) { 2935280Sroot if (tp == 0) { 2945280Sroot in_pcbdetach(inp); 2955280Sroot return (ENOBUFS); 2965280Sroot } 2975280Sroot tp->t_state = TCPS_LISTEN; 2985280Sroot } else 2995280Sroot tp->t_state = TCPS_CLOSED; 3005280Sroot return (0); 3015280Sroot } 3025280Sroot 3035280Sroot /* 3045280Sroot * Initiate (or continue) disconnect. 3055280Sroot * If embryonic state, just send reset (once). 3065280Sroot * If not in ``let data drain'' option, just drop. 3075280Sroot * Otherwise (hard), mark socket disconnecting and drop 3085280Sroot * current input data; switch states based on user close, and 3095280Sroot * send segment to peer (with FIN). 3105280Sroot */ 3115280Sroot tcp_disconnect(tp) 3125280Sroot struct tcpcb *tp; 3135280Sroot { 3145280Sroot struct socket *so = tp->t_inpcb->inp_socket; 3155280Sroot 3165280Sroot if (tp->t_state < TCPS_ESTABLISHED) 3175280Sroot tcp_close(tp); 3185392Swnj else if (so->so_linger == 0) 3195280Sroot tcp_drop(tp, 0); 3205280Sroot else { 3215280Sroot soisdisconnecting(so); 3225280Sroot sbflush(&so->so_rcv); 3235280Sroot tcp_usrclosed(tp); 3245280Sroot (void) tcp_output(tp); 3255280Sroot } 3265280Sroot } 3275280Sroot 3285280Sroot /* 3295280Sroot * User issued close, and wish to trail through shutdown states: 3305280Sroot * if never received SYN, just forget it. If got a SYN from peer, 3315280Sroot * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN. 3325280Sroot * If already got a FIN from peer, then almost done; go to LAST_ACK 3335280Sroot * state. In all other cases, have already sent FIN to peer (e.g. 3345280Sroot * after PRU_SHUTDOWN), and just have to play tedious game waiting 3355280Sroot * for peer to send FIN or not respond to keep-alives, etc. 3365897Swnj * We can let the user exit from the close as soon as the FIN is acked. 3375280Sroot */ 3385245Sroot tcp_usrclosed(tp) 3395245Sroot struct tcpcb *tp; 3405245Sroot { 3415245Sroot 3425245Sroot switch (tp->t_state) { 3435245Sroot 3445245Sroot case TCPS_LISTEN: 3455245Sroot case TCPS_SYN_SENT: 3465245Sroot tp->t_state = TCPS_CLOSED; 3475245Sroot tcp_close(tp); 3485245Sroot break; 3495245Sroot 3505245Sroot case TCPS_SYN_RECEIVED: 3515245Sroot case TCPS_ESTABLISHED: 3525245Sroot tp->t_state = TCPS_FIN_WAIT_1; 3535245Sroot break; 3545245Sroot 3555245Sroot case TCPS_CLOSE_WAIT: 3565245Sroot tp->t_state = TCPS_LAST_ACK; 3575245Sroot break; 3585245Sroot } 3595897Swnj if (tp->t_state >= TCPS_FIN_WAIT_2) 3605897Swnj soisdisconnected(tp->t_inpcb->inp_socket); 3615245Sroot } 362