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