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