1 /* tcp_subr.c 4.2 81/11/25 */ 2 3 #include "../h/param.h" 4 #include "../h/systm.h" 5 #include "../h/mbuf.h" 6 #include "../h/socket.h" 7 #include "../h/socketvar.h" 8 #include "../h/protosw.h" 9 #include "../net/inet.h" 10 #include "../net/inet_pcb.h" 11 #include "../net/inet_systm.h" 12 #include "../net/if.h" 13 #include "../net/imp.h" 14 #include "../net/ip.h" 15 #include "../net/ip_var.h" 16 #include "../net/tcp.h" 17 #define TCPFSTAB 18 #include "../net/tcp_fsm.h" 19 #include "../net/tcp_var.h" 20 #include "/usr/include/errno.h" 21 22 /* 23 * Tcp initialization 24 */ 25 tcp_init() 26 { 27 28 tcp_iss = 1; /* wrong */ 29 tcb.inp_next = tcb.inp_prev = &tcb; 30 } 31 32 /* 33 * Create template to be used to send tcp packets on a connection. 34 * Call after host entry created, allocates an mbuf and fills 35 * in a skeletal tcp/ip header, minimizing the amount of work 36 * necessary when the connection is used. 37 */ 38 struct tcpiphdr * 39 tcp_template(tp) 40 struct tcpcb *tp; 41 { 42 register struct inpcb *inp = tp->t_inpcb; 43 register struct mbuf *m; 44 register struct tcpiphdr *n; 45 46 COUNT(TCP_TEMPLATE); 47 m = m_get(1); 48 if (m == 0) 49 return (0); 50 m->m_off = MMAXOFF - sizeof (struct tcpiphdr); 51 m->m_len = sizeof (struct tcpiphdr); 52 n = mtod(m, struct tcpiphdr *); 53 n->ti_next = n->ti_prev = 0; 54 n->ti_x1 = 0; 55 n->ti_pr = IPPROTO_TCP; 56 n->ti_len = htons(sizeof (struct tcpiphdr) - sizeof (struct ip)); 57 n->ti_src = inp->inp_laddr; 58 n->ti_dst = inp->inp_faddr; 59 n->ti_sport = inp->inp_lport; 60 n->ti_dport = inp->inp_fport; 61 n->ti_seq = 0; 62 n->ti_ackno = 0; 63 n->ti_x2 = 0; 64 n->ti_off = 5; 65 n->ti_flags = 0; 66 n->ti_win = 0; 67 n->ti_sum = 0; 68 n->ti_urp = 0; 69 return (n); 70 } 71 72 /* 73 * Reflect a control message back to sender of tcp segment ti, 74 * with ack, seq and flags fields as specified by parameters. 75 */ 76 tcp_reflect(ti, ack, seq, flags) 77 register struct tcpiphdr *ti; 78 tcpseq_t ack, seq; 79 int flags; 80 { 81 82 m_freem(m->m_next); 83 m->m_next = 0; 84 m->m_len = sizeof(struct tcpiphdr); 85 #define xchg(a,b) j=a; a=b; b=j 86 xchg(ti->ti_dst.s_addr, ti->ti_src.s_addr); 87 xchg(ti->ti_dport, ti->ti_sport); 88 #undef xchg 89 ti->ti_ack = htonl(ack); 90 ti->ti_seq = htonl(seq); 91 ti->ti_flags = flags; 92 93 ti->ti_len = htons(sizeof (struct tcphdr)); 94 ti->ti_off = 5; 95 ti->ti_sum = inet_cksum(m, sizeof(struct tcpiphdr)); 96 ((struct ip *)ti)->ip_len = sizeof(struct tcpiphdr); 97 ((struct ip *)ti)->ip_ttl = MAXTTL; 98 ip_output(m); 99 } 100 101 struct tcpcb * 102 tcp_newtcpcb(inp) 103 struct inpcb *inp; 104 { 105 struct mbuf *m = m_getclr(0); 106 register struct tcpcb *tp; 107 COUNT(TCP_NEWTCPCB); 108 109 if (m == 0) 110 return (0); 111 tp = mtod(m, struct tcpcb *); 112 113 /* 114 * Make empty reassembly queue. 115 */ 116 tp->seg_next = tp->seg_prev = (struct tcpiphdr *)tp; 117 118 /* 119 * Initialize sequence numbers and round trip retransmit timer. 120 */ 121 tp->t_xmtime = T_REXMT; 122 tp->snd_end = tp->seq_fin = tp->snd_nxt = tp->snd_hi = tp->snd_una = 123 tp->iss = tcp_iss; 124 tp->snd_off = tp->iss + 1; 125 tcp_iss += (ISSINCR >> 1) + 1; 126 127 /* 128 * Hook to inpcb. 129 */ 130 tp->t_inpcb = inp; 131 inp->inp_ppcb = (caddr_t)tp; 132 return (tp); 133 } 134 135 tcp_drop(tp, errno) 136 struct tcpcb *tp; 137 int errno; 138 { 139 struct socket *so = tp->t_inpcb->inp_socket; 140 141 COUNT(TCP_DROP); 142 if (TCPS_HAVERCVDSYN(tp->t_state) && 143 TCPS_OURFINISACKED(tp->t_state) == 0) { 144 tp->t_state = TCPS_CLOSED; 145 tcp_output(tp); 146 } 147 so->so_error = errno; 148 socantrcvmore(so); 149 socantsndmore(so); 150 tcp_close(tp); 151 } 152 153 tcp_close(tp) 154 register struct tcpcb *tp; 155 { 156 register struct tcpiphdr *t; 157 158 COUNT(TCP_CLOSE); 159 tcp_canceltimers(tp); 160 t = tp->seg_next; 161 for (; t != (struct tcpiphdr *)tp; t = (struct tcpiphdr *)t->ti_next) 162 m_freem(dtom(t)); 163 if (tp->t_template) { 164 (void) m_free(dtom(tp->t_template)); 165 tp->t_template = 0; 166 } 167 in_pcbfree(tp->t_inpcb); 168 (void) m_free(dtom(tp)); 169 } 170 171 /*ARGSUSED*/ 172 tcp_sense(m) 173 struct mbuf *m; 174 { 175 176 COUNT(TCP_SENSE); 177 return (EOPNOTSUPP); 178 } 179 180 tcp_drain() 181 { 182 register struct inpcb *ip; 183 184 COUNT(TCP_DRAIN); 185 } 186 187 tcp_ctlinput(m) 188 struct mbuf *m; 189 { 190 191 COUNT(TCP_CTLINPUT); 192 m_freem(m); 193 } 194