date and time created 82/12/16 14:38:25 by sam
[unix-history] / usr / src / sys / netinet / tcp_usrreq.c
CommitLineData
59965020 1/* tcp_usrreq.c 1.70 82/11/03 */
72f24d7d 2
4eb5d593 3#include "../h/param.h"
72f24d7d 4#include "../h/systm.h"
dad64fdf
BJ
5#include "../h/mbuf.h"
6#include "../h/socket.h"
eee3ab16
BJ
7#include "../h/socketvar.h"
8#include "../h/protosw.h"
6e7edb25
BJ
9
10#include "../net/if.h"
c124e997 11#include "../net/route.h"
6e7edb25 12#include "../netinet/in.h"
839fe741
BJ
13#include "../netinet/in_pcb.h"
14#include "../netinet/in_systm.h"
839fe741
BJ
15#include "../netinet/ip.h"
16#include "../netinet/ip_var.h"
17#include "../netinet/tcp.h"
18#include "../netinet/tcp_fsm.h"
19#include "../netinet/tcp_seq.h"
20#include "../netinet/tcp_timer.h"
21#include "../netinet/tcp_var.h"
22#include "../netinet/tcpip.h"
23#include "../netinet/tcp_debug.h"
8a2f82db 24#include <errno.h>
eee3ab16 25
290e0b0a
BJ
26/*
27 * TCP protocol interface to socket abstraction.
28 */
29extern char *tcpstates[];
4ad99bae 30struct tcpcb *tcp_newtcpcb();
290e0b0a 31
9c5022e3 32/*
290e0b0a 33 * Process a TCP user request for TCP tb. If this is a send request
9c5022e3
BJ
34 * then m is the mbuf chain of send data. If this is a timer expiration
35 * (called from the software clock routine), then timertype tells which timer.
36 */
a8d3bf7f 37/*ARGSUSED*/
8075bb0e 38tcp_usrreq(so, req, m, nam, opt)
eee3ab16
BJ
39 struct socket *so;
40 int req;
8075bb0e
BJ
41 struct mbuf *m, *nam;
42 struct socketopt *opt;
4eb5d593 43{
53a5409e 44 register struct inpcb *inp = sotoinpcb(so);
cdad2eb1 45 register struct tcpcb *tp;
72f24d7d 46 int s = splnet();
eee3ab16 47 int error = 0;
17b82ed4 48 int ostate;
72f24d7d 49
53a5409e 50 /*
290e0b0a
BJ
51 * When a TCP is attached to a socket, then there will be
52 * a (struct inpcb) pointed at by the socket, and this
53 * structure will point at a subsidary (struct tcpcb).
53a5409e 54 */
0974b45c 55 if (inp == 0 && req != PRU_ATTACH) {
a6503abf 56 splx(s);
290e0b0a 57 return (EINVAL); /* XXX */
a6503abf
BJ
58 }
59 if (inp) {
cdad2eb1 60 tp = intotcpcb(inp);
8075bb0e 61 /* WHAT IF TP IS 0? */
9c5022e3 62#ifdef KPROF
a6503abf 63 tcp_acounts[tp->t_state][req]++;
9c5022e3 64#endif
17b82ed4 65 ostate = tp->t_state;
ebf42a75
BJ
66 } else
67 ostate = 0;
eee3ab16 68 switch (req) {
4eb5d593 69
290e0b0a
BJ
70 /*
71 * TCP attaches to socket via PRU_ATTACH, reserving space,
8075bb0e 72 * and an internet control block.
290e0b0a 73 */
eee3ab16 74 case PRU_ATTACH:
4ad99bae 75 if (inp) {
eee3ab16 76 error = EISCONN;
cdad2eb1 77 break;
53a5409e 78 }
a1edc12b 79 error = tcp_attach(so);
a6503abf 80 if (error)
4ad99bae 81 break;
8e65fd66
BJ
82 if ((so->so_options & SO_DONTLINGER) == 0)
83 so->so_linger = TCP_LINGERTIME;
290e0b0a 84 tp = sototcpcb(so);
72f24d7d 85 break;
4eb5d593 86
290e0b0a
BJ
87 /*
88 * PRU_DETACH detaches the TCP protocol from the socket.
89 * If the protocol state is non-embryonic, then can't
90 * do this directly: have to initiate a PRU_DISCONNECT,
91 * which may finish later; embryonic TCB's can just
92 * be discarded here.
93 */
eee3ab16 94 case PRU_DETACH:
290e0b0a
BJ
95 if (tp->t_state > TCPS_LISTEN)
96 tcp_disconnect(tp);
97 else {
98 tcp_close(tp);
99 tp = 0;
100 }
eee3ab16
BJ
101 break;
102
8075bb0e
BJ
103 /*
104 * Give the socket an address.
105 */
106 case PRU_BIND:
107 error = in_pcbbind(inp, nam);
108 if (error)
109 break;
110 break;
111
112 /*
113 * Prepare to accept connections.
114 */
115 case PRU_LISTEN:
116 if (inp->inp_lport == 0)
117 error = in_pcbbind(inp, (struct mbuf *)0);
118 if (error == 0)
119 tp->t_state = TCPS_LISTEN;
120 break;
121
290e0b0a
BJ
122 /*
123 * Initiate connection to peer.
124 * Create a template for use in transmissions on this connection.
125 * Enter SYN_SENT state, and mark socket as connecting.
126 * Start keep-alive timer, and seed output sequence space.
127 * Send initial segment on connection.
128 */
eee3ab16 129 case PRU_CONNECT:
8075bb0e
BJ
130 if (inp->inp_lport == 0) {
131 error = in_pcbbind(inp, (struct mbuf *)0);
132 if (error)
133 break;
134 }
135 error = in_pcbconnect(inp, nam);
4ad99bae 136 if (error)
53a5409e 137 break;
b454c3ea 138 tp->t_template = tcp_template(tp);
290e0b0a
BJ
139 if (tp->t_template == 0) {
140 in_pcbdisconnect(inp);
141 error = ENOBUFS;
142 break;
143 }
53a5409e 144 soisconnecting(so);
a6503abf 145 tp->t_state = TCPS_SYN_SENT;
4aed14e3
BJ
146 tp->t_timer[TCPT_KEEP] = TCPTV_KEEP;
147 tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2;
148 tcp_sendseqinit(tp);
8a2f82db 149 error = tcp_output(tp);
72f24d7d 150 break;
4eb5d593 151
290e0b0a
BJ
152 /*
153 * Initiate disconnect from peer.
154 * If connection never passed embryonic stage, just drop;
155 * else if don't need to let data drain, then can just drop anyways,
156 * else have to begin TCP shutdown process: mark socket disconnecting,
157 * drain unread data, state switch to reflect user close, and
158 * send segment (e.g. FIN) to peer. Socket will be really disconnected
159 * when peer sends FIN and acks ours.
160 *
161 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
162 */
163 case PRU_DISCONNECT:
164 tcp_disconnect(tp);
4aed14e3
BJ
165 break;
166
290e0b0a
BJ
167 /*
168 * Accept a connection. Essentially all the work is
169 * done at higher levels; just return the address
170 * of the peer, storing through addr.
171 */
1acff8ec 172 case PRU_ACCEPT: {
8075bb0e 173 struct sockaddr_in *sin = mtod(nam, struct sockaddr_in *);
1acff8ec 174
8075bb0e
BJ
175 nam->m_len = sizeof (struct sockaddr_in);
176 sin->sin_family = AF_INET;
177 sin->sin_port = inp->inp_fport;
178 sin->sin_addr = inp->inp_faddr;
eee3ab16 179 break;
8075bb0e 180 }
eee3ab16 181
290e0b0a
BJ
182 /*
183 * Mark the connection as being incapable of further output.
184 */
eee3ab16 185 case PRU_SHUTDOWN:
0974b45c 186 socantsendmore(so);
4aed14e3 187 tcp_usrclosed(tp);
8a2f82db 188 error = tcp_output(tp);
72f24d7d
BJ
189 break;
190
290e0b0a
BJ
191 /*
192 * After a receive, possibly send window update to peer.
193 */
eee3ab16 194 case PRU_RCVD:
f1b2fa5b 195 (void) tcp_output(tp);
72f24d7d
BJ
196 break;
197
290e0b0a
BJ
198 /*
199 * Do a send by putting data in output queue and updating urgent
200 * marker if URG set. Possibly send more data.
201 */
eee3ab16 202 case PRU_SEND:
a6503abf 203 sbappend(&so->so_snd, m);
8a2f82db 204#ifdef notdef
0974b45c 205 if (tp->t_flags & TF_PUSH)
a6503abf 206 tp->snd_end = tp->snd_una + so->so_snd.sb_cc;
8a2f82db
SL
207#endif
208 error = tcp_output(tp);
72f24d7d
BJ
209 break;
210
290e0b0a
BJ
211 /*
212 * Abort the TCP.
213 */
eee3ab16 214 case PRU_ABORT:
a6503abf 215 tcp_drop(tp, ECONNABORTED);
72f24d7d
BJ
216 break;
217
290e0b0a 218/* SOME AS YET UNIMPLEMENTED HOOKS */
eee3ab16 219 case PRU_CONTROL:
53a5409e 220 error = EOPNOTSUPP;
eee3ab16
BJ
221 break;
222
f1b2fa5b
BJ
223 case PRU_SENSE:
224 error = EOPNOTSUPP;
225 break;
0244dbc7 226/* END UNIMPLEMENTED HOOKS */
f1b2fa5b
BJ
227
228 case PRU_RCVOOB:
8b5a83bb
BJ
229 if (so->so_oobmark == 0 &&
230 (so->so_state & SS_RCVATMARK) == 0) {
0244dbc7
BJ
231 error = EINVAL;
232 break;
233 }
b2db9217 234 if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
8b5a83bb 235 error = EWOULDBLOCK;
b2db9217 236 break;
8b5a83bb 237 }
283ea225 238 m->m_len = 1;
b2db9217 239 *mtod(m, caddr_t) = tp->t_iobc;
f1b2fa5b
BJ
240 break;
241
242 case PRU_SENDOOB:
8b5a83bb
BJ
243 if (sbspace(&so->so_snd) < -512) {
244 error = ENOBUFS;
245 break;
246 }
0244dbc7
BJ
247 tp->snd_up = tp->snd_una + so->so_snd.sb_cc + 1;
248 sbappend(&so->so_snd, m);
b2db9217 249 tp->t_force = 1;
8a2f82db 250 error = tcp_output(tp);
b2db9217 251 tp->t_force = 0;
f1b2fa5b
BJ
252 break;
253
126472ab 254 case PRU_SOCKADDR:
8075bb0e 255 in_setsockaddr(inp, nam);
126472ab
SL
256 break;
257
290e0b0a
BJ
258 /*
259 * TCP slow timer went off; going through this
260 * routine for tracing's sake.
261 */
eee3ab16 262 case PRU_SLOWTIMO:
8075bb0e
BJ
263 tcp_timers(tp, (int)nam);
264 req |= (int)nam << 8; /* for debug's sake */
eee3ab16
BJ
265 break;
266
9c5022e3
BJ
267 default:
268 panic("tcp_usrreq");
72f24d7d 269 }
17b82ed4
BJ
270 if (tp && (so->so_options & SO_DEBUG))
271 tcp_trace(TA_USER, ostate, tp, (struct tcpiphdr *)0, req);
72f24d7d 272 splx(s);
53a5409e 273 return (error);
4eb5d593 274}
4aed14e3 275
306f91c9 276int tcp_sendspace = 1024*2;
1bc20e72 277int tcp_recvspace = 1024*2;
290e0b0a
BJ
278/*
279 * Attach TCP protocol to socket, allocating
280 * internet protocol control block, tcp control block,
281 * bufer space, and entering LISTEN state if to accept connections.
282 */
8075bb0e 283tcp_attach(so)
290e0b0a 284 struct socket *so;
290e0b0a
BJ
285{
286 register struct tcpcb *tp;
287 struct inpcb *inp;
288 int error;
289
59965020 290 error = soreserve(so, tcp_sendspace, tcp_recvspace);
ebf42a75
BJ
291 if (error)
292 goto bad;
293 error = in_pcballoc(so, &tcb);
290e0b0a 294 if (error)
8075bb0e
BJ
295 goto bad;
296 inp = sotoinpcb(so);
290e0b0a 297 tp = tcp_newtcpcb(inp);
ebf42a75
BJ
298 if (tp == 0) {
299 error = ENOBUFS;
300 goto bad2;
301 }
8075bb0e 302 tp->t_state = TCPS_CLOSED;
290e0b0a 303 return (0);
ebf42a75
BJ
304bad2:
305 in_pcbdetach(inp);
306bad:
307 return (error);
290e0b0a
BJ
308}
309
310/*
311 * Initiate (or continue) disconnect.
312 * If embryonic state, just send reset (once).
313 * If not in ``let data drain'' option, just drop.
314 * Otherwise (hard), mark socket disconnecting and drop
315 * current input data; switch states based on user close, and
316 * send segment to peer (with FIN).
317 */
318tcp_disconnect(tp)
319 struct tcpcb *tp;
320{
321 struct socket *so = tp->t_inpcb->inp_socket;
322
323 if (tp->t_state < TCPS_ESTABLISHED)
324 tcp_close(tp);
8e65fd66 325 else if (so->so_linger == 0)
290e0b0a
BJ
326 tcp_drop(tp, 0);
327 else {
328 soisdisconnecting(so);
329 sbflush(&so->so_rcv);
330 tcp_usrclosed(tp);
331 (void) tcp_output(tp);
332 }
333}
334
335/*
336 * User issued close, and wish to trail through shutdown states:
337 * if never received SYN, just forget it. If got a SYN from peer,
338 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
339 * If already got a FIN from peer, then almost done; go to LAST_ACK
340 * state. In all other cases, have already sent FIN to peer (e.g.
341 * after PRU_SHUTDOWN), and just have to play tedious game waiting
342 * for peer to send FIN or not respond to keep-alives, etc.
085a0b90 343 * We can let the user exit from the close as soon as the FIN is acked.
290e0b0a 344 */
4aed14e3
BJ
345tcp_usrclosed(tp)
346 struct tcpcb *tp;
347{
348
4aed14e3
BJ
349 switch (tp->t_state) {
350
351 case TCPS_LISTEN:
352 case TCPS_SYN_SENT:
353 tp->t_state = TCPS_CLOSED;
354 tcp_close(tp);
355 break;
356
357 case TCPS_SYN_RECEIVED:
358 case TCPS_ESTABLISHED:
359 tp->t_state = TCPS_FIN_WAIT_1;
360 break;
361
362 case TCPS_CLOSE_WAIT:
363 tp->t_state = TCPS_LAST_ACK;
364 break;
365 }
085a0b90
BJ
366 if (tp->t_state >= TCPS_FIN_WAIT_2)
367 soisdisconnected(tp->t_inpcb->inp_socket);
4aed14e3 368}