1*5062Swnj /* tcp_usrreq.c 1.33 81/11/24 */ 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" 94809Swnj #include "../net/inet.h" 104886Swnj #include "../net/inet_pcb.h" 114809Swnj #include "../net/inet_systm.h" 124954Swnj #include "../net/if.h" 134809Swnj #include "../net/imp.h" 144809Swnj #include "../net/ip.h" 154900Swnj #include "../net/ip_var.h" 164809Swnj #include "../net/tcp.h" 174567Swnj #define TCPFSTAB 184584Swnj #ifdef TCPDEBUG 194584Swnj #define TCPSTATES 204584Swnj #endif 214809Swnj #include "../net/tcp_fsm.h" 224809Swnj #include "../net/tcp_var.h" 234809Swnj #include "/usr/include/errno.h" 244497Swnj 254734Swnj /* 264809Swnj * Tcp initialization 274809Swnj */ 284809Swnj tcp_init() 294809Swnj { 304809Swnj 314809Swnj tcp_iss = 1; /* wrong */ 324886Swnj tcb.inp_next = tcb.inp_prev = &tcb; 334809Swnj } 344809Swnj 354809Swnj /* 364734Swnj * Tcp finite state machine entries for timer and user generated 374734Swnj * requests. These routines raise the ipl to that of the network 384734Swnj * to prevent reentry. In particluar, this requires that the software 394734Swnj * clock interrupt have lower priority than the network so that 404734Swnj * we can enter the network from timeout routines without improperly 414734Swnj * nesting the interrupt stack. 424734Swnj */ 434734Swnj 444734Swnj /* 454809Swnj * Tcp protocol timeout routine called every 500 ms. 464734Swnj * Updates the timers in all active tcb's and 474734Swnj * causes finite state machine actions if timers expire. 484734Swnj */ 494809Swnj tcp_slowtimo() 504497Swnj { 514886Swnj register struct inpcb *ip; 524886Swnj register struct tcpcb *tp; 534567Swnj int s = splnet(); 544809Swnj register short *tmp; 554731Swnj register int i; 564567Swnj COUNT(TCP_TIMEO); 574497Swnj 584567Swnj /* 594567Swnj * Search through tcb's and update active timers. 604567Swnj */ 614886Swnj for (ip = tcb.inp_next; ip != &tcb; ip = ip->inp_next) { 624886Swnj tp = intotcpcb(ip); 634731Swnj tmp = &tp->t_init; 644735Swnj for (i = 0; i < TNTIMERS; i++) { 654731Swnj if (*tmp && --*tmp == 0) 664911Swnj (void) tcp_usrreq(tp->t_inpcb->inp_socket, 674911Swnj PRU_SLOWTIMO, (struct mbuf *)0, 684911Swnj (caddr_t)i); 694735Swnj tmp++; 704735Swnj } 714567Swnj tp->t_xmt++; 724567Swnj } 734809Swnj tcp_iss += ISSINCR/2; /* increment iss */ 744567Swnj splx(s); 754497Swnj } 764497Swnj 774731Swnj /* 784734Swnj * Cancel all timers for tcp tp. 794734Swnj */ 804734Swnj tcp_tcancel(tp) 814886Swnj struct tcpcb *tp; 824734Swnj { 834809Swnj register short *tmp = &tp->t_init; 844734Swnj register int i; 854734Swnj 864734Swnj for (i = 0; i < TNTIMERS; i++) 874734Swnj *tmp++ = 0; 884734Swnj } 894734Swnj 904954Swnj struct tcpcb *tcp_newtcpcb(); 914734Swnj /* 924731Swnj * Process a TCP user request for tcp tb. If this is a send request 934731Swnj * then m is the mbuf chain of send data. If this is a timer expiration 944731Swnj * (called from the software clock routine), then timertype tells which timer. 954731Swnj */ 964809Swnj tcp_usrreq(so, req, m, addr) 974809Swnj struct socket *so; 984809Swnj int req; 994731Swnj struct mbuf *m; 1004809Swnj caddr_t addr; 1014497Swnj { 1024886Swnj register struct inpcb *inp = sotoinpcb(so); 1034911Swnj register struct tcpcb *tp; 1044567Swnj int s = splnet(); 1054567Swnj register int nstate; 1064584Swnj #ifdef TCPDEBUG 1074584Swnj struct tcp_debug tdb; 1084584Swnj #endif 1094809Swnj int error = 0; 1104567Swnj COUNT(TCP_USRREQ); 1114497Swnj 1124886Swnj /* 1134886Swnj * Make sure attached. If not, 1144886Swnj * only PRU_ATTACH is valid. 1154886Swnj */ 1164911Swnj #ifdef TCPDEBUG 1174911Swnj tdb.td_tod = 0; 1184911Swnj #endif 1194911Swnj if (inp == 0) { 1204886Swnj if (req != PRU_ATTACH) { 1214886Swnj splx(s); 1224886Swnj return (EINVAL); 1234886Swnj } 1244911Swnj } else { 1254911Swnj tp = intotcpcb(inp); 1264911Swnj nstate = tp->t_state; 1274731Swnj #ifdef KPROF 1284911Swnj tcp_acounts[nstate][req]++; 1294731Swnj #endif 1304584Swnj #ifdef TCPDEBUG 1314911Swnj if (((tp->t_socket->so_options & SO_DEBUG) || tcpconsdebug)) { 1324911Swnj tdb_setup(tp, (struct tcpiphdr *)0, req, &tdb); 1334911Swnj tdb.td_tim = timertype; 1344911Swnj } 1354584Swnj #endif 1364911Swnj tp->tc_flags &= ~TC_NET_KEEP; 1374911Swnj } 1384911Swnj 1394809Swnj switch (req) { 1404497Swnj 1414809Swnj case PRU_ATTACH: 1424954Swnj if (inp) { 1434809Swnj error = EISCONN; 1444911Swnj break; 1454886Swnj } 1464954Swnj error = in_pcballoc(so, &tcb, 2048, 2048, (struct sockaddr_in *)addr); 1474954Swnj if (error) { 1484967Swnj (void) m_free(dtom(tp)); 1494954Swnj break; 1504954Swnj } 1514954Swnj inp = (struct inpcb *)so->so_pcb; 152*5062Swnj if (so->so_options & SO_ACCEPTCONN) { 153*5062Swnj tp = tcp_newtcpcb(inp); 154*5062Swnj if (tp == 0) { 155*5062Swnj error = ENOBUFS; 156*5062Swnj break; 157*5062Swnj } 1584886Swnj nstate = LISTEN; 159*5062Swnj } else 1604886Swnj nstate = CLOSED; 1614567Swnj break; 1624497Swnj 1634809Swnj case PRU_DETACH: 1644809Swnj break; 1654809Swnj 1664809Swnj case PRU_CONNECT: 1674954Swnj error = in_pcbsetpeer(inp, (struct sockaddr_in *)addr); 1684954Swnj if (error) 1694886Swnj break; 170*5062Swnj tp = tcp_newtcpcb(inp); 171*5062Swnj if (tp == 0) { 172*5062Swnj inp->inp_faddr.s_addr = 0; 173*5062Swnj error = ENOBUFS; 174*5062Swnj break; 175*5062Swnj } 176*5062Swnj tp->t_inpcb = inp; 177*5062Swnj inp->inp_ppcb = (caddr_t)tp; 1784911Swnj (void) tcp_sndctl(tp); 1794567Swnj nstate = SYN_SENT; 1804886Swnj soisconnecting(so); 1814567Swnj break; 1824497Swnj 1834925Swnj case PRU_ACCEPT: 1844954Swnj soisconnected(so); 1854954Swnj break; 1864925Swnj 1874809Swnj case PRU_DISCONNECT: 1884886Swnj if (nstate < ESTAB) 1894911Swnj tcp_disconnect(tp); 1904886Swnj else { 1914886Swnj tp->tc_flags |= TC_SND_FIN; 1924911Swnj (void) tcp_sndctl(tp); 1934886Swnj soisdisconnecting(so); 1944886Swnj } 1954809Swnj break; 1964809Swnj 1974809Swnj case PRU_SHUTDOWN: 1984731Swnj switch (nstate) { 1994497Swnj 2004731Swnj case LISTEN: 2014734Swnj case SYN_SENT: 2024731Swnj nstate = CLOSED; 2034731Swnj break; 2044731Swnj 2054734Swnj case SYN_RCVD: 2064731Swnj case L_SYN_RCVD: 2074731Swnj case ESTAB: 2084734Swnj case CLOSE_WAIT: 2094731Swnj tp->tc_flags |= TC_SND_FIN; 2104911Swnj (void) tcp_sndctl(tp); 2114731Swnj nstate = nstate != CLOSE_WAIT ? FIN_W1 : LAST_ACK; 2124731Swnj break; 2134731Swnj 2144731Swnj case FIN_W1: 2154731Swnj case FIN_W2: 2164731Swnj case TIME_WAIT: 2174731Swnj case CLOSING: 2184731Swnj case LAST_ACK: 2194731Swnj case RCV_WAIT: 2204731Swnj break; 2214731Swnj 2224731Swnj default: 2234731Swnj goto bad; 2244731Swnj } 2254567Swnj break; 2264497Swnj 2274809Swnj case PRU_RCVD: 2284731Swnj if (nstate < ESTAB || nstate == CLOSED) 2294731Swnj goto bad; 2304734Swnj tcp_sndwin(tp); 2314809Swnj if (nstate == RCV_WAIT && rcv_empty(tp)) 2324567Swnj nstate = CLOSED; 2334567Swnj break; 2344497Swnj 2354809Swnj case PRU_SEND: 2364731Swnj switch (nstate) { 2374567Swnj 2384731Swnj case ESTAB: 2394731Swnj case CLOSE_WAIT: 2404886Swnj tcp_usrsend(tp, m); 2414731Swnj break; 2424731Swnj 2434731Swnj default: 2444731Swnj if (nstate < ESTAB) 2454731Swnj goto bad; 2464809Swnj m_freem(m); 2474886Swnj error = ENOTCONN; 2484731Swnj break; 2494731Swnj } 2504567Swnj break; 2514567Swnj 2524809Swnj case PRU_ABORT: 2534886Swnj tcp_abort(tp); 2544567Swnj nstate = CLOSED; 2554567Swnj break; 2564567Swnj 2574809Swnj case PRU_CONTROL: 2584886Swnj error = EOPNOTSUPP; 2594809Swnj break; 2604809Swnj 2614809Swnj case PRU_SLOWTIMO: 2624809Swnj switch (nstate) { 2634809Swnj 2644809Swnj case 0: 2654809Swnj case CLOSED: 2664809Swnj case LISTEN: 2674809Swnj goto bad; 2684809Swnj 2694809Swnj default: 2704809Swnj nstate = tcp_timers(tp, (int)addr); 2714809Swnj } 2724809Swnj break; 2734809Swnj 2744731Swnj default: 2754731Swnj panic("tcp_usrreq"); 2764731Swnj bad: 2774731Swnj printf("tcp: bad state: tcb=%x state=%d input=%d\n", 2784809Swnj tp, tp->t_state, req); 2794731Swnj nstate = EFAILEC; 2804567Swnj break; 2814567Swnj } 2824567Swnj #ifdef TCPDEBUG 2834605Swnj if (tdb.td_tod) 2844605Swnj tdb_stuff(&tdb, nstate); 2854567Swnj #endif 2864567Swnj switch (nstate) { 2874567Swnj 2884584Swnj case CLOSED: 2894567Swnj case SAME: 2904567Swnj break; 2914567Swnj 2924567Swnj case EFAILEC: 2934731Swnj if (m) 2944731Swnj m_freem(dtom(m)); 2954567Swnj break; 2964567Swnj 2974567Swnj default: 2984567Swnj tp->t_state = nstate; 2994567Swnj break; 3004567Swnj } 3014567Swnj splx(s); 3024886Swnj return (error); 3034497Swnj } 3044497Swnj 3054954Swnj struct tcpcb * 306*5062Swnj tcp_newtcpcb(inp) 307*5062Swnj struct inpcb *inp; 3084809Swnj { 3094954Swnj struct mbuf *m = m_getclr(0); 3104954Swnj register struct tcpcb *tp; 3114954Swnj COUNT(TCP_NEWTCPCB); 3124497Swnj 3134954Swnj if (m == 0) 3144954Swnj return (0); 3154954Swnj tp = mtod(m, struct tcpcb *); 3164954Swnj 3174682Swnj /* 3184886Swnj * Make empty reassembly queue. 3194682Swnj */ 3204886Swnj tp->seg_next = tp->seg_prev = (struct tcpiphdr *)tp; 3214497Swnj 3224682Swnj /* 3234886Swnj * Initialize sequence numbers and round trip retransmit timer. 3244682Swnj */ 3254567Swnj tp->t_xmtime = T_REXMT; 3264682Swnj tp->snd_end = tp->seq_fin = tp->snd_nxt = tp->snd_hi = tp->snd_una = 3274682Swnj tp->iss = tcp_iss; 3284567Swnj tp->snd_off = tp->iss + 1; 3294664Swnj tcp_iss += (ISSINCR >> 1) + 1; 330*5062Swnj 331*5062Swnj /* 332*5062Swnj * Hook to inpcb. 333*5062Swnj */ 334*5062Swnj tp->t_inpcb = inp; 335*5062Swnj inp->inp_ppcb = (caddr_t)tp; 3364954Swnj return (tp); 3374497Swnj } 3384497Swnj 3394886Swnj tcp_disconnect(tp) 3404886Swnj register struct tcpcb *tp; 3414886Swnj { 3424886Swnj register struct tcpiphdr *t; 3434886Swnj 3444954Swnj COUNT(TCP_DISCONNECT); 3454734Swnj tcp_tcancel(tp); 3464886Swnj t = tp->seg_next; 3474900Swnj for (; t != (struct tcpiphdr *)tp; t = (struct tcpiphdr *)t->ti_next) 3484567Swnj m_freem(dtom(t)); 3494886Swnj tcp_drainunack(tp); 3504734Swnj if (tp->t_template) { 3514911Swnj (void) m_free(dtom(tp->t_template)); 3524734Swnj tp->t_template = 0; 3534664Swnj } 3544886Swnj in_pcbfree(tp->t_inpcb); 355*5062Swnj (void) m_free(dtom(tp)); 3564497Swnj } 3574497Swnj 3584911Swnj tcp_abort(tp) 3594911Swnj register struct tcpcb *tp; 3604886Swnj { 3614886Swnj 3624954Swnj COUNT(TCP_ABORT); 3634886Swnj switch (tp->t_state) { 3644886Swnj 3654886Swnj case SYN_RCVD: 3664886Swnj case ESTAB: 3674886Swnj case FIN_W1: 3684886Swnj case FIN_W2: 3694886Swnj case CLOSE_WAIT: 3704886Swnj tp->tc_flags |= TC_SND_RST; 3714886Swnj tcp_sndnull(tp); 3724886Swnj } 3734911Swnj soisdisconnected(tp->t_inpcb->inp_socket); 374*5062Swnj tcp_disconnect(tp); 3754886Swnj } 3764886Swnj 3774682Swnj /* 3784734Swnj * Send data queue headed by m0 into the protocol. 3794682Swnj */ 3804678Swnj tcp_usrsend(tp, m0) 3814886Swnj register struct tcpcb *tp; 3824584Swnj struct mbuf *m0; 3834497Swnj { 3844886Swnj register struct socket *so = tp->t_inpcb->inp_socket; 3854682Swnj COUNT(TCP_USRSEND); 3864497Swnj 3874886Swnj sbappend(&so->so_snd, m0); 3884809Swnj if (tp->t_options & TO_EOL) 3894886Swnj tp->snd_end = tp->snd_off + so->so_snd.sb_cc; 3904809Swnj if (tp->t_options & TO_URG) { 3914886Swnj tp->snd_urp = tp->snd_off + so->so_snd.sb_cc + 1; 3924576Swnj tp->tc_flags |= TC_SND_URG; 3934567Swnj } 3944911Swnj (void) tcp_send(tp); 3954497Swnj } 3964497Swnj 3974682Swnj /* 3984682Swnj * TCP timer went off processing. 3994682Swnj */ 4004584Swnj tcp_timers(tp, timertype) 4014886Swnj register struct tcpcb *tp; 4024584Swnj int timertype; 4034497Swnj { 4044497Swnj 4054567Swnj COUNT(TCP_TIMERS); 4064584Swnj switch (timertype) { 4074497Swnj 4084567Swnj case TFINACK: /* fin-ack timer */ 4094567Swnj switch (tp->t_state) { 4104497Swnj 4114567Swnj case TIME_WAIT: 4124567Swnj /* 4134567Swnj * We can be sure our ACK of foreign FIN was rcvd, 4144567Swnj * and can close if no data left for user. 4154567Swnj */ 4164567Swnj if (rcv_empty(tp)) { 4174886Swnj tcp_disconnect(tp); 4184567Swnj return (CLOSED); 4194567Swnj } 4204567Swnj return (RCV_WAIT); /* 17 */ 4214497Swnj 4224731Swnj case CLOSING: 4234576Swnj tp->tc_flags |= TC_WAITED_2_ML; 4244567Swnj return (SAME); 4254497Swnj 4264567Swnj default: 4274567Swnj return (SAME); 4284567Swnj } 4294497Swnj 4304567Swnj case TREXMT: /* retransmission timer */ 4314567Swnj if (tp->t_rexmt_val > tp->snd_una) { /* 34 */ 4324567Swnj /* 4334809Swnj * Set so for a retransmission, increase rexmt time 4344567Swnj * in case of multiple retransmissions. 4354567Swnj */ 4364567Swnj tp->snd_nxt = tp->snd_una; 4374576Swnj tp->tc_flags |= TC_REXMT; 4384567Swnj tp->t_xmtime = tp->t_xmtime << 1; 4394567Swnj if (tp->t_xmtime > T_REMAX) 4404567Swnj tp->t_xmtime = T_REMAX; 4414911Swnj (void) tcp_send(tp); 4424567Swnj } 4434567Swnj return (SAME); 4444497Swnj 4454567Swnj case TREXMTTL: /* retransmit too long */ 446*5062Swnj if (tp->t_rtl_val > tp->snd_una) { /* 36 */ 447*5062Swnj tcp_error(tp, ETIMEDOUT); 4484567Swnj return (CLOSED); 4494567Swnj } 4504567Swnj return (SAME); 4514497Swnj 4524567Swnj case TPERSIST: /* persist timer */ 4534567Swnj /* 4544567Swnj * Force a byte send through closed window. 4554567Swnj */ 4564576Swnj tp->tc_flags |= TC_FORCE_ONE; 4574911Swnj (void) tcp_send(tp); 4584567Swnj return (SAME); 4594567Swnj } 4604567Swnj panic("tcp_timers"); 4614911Swnj /*NOTREACHED*/ 4624497Swnj } 4634497Swnj 4644911Swnj /*ARGSUSED*/ 4654886Swnj tcp_sense(m) 4664886Swnj struct mbuf *m; 4674886Swnj { 4684886Swnj 4694954Swnj COUNT(TCP_SENSE); 4704886Swnj return (EOPNOTSUPP); 4714886Swnj } 4724886Swnj 473*5062Swnj tcp_drop(tp, errno) 4744911Swnj struct tcpcb *tp; 4754809Swnj int errno; 4764497Swnj { 4774911Swnj struct socket *so = tp->t_inpcb->inp_socket; 4784954Swnj 4794911Swnj COUNT(TCP_ERROR); 4804809Swnj so->so_error = errno; 4814886Swnj sorwakeup(so); 4824886Swnj sowwakeup(so); 483*5062Swnj tcp_disconnect(tp); 4844497Swnj } 4854584Swnj 4864584Swnj #ifdef TCPDEBUG 4874682Swnj /* 4884682Swnj * TCP debugging utility subroutines. 4894682Swnj * THE NAMES OF THE FIELDS USED BY THESE ROUTINES ARE STUPID. 4904682Swnj */ 4914670Swnj tdb_setup(tp, n, input, tdp) 4924886Swnj struct tcpcb *tp; 4934886Swnj register struct tcpiphdr *n; 4944670Swnj int input; 4954670Swnj register struct tcp_debug *tdp; 4964670Swnj { 4974670Swnj 4984682Swnj COUNT(TDB_SETUP); 4994670Swnj tdp->td_tod = time; 5004670Swnj tdp->td_tcb = tp; 5014670Swnj tdp->td_old = tp->t_state; 5024670Swnj tdp->td_inp = input; 5034670Swnj tdp->td_tim = 0; 5044670Swnj tdp->td_new = -1; 5054670Swnj if (n) { 5064900Swnj tdp->td_sno = n->ti_seq; 5074900Swnj tdp->td_ano = n->ti_ackno; 5084670Swnj tdp->td_wno = n->t_win; 5094900Swnj tdp->td_lno = n->ti_len; 5104900Swnj tdp->td_flg = n->ti_flags; 5114670Swnj } else 5124670Swnj tdp->td_sno = tdp->td_ano = tdp->td_wno = tdp->td_lno = 5134670Swnj tdp->td_flg = 0; 5144670Swnj } 5154670Swnj 5164670Swnj tdb_stuff(tdp, nstate) 5174670Swnj struct tcp_debug *tdp; 5184670Swnj int nstate; 5194670Swnj { 5204682Swnj COUNT(TDB_STUFF); 5214670Swnj 5224670Swnj tdp->td_new = nstate; 5234670Swnj tcp_debug[tdbx++ % TDBSIZE] = *tdp; 5244670Swnj if (tcpconsdebug & 2) 5254670Swnj tcp_prt(tdp); 5264670Swnj } 5274682Swnj 5284682Swnj tcp_prt(tdp) 5294682Swnj register struct tcp_debug *tdp; 5304682Swnj { 5314682Swnj COUNT(TCP_PRT); 5324682Swnj 5334698Swnj printf("%x ", ((int)tdp->td_tcb)&0xffffff); 5344698Swnj if (tdp->td_inp == INSEND) { 5354698Swnj printf("SEND #%x", tdp->td_sno); 5364698Swnj tdp->td_lno = ntohs(tdp->td_lno); 5374698Swnj tdp->td_wno = ntohs(tdp->td_wno); 5384698Swnj } else { 5394698Swnj if (tdp->td_inp == INRECV) 5404698Swnj printf("RCV #%x ", tdp->td_sno); 5414698Swnj printf("%s.%s", 5424698Swnj tcpstates[tdp->td_old], tcpinputs[tdp->td_inp]); 5434698Swnj if (tdp->td_inp == ISTIMER) 5444698Swnj printf("(%s)", tcptimers[tdp->td_tim]); 5454698Swnj printf(" -> %s", 5464698Swnj tcpstates[(tdp->td_new > 0) ? tdp->td_new : tdp->td_old]); 5474698Swnj if (tdp->td_new == -1) 5484698Swnj printf(" (FAILED)"); 5494698Swnj } 5504682Swnj /* GROSS... DEPENDS ON SIGN EXTENSION OF CHARACTERS */ 5514698Swnj if (tdp->td_lno) 5524698Swnj printf(" len=%d", tdp->td_lno); 5534698Swnj if (tdp->td_wno) 5544698Swnj printf(" win=%d", tdp->td_wno); 5554698Swnj if (tdp->td_flg & TH_FIN) printf(" FIN"); 5564698Swnj if (tdp->td_flg & TH_SYN) printf(" SYN"); 5574698Swnj if (tdp->td_flg & TH_RST) printf(" RST"); 5584698Swnj if (tdp->td_flg & TH_EOL) printf(" EOL"); 5594698Swnj if (tdp->td_flg & TH_ACK) printf(" ACK %x", tdp->td_ano); 5604698Swnj if (tdp->td_flg & TH_URG) printf(" URG"); 5614682Swnj printf("\n"); 5624682Swnj } 5634670Swnj #endif 564