Commit | Line | Data |
---|---|---|
3e60e1e6 | 1 | /* tcp_subr.c 4.22 82/04/04 */ |
ecaa4e6f BJ |
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" | |
0974b45c | 9 | #include "../net/in.h" |
c124e997 | 10 | #include "../net/route.h" |
0974b45c BJ |
11 | #include "../net/in_pcb.h" |
12 | #include "../net/in_systm.h" | |
ecaa4e6f | 13 | #include "../net/if.h" |
ecaa4e6f BJ |
14 | #include "../net/ip.h" |
15 | #include "../net/ip_var.h" | |
16 | #include "../net/tcp.h" | |
ecaa4e6f | 17 | #include "../net/tcp_fsm.h" |
0974b45c BJ |
18 | #include "../net/tcp_seq.h" |
19 | #include "../net/tcp_timer.h" | |
ecaa4e6f | 20 | #include "../net/tcp_var.h" |
0974b45c | 21 | #include "../net/tcpip.h" |
f1b2fa5b | 22 | #include "../errno.h" |
ecaa4e6f BJ |
23 | |
24 | /* | |
25 | * Tcp initialization | |
26 | */ | |
27 | tcp_init() | |
28 | { | |
29 | ||
0974b45c | 30 | COUNT(TCP_INIT); |
ecaa4e6f BJ |
31 | tcp_iss = 1; /* wrong */ |
32 | tcb.inp_next = tcb.inp_prev = &tcb; | |
405c9168 BJ |
33 | tcp_alpha = TCP_ALPHA; |
34 | tcp_beta = TCP_BETA; | |
ecaa4e6f BJ |
35 | } |
36 | ||
37 | /* | |
38 | * Create template to be used to send tcp packets on a connection. | |
39 | * Call after host entry created, allocates an mbuf and fills | |
40 | * in a skeletal tcp/ip header, minimizing the amount of work | |
41 | * necessary when the connection is used. | |
42 | */ | |
43 | struct tcpiphdr * | |
44 | tcp_template(tp) | |
45 | struct tcpcb *tp; | |
46 | { | |
47 | register struct inpcb *inp = tp->t_inpcb; | |
48 | register struct mbuf *m; | |
49 | register struct tcpiphdr *n; | |
50 | ||
51 | COUNT(TCP_TEMPLATE); | |
e6b33a03 | 52 | m = m_get(M_WAIT); |
ecaa4e6f BJ |
53 | if (m == 0) |
54 | return (0); | |
55 | m->m_off = MMAXOFF - sizeof (struct tcpiphdr); | |
56 | m->m_len = sizeof (struct tcpiphdr); | |
57 | n = mtod(m, struct tcpiphdr *); | |
58 | n->ti_next = n->ti_prev = 0; | |
59 | n->ti_x1 = 0; | |
60 | n->ti_pr = IPPROTO_TCP; | |
61 | n->ti_len = htons(sizeof (struct tcpiphdr) - sizeof (struct ip)); | |
62 | n->ti_src = inp->inp_laddr; | |
63 | n->ti_dst = inp->inp_faddr; | |
64 | n->ti_sport = inp->inp_lport; | |
65 | n->ti_dport = inp->inp_fport; | |
66 | n->ti_seq = 0; | |
0974b45c | 67 | n->ti_ack = 0; |
ecaa4e6f BJ |
68 | n->ti_x2 = 0; |
69 | n->ti_off = 5; | |
70 | n->ti_flags = 0; | |
71 | n->ti_win = 0; | |
72 | n->ti_sum = 0; | |
73 | n->ti_urp = 0; | |
74 | return (n); | |
75 | } | |
76 | ||
77 | /* | |
405c9168 BJ |
78 | * Send a single message to the TCP at address specified by |
79 | * the given TCP/IP header. If flags==0, then we make a copy | |
80 | * of the tcpiphdr at ti and send directly to the addressed host. | |
81 | * This is used to force keep alive messages out using the TCP | |
82 | * template for a connection tp->t_template. If flags are given | |
83 | * then we send a message back to the TCP which originated the | |
84 | * segment ti, and discard the mbuf containing it and any other | |
85 | * attached mbufs. | |
86 | * | |
87 | * In any case the ack and sequence number of the transmitted | |
88 | * segment are as specified by the parameters. | |
ecaa4e6f | 89 | */ |
8e65fd66 BJ |
90 | tcp_respond(tp, ti, ack, seq, flags) |
91 | struct tcpcb *tp; | |
ecaa4e6f | 92 | register struct tcpiphdr *ti; |
0974b45c | 93 | tcp_seq ack, seq; |
ecaa4e6f BJ |
94 | int flags; |
95 | { | |
405c9168 | 96 | struct mbuf *m; |
1e977657 | 97 | int win = 0, tlen; |
c124e997 | 98 | struct route *ro = 0; |
ecaa4e6f | 99 | |
0974b45c | 100 | COUNT(TCP_RESPOND); |
c124e997 | 101 | if (tp) { |
8e65fd66 | 102 | win = sbspace(&tp->t_inpcb->inp_socket->so_rcv); |
c124e997 SL |
103 | ro = &tp->t_inpcb->inp_route; |
104 | } | |
405c9168 | 105 | if (flags == 0) { |
ef9b4258 | 106 | m = m_get(M_DONTWAIT); |
405c9168 BJ |
107 | if (m == 0) |
108 | return; | |
109 | m->m_off = MMINOFF; | |
1e977657 | 110 | m->m_len = sizeof (struct tcpiphdr) + 1; |
405c9168 BJ |
111 | *mtod(m, struct tcpiphdr *) = *ti; |
112 | ti = mtod(m, struct tcpiphdr *); | |
113 | flags = TH_ACK; | |
1e977657 | 114 | tlen = 1; |
405c9168 | 115 | } else { |
4aed14e3 | 116 | m = dtom(ti); |
405c9168 BJ |
117 | m_freem(m->m_next); |
118 | m->m_next = 0; | |
1acff8ec | 119 | m->m_off = (int)ti - (int)m; |
405c9168 | 120 | m->m_len = sizeof (struct tcpiphdr); |
0974b45c | 121 | #define xchg(a,b,type) { type t; t=a; a=b; b=t; } |
405c9168 BJ |
122 | xchg(ti->ti_dst.s_addr, ti->ti_src.s_addr, u_long); |
123 | xchg(ti->ti_dport, ti->ti_sport, u_short); | |
ecaa4e6f | 124 | #undef xchg |
1e977657 | 125 | tlen = 0; |
405c9168 | 126 | } |
0974b45c BJ |
127 | ti->ti_next = ti->ti_prev = 0; |
128 | ti->ti_x1 = 0; | |
1e977657 | 129 | ti->ti_len = sizeof (struct tcphdr) + tlen; |
4aed14e3 BJ |
130 | ti->ti_seq = seq; |
131 | ti->ti_ack = ack; | |
132 | #if vax | |
668cc26d | 133 | ti->ti_len = htons((u_short)ti->ti_len); |
4aed14e3 BJ |
134 | ti->ti_seq = htonl(ti->ti_seq); |
135 | ti->ti_ack = htonl(ti->ti_ack); | |
136 | #endif | |
0974b45c BJ |
137 | ti->ti_x2 = 0; |
138 | ti->ti_off = sizeof (struct tcphdr) >> 2; | |
ecaa4e6f | 139 | ti->ti_flags = flags; |
8e65fd66 BJ |
140 | ti->ti_win = win; |
141 | #if vax | |
142 | ti->ti_win = htons(ti->ti_win); | |
143 | #endif | |
144 | ti->ti_urp = 0; | |
f3cdd721 | 145 | ti->ti_sum = in_cksum(m, sizeof (struct tcpiphdr) + tlen); |
1e977657 | 146 | ((struct ip *)ti)->ip_len = sizeof (struct tcpiphdr) + tlen; |
0974b45c | 147 | ((struct ip *)ti)->ip_ttl = TCP_TTL; |
c124e997 | 148 | (void) ip_output(m, (struct mbuf *)0, ro, 0); |
ecaa4e6f | 149 | } |
a6503abf | 150 | |
0974b45c BJ |
151 | /* |
152 | * Create a new TCP control block, making an | |
153 | * empty reassembly queue and hooking it to the argument | |
154 | * protocol control block. | |
155 | */ | |
a6503abf BJ |
156 | struct tcpcb * |
157 | tcp_newtcpcb(inp) | |
158 | struct inpcb *inp; | |
159 | { | |
e6b33a03 | 160 | struct mbuf *m = m_getclr(M_DONTWAIT); |
a6503abf BJ |
161 | register struct tcpcb *tp; |
162 | COUNT(TCP_NEWTCPCB); | |
163 | ||
164 | if (m == 0) | |
165 | return (0); | |
166 | tp = mtod(m, struct tcpcb *); | |
a6503abf | 167 | tp->seg_next = tp->seg_prev = (struct tcpiphdr *)tp; |
0974b45c | 168 | tp->t_maxseg = 1024; |
3e60e1e6 | 169 | tp->t_flags = 0; /* sends options! */ |
a6503abf BJ |
170 | tp->t_inpcb = inp; |
171 | inp->inp_ppcb = (caddr_t)tp; | |
172 | return (tp); | |
173 | } | |
174 | ||
0974b45c BJ |
175 | /* |
176 | * Drop a TCP connection, reporting | |
177 | * the specified error. If connection is synchronized, | |
178 | * then send a RST to peer. | |
179 | */ | |
a6503abf BJ |
180 | tcp_drop(tp, errno) |
181 | struct tcpcb *tp; | |
182 | int errno; | |
183 | { | |
184 | struct socket *so = tp->t_inpcb->inp_socket; | |
185 | ||
186 | COUNT(TCP_DROP); | |
d3504cc0 | 187 | if (TCPS_HAVERCVDSYN(tp->t_state)) { |
a6503abf BJ |
188 | tp->t_state = TCPS_CLOSED; |
189 | tcp_output(tp); | |
190 | } | |
191 | so->so_error = errno; | |
a6503abf BJ |
192 | tcp_close(tp); |
193 | } | |
194 | ||
0974b45c BJ |
195 | /* |
196 | * Close a TCP control block: | |
197 | * discard all space held by the tcp | |
198 | * discard internet protocol block | |
199 | * wake up any sleepers | |
200 | */ | |
a6503abf BJ |
201 | tcp_close(tp) |
202 | register struct tcpcb *tp; | |
203 | { | |
204 | register struct tcpiphdr *t; | |
364801f5 BJ |
205 | struct inpcb *inp = tp->t_inpcb; |
206 | struct socket *so = inp->inp_socket; | |
a6503abf BJ |
207 | |
208 | COUNT(TCP_CLOSE); | |
a6503abf BJ |
209 | t = tp->seg_next; |
210 | for (; t != (struct tcpiphdr *)tp; t = (struct tcpiphdr *)t->ti_next) | |
211 | m_freem(dtom(t)); | |
0974b45c | 212 | if (tp->t_template) |
a6503abf | 213 | (void) m_free(dtom(tp->t_template)); |
0974b45c BJ |
214 | if (tp->t_tcpopt) |
215 | (void) m_free(dtom(tp->t_tcpopt)); | |
216 | if (tp->t_ipopt) | |
217 | (void) m_free(dtom(tp->t_ipopt)); | |
a6503abf | 218 | (void) m_free(dtom(tp)); |
364801f5 | 219 | inp->inp_ppcb = 0; |
252398a6 | 220 | in_pcbdetach(inp); |
4aed14e3 | 221 | soisdisconnected(so); |
a6503abf BJ |
222 | } |
223 | ||
a6503abf BJ |
224 | tcp_drain() |
225 | { | |
a6503abf BJ |
226 | |
227 | COUNT(TCP_DRAIN); | |
228 | } | |
229 | ||
230 | tcp_ctlinput(m) | |
231 | struct mbuf *m; | |
232 | { | |
233 | ||
234 | COUNT(TCP_CTLINPUT); | |
235 | m_freem(m); | |
236 | } |