tcp_usrreq.c revision 1.13 1 /* $NetBSD: tcp_usrreq.c,v 1.13 1995/03/21 07:48:14 glass Exp $ */
2
3 /*
4 * Copyright (c) 1982, 1986, 1988, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 * @(#)tcp_usrreq.c 8.2 (Berkeley) 1/3/94
36 */
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/malloc.h>
42 #include <sys/mbuf.h>
43 #include <sys/socket.h>
44 #include <sys/socketvar.h>
45 #include <sys/protosw.h>
46 #include <sys/errno.h>
47 #include <sys/stat.h>
48
49 #include <net/if.h>
50 #include <net/route.h>
51
52 #include <netinet/in.h>
53 #include <netinet/in_systm.h>
54 #include <netinet/ip.h>
55 #include <netinet/in_pcb.h>
56 #include <netinet/ip_var.h>
57 #include <netinet/tcp.h>
58 #include <netinet/tcp_fsm.h>
59 #include <netinet/tcp_seq.h>
60 #include <netinet/tcp_timer.h>
61 #include <netinet/tcp_var.h>
62 #include <netinet/tcpip.h>
63 #include <netinet/tcp_debug.h>
64
65 /*
66 * TCP protocol interface to socket abstraction.
67 */
68 extern char *tcpstates[];
69
70 /*
71 * Process a TCP user request for TCP tb. If this is a send request
72 * then m is the mbuf chain of send data. If this is a timer expiration
73 * (called from the software clock routine), then timertype tells which timer.
74 */
75 /*ARGSUSED*/
76 int
77 tcp_usrreq(so, req, m, nam, control)
78 struct socket *so;
79 int req;
80 struct mbuf *m, *nam, *control;
81 {
82 register struct inpcb *inp;
83 register struct tcpcb *tp;
84 int s;
85 int error = 0;
86 int ostate;
87
88 if (req == PRU_CONTROL)
89 return (in_control(so, (int)m, (caddr_t)nam,
90 (struct ifnet *)control));
91 if (control && control->m_len) {
92 m_freem(control);
93 if (m)
94 m_freem(m);
95 return (EINVAL);
96 }
97
98 s = splnet();
99 inp = sotoinpcb(so);
100 /*
101 * When a TCP is attached to a socket, then there will be
102 * a (struct inpcb) pointed at by the socket, and this
103 * structure will point at a subsidary (struct tcpcb).
104 */
105 if (inp == 0 && req != PRU_ATTACH) {
106 splx(s);
107 return (EINVAL); /* XXX */
108 }
109 if (inp) {
110 tp = intotcpcb(inp);
111 /* WHAT IF TP IS 0? */
112 #ifdef KPROF
113 tcp_acounts[tp->t_state][req]++;
114 #endif
115 ostate = tp->t_state;
116 } else
117 ostate = 0;
118 switch (req) {
119
120 /*
121 * TCP attaches to socket via PRU_ATTACH, reserving space,
122 * and an internet control block.
123 */
124 case PRU_ATTACH:
125 if (inp) {
126 error = EISCONN;
127 break;
128 }
129 error = tcp_attach(so);
130 if (error)
131 break;
132 if ((so->so_options & SO_LINGER) && so->so_linger == 0)
133 so->so_linger = TCP_LINGERTIME * hz;
134 tp = sototcpcb(so);
135 break;
136
137 /*
138 * PRU_DETACH detaches the TCP protocol from the socket.
139 * If the protocol state is non-embryonic, then can't
140 * do this directly: have to initiate a PRU_DISCONNECT,
141 * which may finish later; embryonic TCB's can just
142 * be discarded here.
143 */
144 case PRU_DETACH:
145 if (tp->t_state > TCPS_LISTEN)
146 tp = tcp_disconnect(tp);
147 else
148 tp = tcp_close(tp);
149 break;
150
151 /*
152 * Give the socket an address.
153 */
154 case PRU_BIND:
155 error = in_pcbbind(inp, nam);
156 if (error)
157 break;
158 break;
159
160 /*
161 * Prepare to accept connections.
162 */
163 case PRU_LISTEN:
164 if (inp->inp_lport == 0)
165 error = in_pcbbind(inp, (struct mbuf *)0);
166 if (error == 0)
167 tp->t_state = TCPS_LISTEN;
168 break;
169
170 /*
171 * Initiate connection to peer.
172 * Create a template for use in transmissions on this connection.
173 * Enter SYN_SENT state, and mark socket as connecting.
174 * Start keep-alive timer, and seed output sequence space.
175 * Send initial segment on connection.
176 */
177 case PRU_CONNECT:
178 if (inp->inp_lport == 0) {
179 error = in_pcbbind(inp, (struct mbuf *)0);
180 if (error)
181 break;
182 }
183 error = in_pcbconnect(inp, nam);
184 if (error)
185 break;
186 tp->t_template = tcp_template(tp);
187 if (tp->t_template == 0) {
188 in_pcbdisconnect(inp);
189 error = ENOBUFS;
190 break;
191 }
192 /* Compute window scaling to request. */
193 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
194 (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
195 tp->request_r_scale++;
196 soisconnecting(so);
197 tcpstat.tcps_connattempt++;
198 tp->t_state = TCPS_SYN_SENT;
199 tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT;
200 tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2;
201 tcp_sendseqinit(tp);
202 error = tcp_output(tp);
203 break;
204
205 /*
206 * Create a TCP connection between two sockets.
207 */
208 case PRU_CONNECT2:
209 error = EOPNOTSUPP;
210 break;
211
212 /*
213 * Initiate disconnect from peer.
214 * If connection never passed embryonic stage, just drop;
215 * else if don't need to let data drain, then can just drop anyways,
216 * else have to begin TCP shutdown process: mark socket disconnecting,
217 * drain unread data, state switch to reflect user close, and
218 * send segment (e.g. FIN) to peer. Socket will be really disconnected
219 * when peer sends FIN and acks ours.
220 *
221 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
222 */
223 case PRU_DISCONNECT:
224 tp = tcp_disconnect(tp);
225 break;
226
227 /*
228 * Accept a connection. Essentially all the work is
229 * done at higher levels; just return the address
230 * of the peer, storing through addr.
231 */
232 case PRU_ACCEPT:
233 in_setpeeraddr(inp, nam);
234 break;
235
236 /*
237 * Mark the connection as being incapable of further output.
238 */
239 case PRU_SHUTDOWN:
240 socantsendmore(so);
241 tp = tcp_usrclosed(tp);
242 if (tp)
243 error = tcp_output(tp);
244 break;
245
246 /*
247 * After a receive, possibly send window update to peer.
248 */
249 case PRU_RCVD:
250 (void) tcp_output(tp);
251 break;
252
253 /*
254 * Do a send by putting data in output queue and updating urgent
255 * marker if URG set. Possibly send more data.
256 */
257 case PRU_SEND:
258 sbappend(&so->so_snd, m);
259 error = tcp_output(tp);
260 break;
261
262 /*
263 * Abort the TCP.
264 */
265 case PRU_ABORT:
266 tp = tcp_drop(tp, ECONNABORTED);
267 break;
268
269 case PRU_SENSE:
270 ((struct stat *) m)->st_blksize = so->so_snd.sb_hiwat;
271 (void) splx(s);
272 return (0);
273
274 case PRU_RCVOOB:
275 if ((so->so_oobmark == 0 &&
276 (so->so_state & SS_RCVATMARK) == 0) ||
277 so->so_options & SO_OOBINLINE ||
278 tp->t_oobflags & TCPOOB_HADDATA) {
279 error = EINVAL;
280 break;
281 }
282 if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
283 error = EWOULDBLOCK;
284 break;
285 }
286 m->m_len = 1;
287 *mtod(m, caddr_t) = tp->t_iobc;
288 if (((int)nam & MSG_PEEK) == 0)
289 tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
290 break;
291
292 case PRU_SENDOOB:
293 if (sbspace(&so->so_snd) < -512) {
294 m_freem(m);
295 error = ENOBUFS;
296 break;
297 }
298 /*
299 * According to RFC961 (Assigned Protocols),
300 * the urgent pointer points to the last octet
301 * of urgent data. We continue, however,
302 * to consider it to indicate the first octet
303 * of data past the urgent section.
304 * Otherwise, snd_up should be one lower.
305 */
306 sbappend(&so->so_snd, m);
307 tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
308 tp->t_force = 1;
309 error = tcp_output(tp);
310 tp->t_force = 0;
311 break;
312
313 case PRU_SOCKADDR:
314 in_setsockaddr(inp, nam);
315 break;
316
317 case PRU_PEERADDR:
318 in_setpeeraddr(inp, nam);
319 break;
320
321 /*
322 * TCP slow timer went off; going through this
323 * routine for tracing's sake.
324 */
325 case PRU_SLOWTIMO:
326 tp = tcp_timers(tp, (int)nam);
327 req |= (int)nam << 8; /* for debug's sake */
328 break;
329
330 default:
331 panic("tcp_usrreq");
332 }
333 if (tp && (so->so_options & SO_DEBUG))
334 tcp_trace(TA_USER, ostate, tp, (struct tcpiphdr *)0, req);
335 splx(s);
336 return (error);
337 }
338
339 int
340 tcp_ctloutput(op, so, level, optname, mp)
341 int op;
342 struct socket *so;
343 int level, optname;
344 struct mbuf **mp;
345 {
346 int error = 0, s;
347 struct inpcb *inp;
348 register struct tcpcb *tp;
349 register struct mbuf *m;
350 register int i;
351
352 s = splnet();
353 inp = sotoinpcb(so);
354 if (inp == NULL) {
355 splx(s);
356 if (op == PRCO_SETOPT && *mp)
357 (void) m_free(*mp);
358 return (ECONNRESET);
359 }
360 if (level != IPPROTO_TCP) {
361 error = ip_ctloutput(op, so, level, optname, mp);
362 splx(s);
363 return (error);
364 }
365 tp = intotcpcb(inp);
366
367 switch (op) {
368
369 case PRCO_SETOPT:
370 m = *mp;
371 switch (optname) {
372
373 case TCP_NODELAY:
374 if (m == NULL || m->m_len < sizeof (int))
375 error = EINVAL;
376 else if (*mtod(m, int *))
377 tp->t_flags |= TF_NODELAY;
378 else
379 tp->t_flags &= ~TF_NODELAY;
380 break;
381
382 case TCP_MAXSEG:
383 if (m && (i = *mtod(m, int *)) > 0 && i <= tp->t_maxseg)
384 tp->t_maxseg = i;
385 else
386 error = EINVAL;
387 break;
388
389 default:
390 error = ENOPROTOOPT;
391 break;
392 }
393 if (m)
394 (void) m_free(m);
395 break;
396
397 case PRCO_GETOPT:
398 *mp = m = m_get(M_WAIT, MT_SOOPTS);
399 m->m_len = sizeof(int);
400
401 switch (optname) {
402 case TCP_NODELAY:
403 *mtod(m, int *) = tp->t_flags & TF_NODELAY;
404 break;
405 case TCP_MAXSEG:
406 *mtod(m, int *) = tp->t_maxseg;
407 break;
408 default:
409 error = ENOPROTOOPT;
410 break;
411 }
412 break;
413 }
414 splx(s);
415 return (error);
416 }
417
418 #ifndef TCP_SENDSPACE
419 #define TCP_SENDSPACE 1024*16;
420 #endif
421 u_long tcp_sendspace = TCP_SENDSPACE;
422 #ifndef TCP_RECVSPACE
423 #define TCP_RECVSPACE 1024*16;
424 #endif
425 u_long tcp_recvspace = TCP_RECVSPACE;
426
427 /*
428 * Attach TCP protocol to socket, allocating
429 * internet protocol control block, tcp control block,
430 * bufer space, and entering LISTEN state if to accept connections.
431 */
432 int
433 tcp_attach(so)
434 struct socket *so;
435 {
436 register struct tcpcb *tp;
437 struct inpcb *inp;
438 int error;
439
440 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
441 error = soreserve(so, tcp_sendspace, tcp_recvspace);
442 if (error)
443 return (error);
444 }
445 error = in_pcballoc(so, &tcb);
446 if (error)
447 return (error);
448 inp = sotoinpcb(so);
449 tp = tcp_newtcpcb(inp);
450 if (tp == 0) {
451 int nofd = so->so_state & SS_NOFDREF; /* XXX */
452
453 so->so_state &= ~SS_NOFDREF; /* don't free the socket yet */
454 in_pcbdetach(inp);
455 so->so_state |= nofd;
456 return (ENOBUFS);
457 }
458 tp->t_state = TCPS_CLOSED;
459 return (0);
460 }
461
462 /*
463 * Initiate (or continue) disconnect.
464 * If embryonic state, just send reset (once).
465 * If in ``let data drain'' option and linger null, just drop.
466 * Otherwise (hard), mark socket disconnecting and drop
467 * current input data; switch states based on user close, and
468 * send segment to peer (with FIN).
469 */
470 struct tcpcb *
471 tcp_disconnect(tp)
472 register struct tcpcb *tp;
473 {
474 struct socket *so = tp->t_inpcb->inp_socket;
475
476 if (TCPS_HAVEESTABLISHED(tp->t_state) == 0)
477 tp = tcp_close(tp);
478 else if ((so->so_options & SO_LINGER) && so->so_linger == 0)
479 tp = tcp_drop(tp, 0);
480 else {
481 soisdisconnecting(so);
482 sbflush(&so->so_rcv);
483 tp = tcp_usrclosed(tp);
484 if (tp)
485 (void) tcp_output(tp);
486 }
487 return (tp);
488 }
489
490 /*
491 * User issued close, and wish to trail through shutdown states:
492 * if never received SYN, just forget it. If got a SYN from peer,
493 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
494 * If already got a FIN from peer, then almost done; go to LAST_ACK
495 * state. In all other cases, have already sent FIN to peer (e.g.
496 * after PRU_SHUTDOWN), and just have to play tedious game waiting
497 * for peer to send FIN or not respond to keep-alives, etc.
498 * We can let the user exit from the close as soon as the FIN is acked.
499 */
500 struct tcpcb *
501 tcp_usrclosed(tp)
502 register struct tcpcb *tp;
503 {
504
505 switch (tp->t_state) {
506
507 case TCPS_CLOSED:
508 case TCPS_LISTEN:
509 case TCPS_SYN_SENT:
510 tp->t_state = TCPS_CLOSED;
511 tp = tcp_close(tp);
512 break;
513
514 case TCPS_SYN_RECEIVED:
515 case TCPS_ESTABLISHED:
516 tp->t_state = TCPS_FIN_WAIT_1;
517 break;
518
519 case TCPS_CLOSE_WAIT:
520 tp->t_state = TCPS_LAST_ACK;
521 break;
522 }
523 if (tp && tp->t_state >= TCPS_FIN_WAIT_2)
524 soisdisconnected(tp->t_inpcb->inp_socket);
525 return (tp);
526 }
527