1*6591Ssam /* tcp_subr.c 4.25 82/04/25 */ 25068Swnj 35068Swnj #include "../h/param.h" 45068Swnj #include "../h/systm.h" 55068Swnj #include "../h/mbuf.h" 65068Swnj #include "../h/socket.h" 75068Swnj #include "../h/socketvar.h" 85068Swnj #include "../h/protosw.h" 95089Swnj #include "../net/in.h" 106353Ssam #include "../net/route.h" 115089Swnj #include "../net/in_pcb.h" 125089Swnj #include "../net/in_systm.h" 135068Swnj #include "../net/if.h" 145068Swnj #include "../net/ip.h" 155068Swnj #include "../net/ip_var.h" 16*6591Ssam #include "../net/ip_icmp.h" 175068Swnj #include "../net/tcp.h" 185068Swnj #include "../net/tcp_fsm.h" 195089Swnj #include "../net/tcp_seq.h" 205089Swnj #include "../net/tcp_timer.h" 215068Swnj #include "../net/tcp_var.h" 225089Swnj #include "../net/tcpip.h" 235111Swnj #include "../errno.h" 245068Swnj 255068Swnj /* 265068Swnj * Tcp initialization 275068Swnj */ 285068Swnj tcp_init() 295068Swnj { 305068Swnj 315089Swnj COUNT(TCP_INIT); 325068Swnj tcp_iss = 1; /* wrong */ 335068Swnj tcb.inp_next = tcb.inp_prev = &tcb; 345164Swnj tcp_alpha = TCP_ALPHA; 355164Swnj tcp_beta = TCP_BETA; 365068Swnj } 375068Swnj 385068Swnj /* 395068Swnj * Create template to be used to send tcp packets on a connection. 405068Swnj * Call after host entry created, allocates an mbuf and fills 415068Swnj * in a skeletal tcp/ip header, minimizing the amount of work 425068Swnj * necessary when the connection is used. 435068Swnj */ 445068Swnj struct tcpiphdr * 455068Swnj tcp_template(tp) 465068Swnj struct tcpcb *tp; 475068Swnj { 485068Swnj register struct inpcb *inp = tp->t_inpcb; 495068Swnj register struct mbuf *m; 505068Swnj register struct tcpiphdr *n; 515068Swnj 525068Swnj COUNT(TCP_TEMPLATE); 535852Sroot m = m_get(M_WAIT); 545068Swnj if (m == 0) 555068Swnj return (0); 565068Swnj m->m_off = MMAXOFF - sizeof (struct tcpiphdr); 575068Swnj m->m_len = sizeof (struct tcpiphdr); 585068Swnj n = mtod(m, struct tcpiphdr *); 595068Swnj n->ti_next = n->ti_prev = 0; 605068Swnj n->ti_x1 = 0; 615068Swnj n->ti_pr = IPPROTO_TCP; 625068Swnj n->ti_len = htons(sizeof (struct tcpiphdr) - sizeof (struct ip)); 635068Swnj n->ti_src = inp->inp_laddr; 645068Swnj n->ti_dst = inp->inp_faddr; 655068Swnj n->ti_sport = inp->inp_lport; 665068Swnj n->ti_dport = inp->inp_fport; 675068Swnj n->ti_seq = 0; 685089Swnj n->ti_ack = 0; 695068Swnj n->ti_x2 = 0; 705068Swnj n->ti_off = 5; 715068Swnj n->ti_flags = 0; 725068Swnj n->ti_win = 0; 735068Swnj n->ti_sum = 0; 745068Swnj n->ti_urp = 0; 755068Swnj return (n); 765068Swnj } 775068Swnj 785068Swnj /* 795164Swnj * Send a single message to the TCP at address specified by 805164Swnj * the given TCP/IP header. If flags==0, then we make a copy 815164Swnj * of the tcpiphdr at ti and send directly to the addressed host. 825164Swnj * This is used to force keep alive messages out using the TCP 835164Swnj * template for a connection tp->t_template. If flags are given 845164Swnj * then we send a message back to the TCP which originated the 855164Swnj * segment ti, and discard the mbuf containing it and any other 865164Swnj * attached mbufs. 875164Swnj * 885164Swnj * In any case the ack and sequence number of the transmitted 895164Swnj * segment are as specified by the parameters. 905068Swnj */ 915392Swnj tcp_respond(tp, ti, ack, seq, flags) 925392Swnj struct tcpcb *tp; 935068Swnj register struct tcpiphdr *ti; 945089Swnj tcp_seq ack, seq; 955068Swnj int flags; 965068Swnj { 975164Swnj struct mbuf *m; 986212Swnj int win = 0, tlen; 996353Ssam struct route *ro = 0; 1005068Swnj 1015089Swnj COUNT(TCP_RESPOND); 1026353Ssam if (tp) { 1035392Swnj win = sbspace(&tp->t_inpcb->inp_socket->so_rcv); 1046353Ssam ro = &tp->t_inpcb->inp_route; 1056353Ssam } 1065164Swnj if (flags == 0) { 1075585Sroot m = m_get(M_DONTWAIT); 1085164Swnj if (m == 0) 1095164Swnj return; 1105164Swnj m->m_off = MMINOFF; 1116212Swnj m->m_len = sizeof (struct tcpiphdr) + 1; 1125164Swnj *mtod(m, struct tcpiphdr *) = *ti; 1135164Swnj ti = mtod(m, struct tcpiphdr *); 1145164Swnj flags = TH_ACK; 1156212Swnj tlen = 1; 1165164Swnj } else { 1175245Sroot m = dtom(ti); 1185164Swnj m_freem(m->m_next); 1195164Swnj m->m_next = 0; 1206117Swnj m->m_off = (int)ti - (int)m; 1215164Swnj m->m_len = sizeof (struct tcpiphdr); 1225089Swnj #define xchg(a,b,type) { type t; t=a; a=b; b=t; } 1235164Swnj xchg(ti->ti_dst.s_addr, ti->ti_src.s_addr, u_long); 1245164Swnj xchg(ti->ti_dport, ti->ti_sport, u_short); 1255068Swnj #undef xchg 1266212Swnj tlen = 0; 1275164Swnj } 1285089Swnj ti->ti_next = ti->ti_prev = 0; 1295089Swnj ti->ti_x1 = 0; 1306212Swnj ti->ti_len = sizeof (struct tcphdr) + tlen; 1315245Sroot ti->ti_seq = seq; 1325245Sroot ti->ti_ack = ack; 1335245Sroot #if vax 1346163Ssam ti->ti_len = htons((u_short)ti->ti_len); 1355245Sroot ti->ti_seq = htonl(ti->ti_seq); 1365245Sroot ti->ti_ack = htonl(ti->ti_ack); 1375245Sroot #endif 1385089Swnj ti->ti_x2 = 0; 1395089Swnj ti->ti_off = sizeof (struct tcphdr) >> 2; 1405068Swnj ti->ti_flags = flags; 1415392Swnj ti->ti_win = win; 1425392Swnj #if vax 1435392Swnj ti->ti_win = htons(ti->ti_win); 1445392Swnj #endif 1455392Swnj ti->ti_urp = 0; 1466304Sroot ti->ti_sum = in_cksum(m, sizeof (struct tcpiphdr) + tlen); 1476212Swnj ((struct ip *)ti)->ip_len = sizeof (struct tcpiphdr) + tlen; 1485089Swnj ((struct ip *)ti)->ip_ttl = TCP_TTL; 1496353Ssam (void) ip_output(m, (struct mbuf *)0, ro, 0); 1505068Swnj } 1515075Swnj 1525089Swnj /* 1535089Swnj * Create a new TCP control block, making an 1545089Swnj * empty reassembly queue and hooking it to the argument 1555089Swnj * protocol control block. 1565089Swnj */ 1575075Swnj struct tcpcb * 1585075Swnj tcp_newtcpcb(inp) 1595075Swnj struct inpcb *inp; 1605075Swnj { 1615852Sroot struct mbuf *m = m_getclr(M_DONTWAIT); 1625075Swnj register struct tcpcb *tp; 1635075Swnj COUNT(TCP_NEWTCPCB); 1645075Swnj 1655075Swnj if (m == 0) 1665075Swnj return (0); 1675075Swnj tp = mtod(m, struct tcpcb *); 1685075Swnj tp->seg_next = tp->seg_prev = (struct tcpiphdr *)tp; 1695089Swnj tp->t_maxseg = 1024; 1706470Sroot tp->t_flags = 0; /* sends options! */ 1715075Swnj tp->t_inpcb = inp; 1725075Swnj inp->inp_ppcb = (caddr_t)tp; 1735075Swnj return (tp); 1745075Swnj } 1755075Swnj 1765089Swnj /* 1775089Swnj * Drop a TCP connection, reporting 1785089Swnj * the specified error. If connection is synchronized, 1795089Swnj * then send a RST to peer. 1805089Swnj */ 1815075Swnj tcp_drop(tp, errno) 1825075Swnj struct tcpcb *tp; 1835075Swnj int errno; 1845075Swnj { 1855075Swnj struct socket *so = tp->t_inpcb->inp_socket; 1865075Swnj 1875075Swnj COUNT(TCP_DROP); 1885286Sroot if (TCPS_HAVERCVDSYN(tp->t_state)) { 1895075Swnj tp->t_state = TCPS_CLOSED; 1905075Swnj tcp_output(tp); 1915075Swnj } 1925075Swnj so->so_error = errno; 1935075Swnj tcp_close(tp); 1945075Swnj } 1955075Swnj 1966584Ssam tcp_abort(inp) 1976584Ssam struct inpcb *inp; 1986584Ssam { 1996584Ssam tcp_close(inp->inp_ppcb); 2006584Ssam } 2016584Ssam 2025089Swnj /* 2035089Swnj * Close a TCP control block: 2045089Swnj * discard all space held by the tcp 2055089Swnj * discard internet protocol block 2065089Swnj * wake up any sleepers 2075089Swnj */ 2085075Swnj tcp_close(tp) 2095075Swnj register struct tcpcb *tp; 2105075Swnj { 2115075Swnj register struct tcpiphdr *t; 2125261Swnj struct inpcb *inp = tp->t_inpcb; 2135261Swnj struct socket *so = inp->inp_socket; 2145075Swnj 2155075Swnj COUNT(TCP_CLOSE); 2165075Swnj t = tp->seg_next; 2175075Swnj for (; t != (struct tcpiphdr *)tp; t = (struct tcpiphdr *)t->ti_next) 2185075Swnj m_freem(dtom(t)); 2195089Swnj if (tp->t_template) 2205075Swnj (void) m_free(dtom(tp->t_template)); 2215089Swnj if (tp->t_tcpopt) 2225089Swnj (void) m_free(dtom(tp->t_tcpopt)); 2235089Swnj if (tp->t_ipopt) 2245089Swnj (void) m_free(dtom(tp->t_ipopt)); 2255075Swnj (void) m_free(dtom(tp)); 2265261Swnj inp->inp_ppcb = 0; 2276472Sroot soisdisconnected(so); 2285269Sroot in_pcbdetach(inp); 2295075Swnj } 2305075Swnj 2315075Swnj tcp_drain() 2325075Swnj { 2335075Swnj 2345075Swnj COUNT(TCP_DRAIN); 2355075Swnj } 2365075Swnj 2376584Ssam tcp_ctlinput(cmd, arg) 2386584Ssam int cmd; 2396584Ssam caddr_t arg; 2405075Swnj { 241*6591Ssam struct in_addr *sin; 242*6591Ssam extern u_char inetctlerrmap[]; 2435075Swnj COUNT(TCP_CTLINPUT); 244*6591Ssam 245*6591Ssam if (cmd < 0 || cmd > PRC_NCMDS) 246*6591Ssam return; 247*6591Ssam switch (cmd) { 248*6591Ssam 249*6591Ssam case PRC_ROUTEDEAD: 250*6591Ssam break; 251*6591Ssam 252*6591Ssam case PRC_QUENCH: 253*6591Ssam break; 254*6591Ssam 255*6591Ssam /* these are handled by ip */ 256*6591Ssam case PRC_IFDOWN: 257*6591Ssam case PRC_HOSTDEAD: 258*6591Ssam case PRC_HOSTUNREACH: 259*6591Ssam break; 260*6591Ssam 261*6591Ssam default: 262*6591Ssam sin = &((struct icmp *)arg)->icmp_ip.ip_dst; 263*6591Ssam in_pcbnotify(&tcb, sin, inetctlerrmap[cmd], tcp_abort); 264*6591Ssam } 2655075Swnj } 266