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