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