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