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