tcp_usrreq.c revision 2788
1/* 2 * Copyright (c) 1982, 1986, 1988, 1993 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.2 (Berkeley) 1/3/94 34 * $Id: tcp_usrreq.c,v 1.4 1994/08/02 07:49:15 davidg Exp $ 35 */ 36 37#include <sys/param.h> 38#include <sys/systm.h> 39#include <sys/malloc.h> 40#include <sys/mbuf.h> 41#include <sys/socket.h> 42#include <sys/socketvar.h> 43#include <sys/protosw.h> 44#include <sys/errno.h> 45#include <sys/stat.h> 46 47#include <net/if.h> 48#include <net/route.h> 49 50#include <netinet/in.h> 51#include <netinet/in_systm.h> 52#include <netinet/ip.h> 53#include <netinet/in_pcb.h> 54#include <netinet/ip_var.h> 55#include <netinet/tcp.h> 56#include <netinet/tcp_fsm.h> 57#include <netinet/tcp_seq.h> 58#include <netinet/tcp_timer.h> 59#include <netinet/tcp_var.h> 60#include <netinet/tcpip.h> 61#ifdef TCPDEBUG 62#include <netinet/tcp_debug.h> 63#endif 64 65/* 66 * TCP protocol interface to socket abstraction. 67 */ 68extern 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*/ 76int 77tcp_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 = 0; 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; 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#ifdef TCPDEBUG 334 if (tp && (so->so_options & SO_DEBUG)) 335 tcp_trace(TA_USER, ostate, tp, (struct tcpiphdr *)0, req); 336#endif 337 splx(s); 338 return (error); 339} 340 341int 342tcp_ctloutput(op, so, level, optname, mp) 343 int op; 344 struct socket *so; 345 int level, optname; 346 struct mbuf **mp; 347{ 348 int error = 0, s; 349 struct inpcb *inp; 350 register struct tcpcb *tp; 351 register struct mbuf *m; 352 register int i; 353 354 s = splnet(); 355 inp = sotoinpcb(so); 356 if (inp == NULL) { 357 splx(s); 358 if (op == PRCO_SETOPT && *mp) 359 (void) m_free(*mp); 360 return (ECONNRESET); 361 } 362 if (level != IPPROTO_TCP) { 363 error = ip_ctloutput(op, so, level, optname, mp); 364 splx(s); 365 return (error); 366 } 367 tp = intotcpcb(inp); 368 369 switch (op) { 370 371 case PRCO_SETOPT: 372 m = *mp; 373 switch (optname) { 374 375 case TCP_NODELAY: 376 if (m == NULL || m->m_len < sizeof (int)) 377 error = EINVAL; 378 else if (*mtod(m, int *)) 379 tp->t_flags |= TF_NODELAY; 380 else 381 tp->t_flags &= ~TF_NODELAY; 382 break; 383 384 case TCP_MAXSEG: 385 if (m && (i = *mtod(m, int *)) > 0 && i <= tp->t_maxseg) 386 tp->t_maxseg = i; 387 else 388 error = EINVAL; 389 break; 390 391 default: 392 error = ENOPROTOOPT; 393 break; 394 } 395 if (m) 396 (void) m_free(m); 397 break; 398 399 case PRCO_GETOPT: 400 *mp = m = m_get(M_WAIT, MT_SOOPTS); 401 m->m_len = sizeof(int); 402 403 switch (optname) { 404 case TCP_NODELAY: 405 *mtod(m, int *) = tp->t_flags & TF_NODELAY; 406 break; 407 case TCP_MAXSEG: 408 *mtod(m, int *) = tp->t_maxseg; 409 break; 410 default: 411 error = ENOPROTOOPT; 412 break; 413 } 414 break; 415 } 416 splx(s); 417 return (error); 418} 419 420/* 421 * tcp_sendspace and tcp_recvspace are the default send and receive window 422 * sizes, respectively. These are obsolescent (this information should 423 * be set by the route). 424 */ 425#ifdef TCP_SMALLSPACE 426u_long tcp_sendspace = 1024*4; 427u_long tcp_recvspace = 1024*4; 428#else 429u_long tcp_sendspace = 1024*16; 430u_long tcp_recvspace = 1024*16; 431#endif 432 433/* 434 * Attach TCP protocol to socket, allocating 435 * internet protocol control block, tcp control block, 436 * bufer space, and entering LISTEN state if to accept connections. 437 */ 438int 439tcp_attach(so) 440 struct socket *so; 441{ 442 register struct tcpcb *tp; 443 struct inpcb *inp; 444 int error; 445 446 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 447 error = soreserve(so, tcp_sendspace, tcp_recvspace); 448 if (error) 449 return (error); 450 } 451 error = in_pcballoc(so, &tcb); 452 if (error) 453 return (error); 454 inp = sotoinpcb(so); 455 tp = tcp_newtcpcb(inp); 456 if (tp == 0) { 457 int nofd = so->so_state & SS_NOFDREF; /* XXX */ 458 459 so->so_state &= ~SS_NOFDREF; /* don't free the socket yet */ 460 in_pcbdetach(inp); 461 so->so_state |= nofd; 462 return (ENOBUFS); 463 } 464 tp->t_state = TCPS_CLOSED; 465 return (0); 466} 467 468/* 469 * Initiate (or continue) disconnect. 470 * If embryonic state, just send reset (once). 471 * If in ``let data drain'' option and linger null, just drop. 472 * Otherwise (hard), mark socket disconnecting and drop 473 * current input data; switch states based on user close, and 474 * send segment to peer (with FIN). 475 */ 476struct tcpcb * 477tcp_disconnect(tp) 478 register struct tcpcb *tp; 479{ 480 struct socket *so = tp->t_inpcb->inp_socket; 481 482 if (tp->t_state < TCPS_ESTABLISHED) 483 tp = tcp_close(tp); 484 else if ((so->so_options & SO_LINGER) && so->so_linger == 0) 485 tp = tcp_drop(tp, 0); 486 else { 487 soisdisconnecting(so); 488 sbflush(&so->so_rcv); 489 tp = tcp_usrclosed(tp); 490 if (tp) 491 (void) tcp_output(tp); 492 } 493 return (tp); 494} 495 496/* 497 * User issued close, and wish to trail through shutdown states: 498 * if never received SYN, just forget it. If got a SYN from peer, 499 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN. 500 * If already got a FIN from peer, then almost done; go to LAST_ACK 501 * state. In all other cases, have already sent FIN to peer (e.g. 502 * after PRU_SHUTDOWN), and just have to play tedious game waiting 503 * for peer to send FIN or not respond to keep-alives, etc. 504 * We can let the user exit from the close as soon as the FIN is acked. 505 */ 506struct tcpcb * 507tcp_usrclosed(tp) 508 register struct tcpcb *tp; 509{ 510 511 switch (tp->t_state) { 512 513 case TCPS_CLOSED: 514 case TCPS_LISTEN: 515 case TCPS_SYN_SENT: 516 tp->t_state = TCPS_CLOSED; 517 tp = tcp_close(tp); 518 break; 519 520 case TCPS_SYN_RECEIVED: 521 case TCPS_ESTABLISHED: 522 tp->t_state = TCPS_FIN_WAIT_1; 523 break; 524 525 case TCPS_CLOSE_WAIT: 526 tp->t_state = TCPS_LAST_ACK; 527 break; 528 } 529 if (tp && tp->t_state >= TCPS_FIN_WAIT_2) 530 soisdisconnected(tp->t_inpcb->inp_socket); 531 return (tp); 532} 533