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