tcp_usrreq.c revision 1549
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 */ 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 */ 65extern 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*/ 73int 74tcp_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 = 0; 81 int s; 82 int error = 0; 83 int ostate; 84 85 if (req == PRU_CONTROL) 86 return (in_control(so, (int)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 return (EINVAL); /* XXX */ 105 } 106 if (inp) { 107 tp = intotcpcb(inp); 108 /* WHAT IF TP IS 0? */ 109#ifdef KPROF 110 tcp_acounts[tp->t_state][req]++; 111#endif 112 ostate = tp->t_state; 113 } else 114 ostate = 0; 115 switch (req) { 116 117 /* 118 * TCP attaches to socket via PRU_ATTACH, reserving space, 119 * and an internet control block. 120 */ 121 case PRU_ATTACH: 122 if (inp) { 123 error = EISCONN; 124 break; 125 } 126 error = tcp_attach(so); 127 if (error) 128 break; 129 if ((so->so_options & SO_LINGER) && so->so_linger == 0) 130 so->so_linger = TCP_LINGERTIME; 131 tp = sototcpcb(so); 132 break; 133 134 /* 135 * PRU_DETACH detaches the TCP protocol from the socket. 136 * If the protocol state is non-embryonic, then can't 137 * do this directly: have to initiate a PRU_DISCONNECT, 138 * which may finish later; embryonic TCB's can just 139 * be discarded here. 140 */ 141 case PRU_DETACH: 142 if (tp->t_state > TCPS_LISTEN) 143 tp = tcp_disconnect(tp); 144 else 145 tp = tcp_close(tp); 146 break; 147 148 /* 149 * Give the socket an address. 150 */ 151 case PRU_BIND: 152 error = in_pcbbind(inp, nam); 153 if (error) 154 break; 155 break; 156 157 /* 158 * Prepare to accept connections. 159 */ 160 case PRU_LISTEN: 161 if (inp->inp_lport == 0) 162 error = in_pcbbind(inp, (struct mbuf *)0); 163 if (error == 0) 164 tp->t_state = TCPS_LISTEN; 165 break; 166 167 /* 168 * Initiate connection to peer. 169 * Create a template for use in transmissions on this connection. 170 * Enter SYN_SENT state, and mark socket as connecting. 171 * Start keep-alive timer, and seed output sequence space. 172 * Send initial segment on connection. 173 */ 174 case PRU_CONNECT: 175 if (inp->inp_lport == 0) { 176 error = in_pcbbind(inp, (struct mbuf *)0); 177 if (error) 178 break; 179 } 180 error = in_pcbconnect(inp, nam); 181 if (error) 182 break; 183 tp->t_template = tcp_template(tp); 184 if (tp->t_template == 0) { 185 in_pcbdisconnect(inp); 186 error = ENOBUFS; 187 break; 188 } 189 /* Compute window scaling to request. */ 190 while (tp->request_r_scale < TCP_MAX_WINSHIFT && 191 (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat) 192 tp->request_r_scale++; 193 soisconnecting(so); 194 tcpstat.tcps_connattempt++; 195 tp->t_state = TCPS_SYN_SENT; 196 tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT; 197 tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2; 198 tcp_sendseqinit(tp); 199 error = tcp_output(tp); 200 break; 201 202 /* 203 * Create a TCP connection between two sockets. 204 */ 205 case PRU_CONNECT2: 206 error = EOPNOTSUPP; 207 break; 208 209 /* 210 * Initiate disconnect from peer. 211 * If connection never passed embryonic stage, just drop; 212 * else if don't need to let data drain, then can just drop anyways, 213 * else have to begin TCP shutdown process: mark socket disconnecting, 214 * drain unread data, state switch to reflect user close, and 215 * send segment (e.g. FIN) to peer. Socket will be really disconnected 216 * when peer sends FIN and acks ours. 217 * 218 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB. 219 */ 220 case PRU_DISCONNECT: 221 tp = tcp_disconnect(tp); 222 break; 223 224 /* 225 * Accept a connection. Essentially all the work is 226 * done at higher levels; just return the address 227 * of the peer, storing through addr. 228 */ 229 case PRU_ACCEPT: 230 in_setpeeraddr(inp, nam); 231 break; 232 233 /* 234 * Mark the connection as being incapable of further output. 235 */ 236 case PRU_SHUTDOWN: 237 socantsendmore(so); 238 tp = tcp_usrclosed(tp); 239 if (tp) 240 error = tcp_output(tp); 241 break; 242 243 /* 244 * After a receive, possibly send window update to peer. 245 */ 246 case PRU_RCVD: 247 (void) tcp_output(tp); 248 break; 249 250 /* 251 * Do a send by putting data in output queue and updating urgent 252 * marker if URG set. Possibly send more data. 253 */ 254 case PRU_SEND: 255 sbappend(&so->so_snd, m); 256 error = tcp_output(tp); 257 break; 258 259 /* 260 * Abort the TCP. 261 */ 262 case PRU_ABORT: 263 tp = tcp_drop(tp, ECONNABORTED); 264 break; 265 266 case PRU_SENSE: 267 ((struct stat *) m)->st_blksize = so->so_snd.sb_hiwat; 268 (void) splx(s); 269 return (0); 270 271 case PRU_RCVOOB: 272 if ((so->so_oobmark == 0 && 273 (so->so_state & SS_RCVATMARK) == 0) || 274 so->so_options & SO_OOBINLINE || 275 tp->t_oobflags & TCPOOB_HADDATA) { 276 error = EINVAL; 277 break; 278 } 279 if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) { 280 error = EWOULDBLOCK; 281 break; 282 } 283 m->m_len = 1; 284 *mtod(m, caddr_t) = tp->t_iobc; 285 if (((int)nam & MSG_PEEK) == 0) 286 tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA); 287 break; 288 289 case PRU_SENDOOB: 290 if (sbspace(&so->so_snd) < -512) { 291 m_freem(m); 292 error = ENOBUFS; 293 break; 294 } 295 /* 296 * According to RFC961 (Assigned Protocols), 297 * the urgent pointer points to the last octet 298 * of urgent data. We continue, however, 299 * to consider it to indicate the first octet 300 * of data past the urgent section. 301 * Otherwise, snd_up should be one lower. 302 */ 303 sbappend(&so->so_snd, m); 304 tp->snd_up = tp->snd_una + so->so_snd.sb_cc; 305 tp->t_force = 1; 306 error = tcp_output(tp); 307 tp->t_force = 0; 308 break; 309 310 case PRU_SOCKADDR: 311 in_setsockaddr(inp, nam); 312 break; 313 314 case PRU_PEERADDR: 315 in_setpeeraddr(inp, nam); 316 break; 317 318 /* 319 * TCP slow timer went off; going through this 320 * routine for tracing's sake. 321 */ 322 case PRU_SLOWTIMO: 323 tp = tcp_timers(tp, (int)nam); 324 req |= (int)nam << 8; /* for debug's sake */ 325 break; 326 327 default: 328 panic("tcp_usrreq"); 329 } 330 if (tp && (so->so_options & SO_DEBUG)) 331 tcp_trace(TA_USER, ostate, tp, (struct tcpiphdr *)0, req); 332 splx(s); 333 return (error); 334} 335 336int 337tcp_ctloutput(op, so, level, optname, mp) 338 int op; 339 struct socket *so; 340 int level, optname; 341 struct mbuf **mp; 342{ 343 int error = 0, s; 344 struct inpcb *inp; 345 register struct tcpcb *tp; 346 register struct mbuf *m; 347 register int i; 348 349 s = splnet(); 350 inp = sotoinpcb(so); 351 if (inp == NULL) { 352 splx(s); 353 if (op == PRCO_SETOPT && *mp) 354 (void) m_free(*mp); 355 return (ECONNRESET); 356 } 357 if (level != IPPROTO_TCP) { 358 error = ip_ctloutput(op, so, level, optname, mp); 359 splx(s); 360 return (error); 361 } 362 tp = intotcpcb(inp); 363 364 switch (op) { 365 366 case PRCO_SETOPT: 367 m = *mp; 368 switch (optname) { 369 370 case TCP_NODELAY: 371 if (m == NULL || m->m_len < sizeof (int)) 372 error = EINVAL; 373 else if (*mtod(m, int *)) 374 tp->t_flags |= TF_NODELAY; 375 else 376 tp->t_flags &= ~TF_NODELAY; 377 break; 378 379 case TCP_MAXSEG: 380 if (m && (i = *mtod(m, int *)) > 0 && i <= tp->t_maxseg) 381 tp->t_maxseg = i; 382 else 383 error = EINVAL; 384 break; 385 386 default: 387 error = ENOPROTOOPT; 388 break; 389 } 390 if (m) 391 (void) m_free(m); 392 break; 393 394 case PRCO_GETOPT: 395 *mp = m = m_get(M_WAIT, MT_SOOPTS); 396 m->m_len = sizeof(int); 397 398 switch (optname) { 399 case TCP_NODELAY: 400 *mtod(m, int *) = tp->t_flags & TF_NODELAY; 401 break; 402 case TCP_MAXSEG: 403 *mtod(m, int *) = tp->t_maxseg; 404 break; 405 default: 406 error = ENOPROTOOPT; 407 break; 408 } 409 break; 410 } 411 splx(s); 412 return (error); 413} 414 415u_long tcp_sendspace = 1024*8; 416u_long tcp_recvspace = 1024*8; 417 418/* 419 * Attach TCP protocol to socket, allocating 420 * internet protocol control block, tcp control block, 421 * bufer space, and entering LISTEN state if to accept connections. 422 */ 423int 424tcp_attach(so) 425 struct socket *so; 426{ 427 register struct tcpcb *tp; 428 struct inpcb *inp; 429 int error; 430 431 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 432 error = soreserve(so, tcp_sendspace, tcp_recvspace); 433 if (error) 434 return (error); 435 } 436 error = in_pcballoc(so, &tcb); 437 if (error) 438 return (error); 439 inp = sotoinpcb(so); 440 tp = tcp_newtcpcb(inp); 441 if (tp == 0) { 442 int nofd = so->so_state & SS_NOFDREF; /* XXX */ 443 444 so->so_state &= ~SS_NOFDREF; /* don't free the socket yet */ 445 in_pcbdetach(inp); 446 so->so_state |= nofd; 447 return (ENOBUFS); 448 } 449 tp->t_state = TCPS_CLOSED; 450 return (0); 451} 452 453/* 454 * Initiate (or continue) disconnect. 455 * If embryonic state, just send reset (once). 456 * If in ``let data drain'' option and linger null, just drop. 457 * Otherwise (hard), mark socket disconnecting and drop 458 * current input data; switch states based on user close, and 459 * send segment to peer (with FIN). 460 */ 461struct tcpcb * 462tcp_disconnect(tp) 463 register struct tcpcb *tp; 464{ 465 struct socket *so = tp->t_inpcb->inp_socket; 466 467 if (tp->t_state < TCPS_ESTABLISHED) 468 tp = tcp_close(tp); 469 else if ((so->so_options & SO_LINGER) && so->so_linger == 0) 470 tp = tcp_drop(tp, 0); 471 else { 472 soisdisconnecting(so); 473 sbflush(&so->so_rcv); 474 tp = tcp_usrclosed(tp); 475 if (tp) 476 (void) tcp_output(tp); 477 } 478 return (tp); 479} 480 481/* 482 * User issued close, and wish to trail through shutdown states: 483 * if never received SYN, just forget it. If got a SYN from peer, 484 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN. 485 * If already got a FIN from peer, then almost done; go to LAST_ACK 486 * state. In all other cases, have already sent FIN to peer (e.g. 487 * after PRU_SHUTDOWN), and just have to play tedious game waiting 488 * for peer to send FIN or not respond to keep-alives, etc. 489 * We can let the user exit from the close as soon as the FIN is acked. 490 */ 491struct tcpcb * 492tcp_usrclosed(tp) 493 register struct tcpcb *tp; 494{ 495 496 switch (tp->t_state) { 497 498 case TCPS_CLOSED: 499 case TCPS_LISTEN: 500 case TCPS_SYN_SENT: 501 tp->t_state = TCPS_CLOSED; 502 tp = tcp_close(tp); 503 break; 504 505 case TCPS_SYN_RECEIVED: 506 case TCPS_ESTABLISHED: 507 tp->t_state = TCPS_FIN_WAIT_1; 508 break; 509 510 case TCPS_CLOSE_WAIT: 511 tp->t_state = TCPS_LAST_ACK; 512 break; 513 } 514 if (tp && tp->t_state >= TCPS_FIN_WAIT_2) 515 soisdisconnected(tp->t_inpcb->inp_socket); 516 return (tp); 517} 518