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