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