fix per Jeffrey Jongeward
[unix-history] / usr / src / sys / netinet / tcp_usrreq.c
... / ...
CommitLineData
1/* tcp_usrreq.c 1.57 82/04/30 */
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 */
28extern char *tcpstates[];
29struct 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 */
36tcp_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;
47COUNT(TCP_USRREQ);
48
49 /*
50 * When a TCP is attached to a socket, then there will be
51 * a (struct inpcb) pointed at by the socket, and this
52 * structure will point at a subsidary (struct tcpcb).
53 * The normal sequence of events is:
54 * PRU_ATTACH creating these structures
55 * PRU_CONNECT connecting to a remote peer
56 * (PRU_SEND|PRU_RCVD)* exchanging data
57 * PRU_DISCONNECT disconnecting from remote peer
58 * PRU_DETACH deleting the structures
59 * With the operations from PRU_CONNECT through PRU_DISCONNECT
60 * possible repeated several times.
61 *
62 * MULTIPLE CONNECTS ARE NOT YET IMPLEMENTED.
63 */
64 if (inp == 0 && req != PRU_ATTACH) {
65 splx(s);
66 return (EINVAL); /* XXX */
67 }
68 if (inp) {
69 tp = intotcpcb(inp);
70#ifdef KPROF
71 tcp_acounts[tp->t_state][req]++;
72#endif
73 ostate = tp->t_state;
74 }
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;
234printf("sendoob seq now %x oobc %x\n", tp->t_oobseq, tp->t_oobc);
235 tp->t_oobflags |= TCPOOB_NEEDACK;
236 /* what to do ...? */
237 if (error = tcp_output(tp))
238 break;
239 }
240#endif
241 if (sbspace(&so->so_snd) < -512) {
242 error = ENOBUFS;
243 break;
244 }
245 tp->snd_up = tp->snd_una + so->so_snd.sb_cc + 1;
246 sbappend(&so->so_snd, m);
247#ifdef notdef
248 if (tp->t_flags & TF_PUSH)
249 tp->snd_end = tp->snd_una + so->so_snd.sb_cc;
250#endif
251 tp->t_force = 1;
252 error = tcp_output(tp);
253 tp->t_force = 0;
254 break;
255
256 case PRU_SOCKADDR:
257 in_setsockaddr((struct sockaddr_in *)addr, inp);
258 break;
259
260 /*
261 * TCP slow timer went off; going through this
262 * routine for tracing's sake.
263 */
264 case PRU_SLOWTIMO:
265 tcp_timers(tp, (int)addr);
266 req |= (int)addr << 8; /* for debug's sake */
267 break;
268
269 default:
270 panic("tcp_usrreq");
271 }
272 if (tp && (so->so_options & SO_DEBUG))
273 tcp_trace(TA_USER, ostate, tp, (struct tcpiphdr *)0, req);
274 splx(s);
275 return (error);
276}
277
278int tcp_sendspace = 1024*2;
279int tcp_recvspace = 1024*2;
280/*
281 * Attach TCP protocol to socket, allocating
282 * internet protocol control block, tcp control block,
283 * bufer space, and entering LISTEN state if to accept connections.
284 */
285tcp_attach(so, sa)
286 struct socket *so;
287 struct sockaddr *sa;
288{
289 register struct tcpcb *tp;
290 struct inpcb *inp;
291 int error;
292
293 error = in_pcbattach(so, &tcb,
294 tcp_sendspace, tcp_recvspace, (struct sockaddr_in *)sa);
295 if (error)
296 return (error);
297 inp = (struct inpcb *)so->so_pcb;
298 tp = tcp_newtcpcb(inp);
299 if (so->so_options & SO_ACCEPTCONN) {
300 if (tp == 0) {
301 in_pcbdetach(inp);
302 return (ENOBUFS);
303 }
304 tp->t_state = TCPS_LISTEN;
305 } else
306 tp->t_state = TCPS_CLOSED;
307 return (0);
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);
325 else if (so->so_linger == 0)
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.
343 * We can let the user exit from the close as soon as the FIN is acked.
344 */
345tcp_usrclosed(tp)
346 struct tcpcb *tp;
347{
348
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 }
366 if (tp->t_state >= TCPS_FIN_WAIT_2)
367 soisdisconnected(tp->t_inpcb->inp_socket);
368}